Add some useful properties to Collection

This commit is contained in:
Laurent Bachelier 2012-03-11 23:59:23 +01:00
commit f4dbefb6ef
4 changed files with 47 additions and 13 deletions

View file

@ -82,11 +82,11 @@ class CanalplusBrowser(BaseBrowser):
if len(split_path) == 0:
for channel in channels:
if len(channel.split_path) == 1:
if channel.level == 1:
yield channel
elif len(split_path) == 1:
for channel in channels:
if len(channel.split_path) == 2 and split_path[0] == channel.split_path[0]:
if channel.level == 2 and split_path == channel.parent:
yield channel
elif len(split_path) == 2:
subchannels = self.iter_resources(split_path[0:1])

View file

@ -106,15 +106,15 @@ class RedmineBackend(BaseBackend, ICapContent, ICapBugTracker, ICapCollection):
return self.iter_issues(query)
def validate_collection(self, objs, collection):
if len(collection.split_path) == 0:
if collection.level == 0:
return
if Issue in objs and len(collection.split_path) == 1:
if Issue in objs and collection.level == 1:
for project in self.iter_projects():
if collection.split_path[0] == project.id:
if collection.basename == project.id:
return Collection([project.id], project.name)
# if the project is not found by ID, try again by name
for project in self.iter_projects():
if collection.split_path[0] == project.name:
if collection.basename == project.name:
return Collection([project.id], project.name)
raise CollectionNotFound(collection.split_path)

View file

@ -41,7 +41,7 @@ class Collection(CapBaseObject):
def __init__(self, split_path, title=None, backend=None):
self.split_path = split_path
self.title = title
_id = split_path[-1] if len(split_path) else None
_id = self.basename
CapBaseObject.__init__(self, _id, backend)
def __unicode__(self):
@ -52,6 +52,18 @@ class Collection(CapBaseObject):
else:
return u'Unknown collection'
@property
def basename(self):
return self.split_path[-1] if self.level else None
@property
def parent(self):
return self.split_path[0:-1] if self.level else None
@property
def level(self):
return len(self.split_path)
class ICapCollection(IBaseCap):
def iter_resources_flat(self, objs, split_path, clean_only=False):
@ -93,7 +105,7 @@ class ICapCollection(IBaseCap):
You can replace the collection object entirely by returning a new one.
"""
# Root
if len(collection.split_path) == 0:
if collection.level == 0:
return
try:
i = self.iter_resources(objs, collection.split_path)
@ -104,3 +116,25 @@ class ICapCollection(IBaseCap):
def _restrict_level(self, split_path, lmax=0):
if len(split_path) > lmax:
raise CollectionNotFound(split_path)
def test():
c = Collection([])
assert c.basename is None
assert c.parent is None
assert c.level == 0
c = Collection([u'lol'])
assert c.basename == u'lol'
assert c.parent == []
assert c.level == 1
c = Collection([u'lol', u'cat'])
assert c.basename == u'cat'
assert c.parent == [u'lol']
assert c.level == 2
c = Collection([u'w', u'e', u'e', u'b', u'o', u'o', u'b'])
assert c.basename == u'b'
assert c.parent == [u'w', u'e', u'e', u'b', u'o', u'o']
assert c.level == 7

View file

@ -867,12 +867,12 @@ class ReplApplication(Cmd, ConsoleApplication):
print
print 'Collections:'
for collection in self.collections:
if collection.id and collection.title:
if collection.basename and collection.title:
print u'%s* (%s) %s (%s)%s' % \
(self.BOLD, collection.id, collection.title, collection.backend, self.NC)
elif collection.id:
(self.BOLD, collection.basename, collection.title, collection.backend, self.NC)
elif collection.basename:
print u'%s* (%s) (%s)%s' % \
(self.BOLD, collection.id, collection.backend, self.NC)
(self.BOLD, collection.basename, collection.backend, self.NC)
else:
print collection
@ -956,7 +956,7 @@ class ReplApplication(Cmd, ConsoleApplication):
self.bcall_error_handler(backend, error, backtrace)
for collection in self.collections:
directories.add(collection.id.encode(sys.stdout.encoding or locale.getpreferredencoding()))
directories.add(collection.basename.encode(sys.stdout.encoding or locale.getpreferredencoding()))
return [s[offs:] for s in directories if s.startswith(mline)]