Use is_leaf and is_node instead of is_item and is_collection
This commit is contained in:
parent
5e8dec6683
commit
128a20714b
@ -178,7 +178,7 @@ class Collection(object):
|
||||
self.encoding = "utf-8"
|
||||
split_path = path.split("/")
|
||||
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
|
||||
self.owner = split_path[0]
|
||||
elif len(split_path) > 1:
|
||||
@ -189,13 +189,16 @@ class Collection(object):
|
||||
self.is_principal = principal
|
||||
|
||||
@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``.
|
||||
|
||||
If ``depth`` is "0", only the actual object under ``path`` is
|
||||
returned. Otherwise, also sub-items are appended to the result. If
|
||||
``include_container`` is ``True`` (the default), the containing object
|
||||
is included in the result.
|
||||
returned.
|
||||
|
||||
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.
|
||||
|
||||
@ -205,14 +208,14 @@ class Collection(object):
|
||||
attributes = sane_path.split("/")
|
||||
if not attributes:
|
||||
return None
|
||||
if not (cls.is_item("/".join(attributes)) or path.endswith("/")):
|
||||
if not (cls.is_leaf("/".join(attributes)) or path.endswith("/")):
|
||||
attributes.pop()
|
||||
|
||||
result = []
|
||||
path = "/".join(attributes)
|
||||
|
||||
principal = len(attributes) <= 1
|
||||
if cls.is_collection(path):
|
||||
if cls.is_node(path):
|
||||
if depth == "0":
|
||||
result.append(cls(path, principal))
|
||||
else:
|
||||
@ -249,13 +252,21 @@ class Collection(object):
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def is_collection(cls, path):
|
||||
"""Return ``True`` if relative ``path`` is a collection."""
|
||||
def is_node(cls, path):
|
||||
"""Return ``True`` if relative ``path`` is a node.
|
||||
|
||||
A node is a WebDAV collection whose members are other collections.
|
||||
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@classmethod
|
||||
def is_item(cls, path):
|
||||
"""Return ``True`` if relative ``path`` is a collection item."""
|
||||
def is_leaf(cls, path):
|
||||
"""Return ``True`` if relative ``path`` is a leaf.
|
||||
|
||||
A leaf is a WebDAV collection whose members are not collections.
|
||||
|
||||
"""
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
|
@ -81,16 +81,16 @@ class Collection(ical.Collection):
|
||||
_, directories, files = next(os.walk(abs_path))
|
||||
for filename in directories + files:
|
||||
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)
|
||||
|
||||
@classmethod
|
||||
def is_collection(cls, path):
|
||||
def is_node(cls, path):
|
||||
abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
|
||||
return os.path.isdir(abs_path)
|
||||
|
||||
@classmethod
|
||||
def is_item(cls, path):
|
||||
def is_leaf(cls, path):
|
||||
abs_path = os.path.join(FOLDER, path.replace("/", os.sep))
|
||||
return os.path.isfile(abs_path) and not abs_path.endswith(".props")
|
||||
|
||||
|
@ -279,7 +279,7 @@ def _propfind_response(path, item, props, user):
|
||||
if item.is_principal:
|
||||
tag = ET.Element(_tag("D", "principal"))
|
||||
element.append(tag)
|
||||
if item.is_item(item.path):
|
||||
if item.is_leaf(item.path):
|
||||
tag = ET.Element(_tag("C", item.resource_type))
|
||||
element.append(tag)
|
||||
tag = ET.Element(_tag("D", "collection"))
|
||||
|
Loading…
Reference in New Issue
Block a user