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)
item = storage.Item(
collection_path=collection_path,
item=vobject_collection)
vobject_item=vobject_collection)
item.prepare()
items.append(item)
elif write_whole_collection and tag == "VADDRESSBOOK":
for vobject_item in vobject_items:
item = storage.Item(
collection_path=collection_path,
item=vobject_item)
vobject_item=vobject_item)
item.prepare()
items.append(item)
elif not write_whole_collection:
vobject_item, = vobject_items
item = storage.Item(collection_path=collection_path,
item=vobject_item)
vobject_item=vobject_item)
item.prepare()
items.append(item)

View File

@ -381,9 +381,10 @@ class ComponentNotFoundError(ValueError):
class Item:
def __init__(self, collection_path=None, collection=None, item=None,
href=None, last_modified=None, text=None, etag=None, uid=None,
name=None, component_name=None, time_range=None):
def __init__(self, collection_path=None, collection=None,
vobject_item=None, href=None, last_modified=None, text=None,
etag=None, uid=None, name=None, component_name=None,
time_range=None):
"""Initialize an item.
``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.
``text`` the text representation of the item (optional if ``item`` is
set).
``text`` the text representation of the item (optional if
``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``.
@ -413,8 +414,9 @@ class Item:
See ``find_tag_and_time_range``.
"""
if text is None and item is None:
raise ValueError("at least one of 'text' or 'item' must be set")
if text is None and vobject_item is None:
raise ValueError(
"at least one of 'text' or 'vobject_item' must be set")
if collection_path is None:
if collection is None:
raise ValueError("at least one of 'collection_path' or "
@ -425,7 +427,7 @@ class Item:
self.href = href
self.last_modified = last_modified
self._text = text
self._item = item
self._vobject_item = vobject_item
self._etag = etag
self._uid = uid
self._name = name
@ -435,7 +437,7 @@ class Item:
def serialize(self):
if self._text is None:
try:
self._text = self.item.serialize()
self._text = self.vobject_item.serialize()
except Exception as e:
raise RuntimeError("Failed to serialize item %r from %r: %s" %
(self.href, self._collection_path,
@ -443,15 +445,15 @@ class Item:
return self._text
@property
def item(self):
if self._item is None:
def vobject_item(self):
if self._vobject_item is None:
try:
self._item = vobject.readOne(self._text)
self._vobject_item = vobject.readOne(self._text)
except Exception as e:
raise RuntimeError("Failed to parse item %r from %r: %s" %
(self.href, self._collection_path,
e)) from e
return self._item
return self._vobject_item
@property
def etag(self):
@ -463,38 +465,38 @@ class Item:
@property
def uid(self):
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
@property
def name(self):
if self._name is None:
self._name = self.item.name or ""
self._name = self.vobject_item.name or ""
return self._name
@property
def component_name(self):
if self._component_name is not None:
return self._component_name
return xmlutils.find_tag(self.item)
return xmlutils.find_tag(self.vobject_item)
@property
def time_range(self):
if self._time_range is None:
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
def prepare(self):
"""Fill cache with values."""
orig_item = self._item
orig_vobject_item = self._vobject_item
self.serialize()
self.etag
self.uid
self.name
self.time_range
self.component_name
self._item = orig_item
self._vobject_item = orig_vobject_item
class BaseCollection:
@ -1414,7 +1416,8 @@ class Collection(BaseCollection):
check_and_sanitize_items(vobject_items,
tag=self.get_meta("tag"))
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 = \
self._store_item_cache(
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)
return True
# Point #3 and #4 of rfc4791-9.7.1
components = ([item.item] if level == 0
else list(getattr(item.item, "%s_list" % tag.lower())))
components = ([item.vobject_item] if level == 0
else list(getattr(item.vobject_item,
"%s_list" % tag.lower())))
for child in filter_:
if child.tag == _tag("C", "prop-filter"):
if not any(_prop_match(comp, child, "C")
for comp in components):
return False
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
elif child.tag == _tag("C", "comp-filter"):
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)
test = filter_.get("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":
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)
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))
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
# memory.
item, filters_matched = retrieved_items.pop(0)