Use is_leaf and is_node instead of is_item and is_collection

This commit is contained in:
Guillaume Ayoub 2012-02-23 15:17:59 +01:00
parent 5e8dec6683
commit 128a20714b
3 changed files with 26 additions and 15 deletions

View File

@ -178,7 +178,7 @@ class Collection(object):
self.encoding = "utf-8" self.encoding = "utf-8"
split_path = path.split("/") split_path = path.split("/")
self.path = path if path != '.' else '' self.path = path if path != '.' else ''
if principal and split_path and self.is_collection(self.path): if principal and split_path and self.is_node(self.path):
# Already existing principal collection # Already existing principal collection
self.owner = split_path[0] self.owner = split_path[0]
elif len(split_path) > 1: elif len(split_path) > 1:
@ -189,13 +189,16 @@ class Collection(object):
self.is_principal = principal self.is_principal = principal
@classmethod @classmethod
def from_path(cls, path, depth="infinite", include_container=True): def from_path(cls, path, depth="1", include_container=True):
"""Return a list of collections and items under the given ``path``. """Return a list of collections and items under the given ``path``.
If ``depth`` is "0", only the actual object under ``path`` is If ``depth`` is "0", only the actual object under ``path`` is
returned. Otherwise, also sub-items are appended to the result. If returned.
``include_container`` is ``True`` (the default), the containing object
is included in the result. If ``depth`` is anything but "0", it is considered as "1" and direct
children are included in the result. If ``include_container`` is
``True`` (the default), the containing object is included in the
result.
The ``path`` is relative. The ``path`` is relative.
@ -205,14 +208,14 @@ class Collection(object):
attributes = sane_path.split("/") attributes = sane_path.split("/")
if not attributes: if not attributes:
return None return None
if not (cls.is_item("/".join(attributes)) or path.endswith("/")): if not (cls.is_leaf("/".join(attributes)) or path.endswith("/")):
attributes.pop() attributes.pop()
result = [] result = []
path = "/".join(attributes) path = "/".join(attributes)
principal = len(attributes) <= 1 principal = len(attributes) <= 1
if cls.is_collection(path): if cls.is_node(path):
if depth == "0": if depth == "0":
result.append(cls(path, principal)) result.append(cls(path, principal))
else: else:
@ -249,13 +252,21 @@ class Collection(object):
raise NotImplementedError raise NotImplementedError
@classmethod @classmethod
def is_collection(cls, path): def is_node(cls, path):
"""Return ``True`` if relative ``path`` is a collection.""" """Return ``True`` if relative ``path`` is a node.
A node is a WebDAV collection whose members are other collections.
"""
raise NotImplementedError raise NotImplementedError
@classmethod @classmethod
def is_item(cls, path): def is_leaf(cls, path):
"""Return ``True`` if relative ``path`` is a collection item.""" """Return ``True`` if relative ``path`` is a leaf.
A leaf is a WebDAV collection whose members are not collections.
"""
raise NotImplementedError raise NotImplementedError
@property @property

View File

@ -81,16 +81,16 @@ class Collection(ical.Collection):
_, directories, files = next(os.walk(abs_path)) _, directories, files = next(os.walk(abs_path))
for filename in directories + files: for filename in directories + files:
rel_filename = posixpath.join(path, filename) rel_filename = posixpath.join(path, filename)
if cls.is_collection(rel_filename) or cls.is_item(rel_filename): if cls.is_node(rel_filename) or cls.is_leaf(rel_filename):
yield cls(rel_filename) yield cls(rel_filename)
@classmethod @classmethod
def is_collection(cls, path): def is_node(cls, path):
abs_path = os.path.join(FOLDER, path.replace("/", os.sep)) abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
return os.path.isdir(abs_path) return os.path.isdir(abs_path)
@classmethod @classmethod
def is_item(cls, path): def is_leaf(cls, path):
abs_path = os.path.join(FOLDER, path.replace("/", os.sep)) abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
return os.path.isfile(abs_path) and not abs_path.endswith(".props") return os.path.isfile(abs_path) and not abs_path.endswith(".props")

View File

@ -279,7 +279,7 @@ def _propfind_response(path, item, props, user):
if item.is_principal: if item.is_principal:
tag = ET.Element(_tag("D", "principal")) tag = ET.Element(_tag("D", "principal"))
element.append(tag) element.append(tag)
if item.is_item(item.path): if item.is_leaf(item.path):
tag = ET.Element(_tag("C", item.resource_type)) tag = ET.Element(_tag("C", item.resource_type))
element.append(tag) element.append(tag)
tag = ET.Element(_tag("D", "collection")) tag = ET.Element(_tag("D", "collection"))