Clean some ical methods

This commit is contained in:
Guillaume Ayoub 2015-02-07 15:39:57 +01:00
parent 38a82b64c1
commit 99575d69ca
2 changed files with 27 additions and 41 deletions

View File

@ -193,7 +193,8 @@ class Collection(object):
else: else:
self.owner = None self.owner = None
self.is_principal = principal self.is_principal = principal
self._items = self._parse(self.text, (Event, Todo, Journal, Card, Timezone)) self._items = self._parse(
self.text, (Event, Todo, Journal, Card, Timezone))
@classmethod @classmethod
def from_path(cls, path, depth="1", include_container=True): def from_path(cls, path, depth="1", include_container=True):
@ -345,8 +346,8 @@ class Collection(object):
return items return items
def get_item(self, name): def get_item(self, name):
"""Get collection item called ``name``.""" """Get item named ``name`` from collection."""
raise NotImplementedError return self._items.get(name)
def append(self, name, text): def append(self, name, text):
"""Append items from ``text`` to collection. """Append items from ``text`` to collection.
@ -354,37 +355,27 @@ class Collection(object):
If ``name`` is given, give this name to new items in ``text``. If ``name`` is given, give this name to new items in ``text``.
""" """
items = self.items.values() new_items = self._parse(
text, (Timezone, Event, Todo, Journal, Card), name)
for new_item in self._parse( for new_item in new_items.values():
text, (Timezone, Event, Todo, Journal, Card), name): if new_item.name not in self._items:
if new_item.name not in (item.name for item in items): self._items[new_item] = new_item
items.append(new_item) self.write()
self.write(items=items)
def remove(self, name): def remove(self, name):
"""Remove object named ``name`` from collection.""" """Remove object named ``name`` from collection."""
components = [ if name in self._items:
component for component in self.components del self._items[name]
if component.name != name] self.write()
items = self.timezones + components
self.write(items=items)
def replace(self, name, text): def replace(self, name, text):
"""Replace content by ``text`` in collection objet called ``name``.""" """Replace content by ``text`` in collection objet called ``name``."""
self.remove(name) self.remove(name)
self.append(name, text) self.append(name, text)
def write(self, headers=None, items=None): def write(self):
"""Write collection with given parameters.""" """Write collection with given parameters."""
headers = headers or self.headers or ( text = serialize(self.tag, self.headers, self.items)
Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
Header("VERSION:%s" % self.version))
items = items if items is not None else self.items.values()
text = serialize(self.tag, headers, items)
self.save(text) self.save(text)
def set_mimetype(self, mimetype): def set_mimetype(self, mimetype):
@ -463,51 +454,48 @@ class Collection(object):
break break
header_lines.append(Header(line)) header_lines.append(Header(line))
return header_lines return header_lines or (
Header("PRODID:-//Radicale//NONSGML Radicale Server//EN"),
Header("VERSION:%s" % self.version))
@staticmethod def filter_items(self, *item_types):
def _filter_items(items, item_type):
return [item for item in items if item.tag == item_type.tag]
@staticmethod
def _filter_items_many(items, item_types):
tags = [item_type.tag for item_type in item_types] tags = [item_type.tag for item_type in item_types]
return [item for item in items if item.tag in tags] return [item for item in self.items if item.tag in tags]
@property @property
def items(self): def items(self):
"""Get list of all items in collection.""" """Get list of all items in collection."""
return self._items.values() return list(self._items.values())
@property @property
def components(self): def components(self):
"""Get list of all components in collection.""" """Get list of all components in collection."""
return self._filter_items_many(self.items, (Event, Todo, Journal, Card)) return self.filter_items(Event, Todo, Journal, Card)
@property @property
def events(self): def events(self):
"""Get list of ``Event`` items in calendar.""" """Get list of ``Event`` items in calendar."""
return self._filter_items(self.items, Event) return self.filter_items(Event)
@property @property
def todos(self): def todos(self):
"""Get list of ``Todo`` items in calendar.""" """Get list of ``Todo`` items in calendar."""
return self._filter_items(self.items, Todo) return self.filter_items(Todo)
@property @property
def journals(self): def journals(self):
"""Get list of ``Journal`` items in calendar.""" """Get list of ``Journal`` items in calendar."""
return self._filter_items(self.items, Journal) return self.filter_items(Journal)
@property @property
def timezones(self): def timezones(self):
"""Get list of ``Timezone`` items in calendar.""" """Get list of ``Timezone`` items in calendar."""
return self._filter_items(self.items, Timezone) return self.filter_items(Timezone)
@property @property
def cards(self): def cards(self):
"""Get list of ``Card`` items in address book.""" """Get list of ``Card`` items in address book."""
return self._filter_items(self.items, Card) return self.filter_items(Card)
@property @property
def owner_url(self): def owner_url(self):

View File

@ -117,8 +117,6 @@ class Collection(ical.Collection):
modification_time = time.gmtime(os.path.getmtime(self._path)) modification_time = time.gmtime(os.path.getmtime(self._path))
return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time) return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time)
def get_item(self, name):
return self._items.get(name)
@property @property
@contextmanager @contextmanager
def props(self): def props(self):