Rename storage.Item.item to vobject_item

This commit is contained in:
Unrud 2018-08-21 18:43:47 +02:00
parent 75c1168f54
commit 2cb7060539
3 changed files with 38 additions and 31 deletions

View File

@ -819,20 +819,20 @@ class Application:
vobject_collection.add(component) vobject_collection.add(component)
item = storage.Item( item = storage.Item(
collection_path=collection_path, collection_path=collection_path,
item=vobject_collection) vobject_item=vobject_collection)
item.prepare() item.prepare()
items.append(item) items.append(item)
elif write_whole_collection and tag == "VADDRESSBOOK": elif write_whole_collection and tag == "VADDRESSBOOK":
for vobject_item in vobject_items: for vobject_item in vobject_items:
item = storage.Item( item = storage.Item(
collection_path=collection_path, collection_path=collection_path,
item=vobject_item) vobject_item=vobject_item)
item.prepare() item.prepare()
items.append(item) items.append(item)
elif not write_whole_collection: elif not write_whole_collection:
vobject_item, = vobject_items vobject_item, = vobject_items
item = storage.Item(collection_path=collection_path, item = storage.Item(collection_path=collection_path,
item=vobject_item) vobject_item=vobject_item)
item.prepare() item.prepare()
items.append(item) items.append(item)

View File

@ -381,9 +381,10 @@ class ComponentNotFoundError(ValueError):
class Item: class Item:
def __init__(self, collection_path=None, collection=None, item=None, def __init__(self, collection_path=None, collection=None,
href=None, last_modified=None, text=None, etag=None, uid=None, vobject_item=None, href=None, last_modified=None, text=None,
name=None, component_name=None, time_range=None): etag=None, uid=None, name=None, component_name=None,
time_range=None):
"""Initialize an item. """Initialize an item.
``collection_path`` the path of the parent collection (optional if ``collection_path`` the path of the parent collection (optional if
@ -395,10 +396,10 @@ class Item:
``last_modified`` the HTTP-datetime of when the item was modified. ``last_modified`` the HTTP-datetime of when the item was modified.
``text`` the text representation of the item (optional if ``item`` is ``text`` the text representation of the item (optional if
set). ``vobject_item`` is set).
``item`` the vobject item (optional if ``text`` is set). ``vobject_item`` the vobject item (optional if ``text`` is set).
``etag`` the etag of the item (optional). See ``get_etag``. ``etag`` the etag of the item (optional). See ``get_etag``.
@ -413,8 +414,9 @@ class Item:
See ``find_tag_and_time_range``. See ``find_tag_and_time_range``.
""" """
if text is None and item is None: if text is None and vobject_item is None:
raise ValueError("at least one of 'text' or 'item' must be set") raise ValueError(
"at least one of 'text' or 'vobject_item' must be set")
if collection_path is None: if collection_path is None:
if collection is None: if collection is None:
raise ValueError("at least one of 'collection_path' or " raise ValueError("at least one of 'collection_path' or "
@ -425,7 +427,7 @@ class Item:
self.href = href self.href = href
self.last_modified = last_modified self.last_modified = last_modified
self._text = text self._text = text
self._item = item self._vobject_item = vobject_item
self._etag = etag self._etag = etag
self._uid = uid self._uid = uid
self._name = name self._name = name
@ -435,7 +437,7 @@ class Item:
def serialize(self): def serialize(self):
if self._text is None: if self._text is None:
try: try:
self._text = self.item.serialize() self._text = self.vobject_item.serialize()
except Exception as e: except Exception as e:
raise RuntimeError("Failed to serialize item %r from %r: %s" % raise RuntimeError("Failed to serialize item %r from %r: %s" %
(self.href, self._collection_path, (self.href, self._collection_path,
@ -443,15 +445,15 @@ class Item:
return self._text return self._text
@property @property
def item(self): def vobject_item(self):
if self._item is None: if self._vobject_item is None:
try: try:
self._item = vobject.readOne(self._text) self._vobject_item = vobject.readOne(self._text)
except Exception as e: except Exception as e:
raise RuntimeError("Failed to parse item %r from %r: %s" % raise RuntimeError("Failed to parse item %r from %r: %s" %
(self.href, self._collection_path, (self.href, self._collection_path,
e)) from e e)) from e
return self._item return self._vobject_item
@property @property
def etag(self): def etag(self):
@ -463,38 +465,38 @@ class Item:
@property @property
def uid(self): def uid(self):
if self._uid is None: if self._uid is None:
self._uid = get_uid_from_object(self.item) self._uid = get_uid_from_object(self.vobject_item)
return self._uid return self._uid
@property @property
def name(self): def name(self):
if self._name is None: if self._name is None:
self._name = self.item.name or "" self._name = self.vobject_item.name or ""
return self._name return self._name
@property @property
def component_name(self): def component_name(self):
if self._component_name is not None: if self._component_name is not None:
return self._component_name return self._component_name
return xmlutils.find_tag(self.item) return xmlutils.find_tag(self.vobject_item)
@property @property
def time_range(self): def time_range(self):
if self._time_range is None: if self._time_range is None:
self._component_name, *self._time_range = ( self._component_name, *self._time_range = (
xmlutils.find_tag_and_time_range(self.item)) xmlutils.find_tag_and_time_range(self.vobject_item))
return self._time_range return self._time_range
def prepare(self): def prepare(self):
"""Fill cache with values.""" """Fill cache with values."""
orig_item = self._item orig_vobject_item = self._vobject_item
self.serialize() self.serialize()
self.etag self.etag
self.uid self.uid
self.name self.name
self.time_range self.time_range
self.component_name self.component_name
self._item = orig_item self._vobject_item = orig_vobject_item
class BaseCollection: class BaseCollection:
@ -1414,7 +1416,8 @@ class Collection(BaseCollection):
check_and_sanitize_items(vobject_items, check_and_sanitize_items(vobject_items,
tag=self.get_meta("tag")) tag=self.get_meta("tag"))
vobject_item, = vobject_items vobject_item, = vobject_items
temp_item = Item(collection=self, item=vobject_item) temp_item = Item(collection=self,
vobject_item=vobject_item)
cache_hash, uid, etag, text, name, tag, start, end = \ cache_hash, uid, etag, text, name, tag, start, end = \
self._store_item_cache( self._store_item_cache(
href, temp_item, input_hash) href, temp_item, input_hash)

View File

@ -194,15 +194,16 @@ def _comp_match(item, filter_, level=0):
logger.warning("Filtering %s is not supported" % name) logger.warning("Filtering %s is not supported" % name)
return True return True
# Point #3 and #4 of rfc4791-9.7.1 # Point #3 and #4 of rfc4791-9.7.1
components = ([item.item] if level == 0 components = ([item.vobject_item] if level == 0
else list(getattr(item.item, "%s_list" % tag.lower()))) else list(getattr(item.vobject_item,
"%s_list" % tag.lower())))
for child in filter_: for child in filter_:
if child.tag == _tag("C", "prop-filter"): if child.tag == _tag("C", "prop-filter"):
if not any(_prop_match(comp, child, "C") if not any(_prop_match(comp, child, "C")
for comp in components): for comp in components):
return False return False
elif child.tag == _tag("C", "time-range"): elif child.tag == _tag("C", "time-range"):
if not _time_range_match(item.item, filter_[0], tag): if not _time_range_match(item.vobject_item, filter_[0], tag):
return False return False
elif child.tag == _tag("C", "comp-filter"): elif child.tag == _tag("C", "comp-filter"):
if not _comp_match(item, child, level=level + 1): if not _comp_match(item, child, level=level + 1):
@ -1253,15 +1254,18 @@ def report(base_prefix, path, xml_request, collection, unlock_storage_fn):
raise ValueError("Unexpected %r in filter" % child.tag) raise ValueError("Unexpected %r in filter" % child.tag)
test = filter_.get("test", "anyof") test = filter_.get("test", "anyof")
if test == "anyof": if test == "anyof":
return any(_prop_match(item.item, f, "CR") for f in filter_) return any(_prop_match(item.vobject_item, f, "CR")
for f in filter_)
if test == "allof": if test == "allof":
return all(_prop_match(item.item, f, "CR") for f in filter_) return all(_prop_match(item.vobject_item, f, "CR")
for f in filter_)
raise ValueError("Unsupported filter test: %r" % test) raise ValueError("Unsupported filter test: %r" % test)
return all(_prop_match(item.item, f, "CR") for f in filter_) return all(_prop_match(item.vobject_item, f, "CR")
for f in filter_)
raise ValueError("unsupported filter %r for %r" % (filter_.tag, tag)) raise ValueError("unsupported filter %r for %r" % (filter_.tag, tag))
while retrieved_items: while retrieved_items:
# ``item.item`` might be accessed during filtering. # ``item.vobject_item`` might be accessed during filtering.
# Don't keep reference to ``item``, because VObject requires a lot of # Don't keep reference to ``item``, because VObject requires a lot of
# memory. # memory.
item, filters_matched = retrieved_items.pop(0) item, filters_matched = retrieved_items.pop(0)