0

suppose i have built a VObject called vobj (e.g., built via vobject.readComponents(vcfStr)) and want to add a new key:value pair to it:

  print('k=%s v=%s' % (k,v))
  try:
      stmnt1 = "vobj.add('%s')" % (k)
      print('stmnt1:"%s"' % stmnt1)
      eval(stmnt1)

      print('vobj after add\n'+20*'#'+'\n')
      vobj.prettyPrint()
      stmnt2 = "vobj.%s.value = '%s'" % (k,v)
      print('\n'+20*'#'+('\nstmnt2:"%s"' % stmnt2))
      eval(stmnt2)
  except Exception as e:
      print('wazzup?!',e)

all the extra prints and the try:except are because i can't make it work! here's the output produced:

  k=bday v=1931-02-10
  stmnt1:"vobj.add('bday')"
  vobj after add
  ####################

   VCARD
      VERSION: 3.0
      PRODID: -//Apple Inc.//Mac OS X 10.12.3//EN
      N:  Foo Bar 
      FN: Foo Bar
      EMAIL: foo@bar.com
      params for  EMAIL:
         TYPE ['INTERNET', 'WORK', 'pref']
      ...
      BDAY: 

  ####################
  stmnt2:"vobj.bday.value = '1931-02-10'"
  wazzup?! invalid syntax (<string>, line 1)

i have three specific questions:

  1. VObject makes use of object.attribute "dot" notation, and the only way i've found to handle arbitrary key names is using eval(). there must be a more pythonic way?

  2. evaluation of the first statement stmnt1 works, and changes vobj as expected, producing an unbound slot for BDAY. but stmnt2 fails with bad syntax and i don't know why.

  3. i have also tried stmnt2 = "vobj.%s.value = ['%s']" % (k,v), making the value a list, because of the two alternatives on the VObject README:

     j.email.value = 'jeffrey@osafoundation.org'
     ...
     j.org.value = ['Open Source Applications Foundation']
    

    does it matter whether a string atom or list is used?

rikb
  • 630
  • 5
  • 18
  • can you include the code where you create the vcard? – wpercy Jan 16 '18 at 19:49
  • @wpercy thanks for your attention! i've generated the vcard lots of ways, eg, `vobj = vobject.vCard()`, via `vobject.readComponents(vcfStr)`... the `prettyPrint()` call seems to show it's well-formed? – rikb Jan 16 '18 at 21:09

0 Answers0