pycarddav icon indicating copy to clipboard operation
pycarddav copied to clipboard

AttributeException in vcard_from_vobject

Open tribut opened this issue 12 years ago • 7 comments
trafficstars

Hi.

Using pycarddav 0.5.1 from pip I get the following exception trying to sync an owncloud 5.0.10 installation:

DEBUG:root:start syncing account owncloud
DEBUG:root:created version table
DEBUG:root:created accounts table
DEBUG:root:created owncloud table
DEBUG:root:getting /remote.php/carddav/addressbooks/foo/bar/141eb29ec8.vcf etag: "b4050229879ea875574ceecdf4b3a904"
[...]
DEBUG:root:getting /remote.php/carddav/addressbooks/foo/bar/056599fa37.vcf etag: "09caced6ccfd9cb1be41eb2ab332bc9a"
Traceback (most recent call last):
  File "/usr/local/bin/pycardsyncer", line 55, in <module>
    controllers.sync(conf)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/controllers.py", line 146, in sync
    my_dbtool.update(vcard, conf.account.name, href=href, etag=etag)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/backend.py", line 229, in update
    vcard = model.vcard_from_string(vcard)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/model.py", line 127, in vcard_from_string
    return vcard_from_vobject(vcard)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/model.py", line 101, in vcard_from_vobject
    line.transformFromNative()
  File "/usr/local/lib/python2.7/dist-packages/vobject/base.py", line 152, in transformFromNative
    return self.behavior.transformFromNative(self)
  File "/usr/local/lib/python2.7/dist-packages/vobject/vcard.py", line 298, in transformFromNative
    obj.value = serializeFields(obj.value)
  File "/usr/local/lib/python2.7/dist-packages/vobject/vcard.py", line 228, in serializeFields
    fields = [backslashEscape(val) for val in obj]
  File "/usr/local/lib/python2.7/dist-packages/vobject/base.py", line 1133, in backslashEscape
    s=s.replace("\\","\\\\").replace(";","\;").replace(",","\,")
vobject.base.NativeError: At line 7: In transformFromNative, unhandled exception: <type 'exceptions.AttributeError'>: 'list' object has no attribute 'replace'

I tried to re-add the except block removed in https://github.com/geier/pycarddav/commit/4176b09407971330d2da540e5b9a2c731bb6ae0d#L0L681 but only got another exception:

DEBUG:root:getting /remote.php/carddav/addressbooks/foo/bar/056599fa37.vcf etag: "09caced6ccfd9cb1be41eb2ab332bc9a"
Traceback (most recent call last):
  File "/usr/local/bin/pycardsyncer", line 55, in <module>
    controllers.sync(conf)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/controllers.py", line 146, in sync
    my_dbtool.update(vcard, conf.account.name, href=href, etag=etag)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/backend.py", line 229, in update
    vcard = model.vcard_from_string(vcard)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/model.py", line 131, in vcard_from_string
    return vcard_from_vobject(vcard)
  File "/usr/local/lib/python2.7/dist-packages/pycarddav/model.py", line 116, in vcard_from_vobject
    property_value = (',').join(property_value)
TypeError: sequence item 0: expected string, list found

I'm using Python 2.7.3 from Debian wheezy.

tribut avatar Sep 03 '13 11:09 tribut

looks like vobject can't handle that vcard. I guess I'll wrap that function call and print failing vcards, so we might know what the problem is

geier avatar Sep 03 '13 13:09 geier

could you check out pycarddav's develop branch, install it and try again? pycarddav should now print the vcard in case (and ignore it until next sync). You might want to paste the card (with all personal information redacted of course) or send it by email, so I can have a look at it.

geier avatar Sep 03 '13 14:09 geier

I took a deeper look at the problem and it was a comma in an Organization field. I don't know if it's legal or not in VCARD, but the field gets split by commas and then the seriealizer is not expecting a list instead of a string. I sent an e-mail to the vobject mailing list about it but is waiting to be approved.

llucax avatar Sep 16 '13 15:09 llucax

I believe ',' and ';' must be escaped with a backslash ''

It would be nice if vobject would be a more forgiving. But since there seems to be hardly any development going on at vobject, I might switch to an icalendar based solution (if I find the time), since python 3 support would be awesome (which is not yet in icalendar, but they are working on it).

geier avatar Sep 16 '13 15:09 geier

not correctly escaped ',' might be an issue with VCards set up with owncloud https://github.com/owncloud/contacts/issues/170

geier avatar Sep 17 '13 14:09 geier

In model.py at function vcard_from_string(vcard_string), when printing the vcard_string, this is what I get for a contact that has a comma in the organization:

BEGIN:VCARD
VERSION:3.0
UID:87452b8b1e
PRODID:-//ownCloud//NONSGML Contacts 0.2.5//EN
REV:2013-09-17T21:11:21+00:00
FN:Test Man
N:Man;Test;;;
ORG:Test, Org
END:VCARD

I don't know if this is an owncloud problem or vobject (vobject doesn't seem to behave as it should anyway, it should handle malformed data with a graceful error, if owncloud is really sending wrong data).

llucax avatar Sep 17 '13 21:09 llucax

the RFC specifies that commas MUST be escaped, but you are right, vobject should at least handle that error more graceful (perhaps it can be configured to do that, I'll have to look into this)

geier avatar Sep 18 '13 00:09 geier