From f4dbefb6efdf63be42699dff2c8619e489a671be Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Sun, 11 Mar 2012 23:59:23 +0100 Subject: [PATCH] Add some useful properties to Collection --- modules/canalplus/browser.py | 4 ++-- modules/redmine/backend.py | 8 +++---- weboob/capabilities/collection.py | 38 +++++++++++++++++++++++++++++-- weboob/tools/application/repl.py | 10 ++++---- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/modules/canalplus/browser.py b/modules/canalplus/browser.py index be36f1c5..48f5c12d 100644 --- a/modules/canalplus/browser.py +++ b/modules/canalplus/browser.py @@ -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]) diff --git a/modules/redmine/backend.py b/modules/redmine/backend.py index e1576c51..ce9a84dc 100644 --- a/modules/redmine/backend.py +++ b/modules/redmine/backend.py @@ -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) diff --git a/weboob/capabilities/collection.py b/weboob/capabilities/collection.py index c8a93564..6ef8c112 100644 --- a/weboob/capabilities/collection.py +++ b/weboob/capabilities/collection.py @@ -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 diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 09789b98..d89185d8 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -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)]