Use a clean way to manage calendars and address books different serialization

This commit is contained in:
Guillaume Ayoub 2012-01-06 19:42:20 +01:00
parent 1dfa887384
commit a4a52c71d2

View File

@ -47,13 +47,17 @@ def open(path, mode="r"):
# pylint: enable=W0622 # pylint: enable=W0622
def serialize(tag, headers=(), items=()): def serialize(tag, headers=(), items=(), whole=False):
"""Return a collection text corresponding to given ``tag``. """Return a text corresponding to given collection ``tag``.
The collection has the given ``headers`` and ``items``. The text may have the given ``headers`` and ``items`` added around the
items if needed (ie. for calendars).
If ``whole`` is ``True``, the collection tags and headers are added, even
for address books.
""" """
if tag == "VCARD" or (tag == "VADDRESSBOOK" and items and len(items) == 1): if tag == "VADDRESSBOOK" and not whole:
lines = [items[0].text] lines = [items[0].text]
else: else:
lines = ["BEGIN:%s" % tag] lines = ["BEGIN:%s" % tag]
@ -382,7 +386,7 @@ class Collection(object):
self._create_dirs(self.path) self._create_dirs(self.path)
text = serialize(self.tag, headers, items) text = serialize(self.tag, headers, items, True)
return open(self.path, "w").write(text) return open(self.path, "w").write(text)
def set_mimetype(self, mimetype): def set_mimetype(self, mimetype):