From bfb3689456ee3bc9cca921c640c744878d9610ae Mon Sep 17 00:00:00 2001 From: Laurent Bachelier Date: Sun, 5 Feb 2012 13:30:02 +0100 Subject: [PATCH] Add object type filtering to iter_resources --- modules/canalplus/backend.py | 9 +++--- modules/canalplus/test.py | 8 +++++ modules/nova/backend.py | 13 +++++---- modules/nova/test.py | 3 +- modules/ouifm/backend.py | 13 +++++---- modules/ouifm/test.py | 4 ++- modules/radiofrance/backend.py | 29 ++++++++++--------- modules/radiofrance/test.py | 10 +++++-- modules/redmine/backend.py | 23 ++++++++------- weboob/applications/boobank/boobank.py | 3 +- .../applications/boobtracker/boobtracker.py | 3 +- weboob/applications/radioob/radioob.py | 3 +- weboob/applications/videoob/videoob.py | 3 +- weboob/capabilities/bank.py | 9 +++--- weboob/capabilities/collection.py | 2 +- weboob/tools/application/repl.py | 18 +++++++----- 16 files changed, 92 insertions(+), 61 deletions(-) diff --git a/modules/canalplus/backend.py b/modules/canalplus/backend.py index 18e7a2c8c1..fdcc9ec9b7 100644 --- a/modules/canalplus/backend.py +++ b/modules/canalplus/backend.py @@ -22,7 +22,7 @@ import re -from weboob.capabilities.video import ICapVideo +from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.tools.backend import BaseBackend, BackendConfig from weboob.tools.value import Value @@ -71,6 +71,7 @@ def fill_video(self, video, fields): OBJECTS = {CanalplusVideo: fill_video} - def iter_resources(self, split_path): - with self.browser: - return self.browser.iter_resources(split_path) + def iter_resources(self, objs, split_path): + if BaseVideo in objs: + with self.browser: + return self.browser.iter_resources(split_path) diff --git a/modules/canalplus/test.py b/modules/canalplus/test.py index 507de9f377..09fdb0fe80 100644 --- a/modules/canalplus/test.py +++ b/modules/canalplus/test.py @@ -19,6 +19,7 @@ from weboob.tools.test import BackendTest +from weboob.capabilities.video import BaseVideo class CanalPlusTest(BackendTest): BACKEND = 'canalplus' @@ -29,3 +30,10 @@ def test_canalplus(self): v = l[0] self.backend.fillobj(v, ('url',)) self.assertTrue(v.url and v.url.startswith('rtmp://'), 'URL for video "%s" not found: %s' % (v.id, v.url)) + + def test_ls(self): + l = list(self.backend.iter_resources((BaseVideo, ), [])) + self.assertTrue(len(l) > 0) + + l = list(self.backend.iter_resources((BaseVideo, ), ['SPORT'])) + self.assertTrue(len(l) > 0) diff --git a/modules/nova/backend.py b/modules/nova/backend.py index 50892aca75..22603d8dfc 100644 --- a/modules/nova/backend.py +++ b/modules/nova/backend.py @@ -62,15 +62,16 @@ class NovaBackend(BaseBackend, ICapRadio, ICapCollection): _RADIOS = {'nova': (u'Radio Nova', u'Radio nova', u'http://broadcast.infomaniak.net:80/radionova-high.mp3'), } - def iter_resources(self, split_path): - if len(split_path) > 0: - raise CollectionNotFound(split_path) + def iter_resources(self, objs, split_path): + if Radio in objs: + if len(split_path) > 0: + raise CollectionNotFound(split_path) - for id in self._RADIOS.iterkeys(): - yield self.get_radio(id) + for id in self._RADIOS.iterkeys(): + yield self.get_radio(id) def iter_radios_search(self, pattern): - for radio in self.iter_resources([]): + for radio in self.iter_resources((Radio, ), []): if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower(): yield radio diff --git a/modules/nova/test.py b/modules/nova/test.py index 1f33fe9585..69c939820b 100644 --- a/modules/nova/test.py +++ b/modules/nova/test.py @@ -19,10 +19,11 @@ from weboob.tools.test import BackendTest +from weboob.capabilities.radio import Radio class NovaTest(BackendTest): BACKEND = 'nova' def test_nova(self): - l = list(self.backend.iter_resources([])) + l = list(self.backend.iter_resources((Radio, ), [])) self.assertTrue(len(l) > 0) diff --git a/modules/ouifm/backend.py b/modules/ouifm/backend.py index 64a6b392e4..2f13a4eeaa 100644 --- a/modules/ouifm/backend.py +++ b/modules/ouifm/backend.py @@ -46,15 +46,16 @@ class OuiFMBackend(BaseBackend, ICapRadio, ICapCollection): def create_default_browser(self): return self.create_browser(parser='json') - def iter_resources(self, split_path): - if len(split_path) > 0: - raise CollectionNotFound(split_path) + def iter_resources(self, objs, split_path): + if Radio in objs: + if len(split_path) > 0: + raise CollectionNotFound(split_path) - for id in self._RADIOS.iterkeys(): - yield self.get_radio(id) + for id in self._RADIOS.iterkeys(): + yield self.get_radio(id) def iter_radios_search(self, pattern): - for radio in self.iter_resources([]): + for radio in self.iter_resources((Radio, ), []): if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower(): yield radio diff --git a/modules/ouifm/test.py b/modules/ouifm/test.py index b6a21451d9..fc5730876b 100644 --- a/modules/ouifm/test.py +++ b/modules/ouifm/test.py @@ -19,10 +19,12 @@ from weboob.tools.test import BackendTest +from weboob.capabilities.radio import Radio + class OuiFMTest(BackendTest): BACKEND = 'ouifm' def test_ouifm(self): - l = list(self.backend.iter_resources([])) + l = list(self.backend.iter_resources((Radio, ), [])) self.assertTrue(len(l) > 0) diff --git a/modules/radiofrance/backend.py b/modules/radiofrance/backend.py index ee3885fcb7..06297060e2 100644 --- a/modules/radiofrance/backend.py +++ b/modules/radiofrance/backend.py @@ -102,22 +102,23 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection, ICapVideo): _RSS_RADIOS = ('francemusique', ) _ANTENNA_RADIOS = ('fip', ) - def iter_resources(self, split_path): - if len(split_path) == 1 and split_path[0] == 'francebleu': - for _id in sorted(self._RADIOS.iterkeys()): - if _id.startswith('fb'): - yield self.get_radio(_id) - elif len(split_path) == 0: - for _id in sorted(self._RADIOS.iterkeys()): - if not _id.startswith('fb'): - yield self.get_radio(_id) - yield Collection('francebleu', 'France Bleu', - children=self.iter_resources(['francebleu'])) - else: - raise CollectionNotFound(split_path) + def iter_resources(self, objs, split_path): + if Radio in objs: + if len(split_path) == 1 and split_path[0] == 'francebleu': + for _id in sorted(self._RADIOS.iterkeys()): + if _id.startswith('fb'): + yield self.get_radio(_id) + elif len(split_path) == 0: + for _id in sorted(self._RADIOS.iterkeys()): + if not _id.startswith('fb'): + yield self.get_radio(_id) + yield Collection('francebleu', 'France Bleu', + children=self.iter_resources(objs, ['francebleu'])) + else: + raise CollectionNotFound(split_path) def iter_radios_search(self, pattern): - for radio in self._flatten_resources(self.iter_resources([])): + for radio in self._flatten_resources(self.iter_resources((Radio, ), [])): if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower(): yield radio diff --git a/modules/radiofrance/test.py b/modules/radiofrance/test.py index 0b847a246c..18bfdd7e6d 100644 --- a/modules/radiofrance/test.py +++ b/modules/radiofrance/test.py @@ -19,14 +19,20 @@ from weboob.tools.test import BackendTest +from weboob.capabilities.video import BaseVideo +from weboob.capabilities.radio import Radio class RadioFranceTest(BackendTest): BACKEND = 'radiofrance' def test_get_radios(self): - l = list(self.backend.iter_resources([])) - self.assertTrue(len(l) > 0) + l = list(self.backend.iter_resources(objs=[Radio], split_path=[])) + self.assertTrue(0 < len(l) < 30) + l = list(self.backend.iter_resources(objs=[Radio], split_path=['francebleu'])) + self.assertTrue(len(l) > 30) + l = list(self.backend.iter_resources(objs=[BaseVideo], split_path=[])) + self.assertEquals(len(l), 0) def test_get_video(self): # this should be available up to 24/10/2014 15h00 diff --git a/modules/redmine/backend.py b/modules/redmine/backend.py index e475e7d37c..1fe37def39 100644 --- a/modules/redmine/backend.py +++ b/modules/redmine/backend.py @@ -94,17 +94,18 @@ def get_content_preview(self, content): return self.browser.get_wiki_preview(project, page, content.content) ############# CapCollection ################################################### - def iter_resources(self, split_path): - if len(split_path) == 0: - return [Collection(project.id, project.name, fct=self.iter_issues) - for project in self.iter_projects()] - - if len(split_path) == 1: - query = Query() - query.project = unicode(split_path[0]) - return self.iter_issues(query) - - raise CollectionNotFound(split_path) + def iter_resources(self, objs, split_path): + if Project in objs or Issue in objs: + if len(split_path) == 0: + return [Collection(project.id, project.name, fct=self.iter_issues) + for project in self.iter_projects()] + + if len(split_path) == 1: + query = Query() + query.project = unicode(split_path[0]) + return self.iter_issues(query) + + raise CollectionNotFound(split_path) ############# CapBugTracker ################################################### diff --git a/weboob/applications/boobank/boobank.py b/weboob/applications/boobank/boobank.py index 139fd212f6..a043234d15 100644 --- a/weboob/applications/boobank/boobank.py +++ b/weboob/applications/boobank/boobank.py @@ -20,7 +20,7 @@ import sys -from weboob.capabilities.bank import ICapBank +from weboob.capabilities.bank import ICapBank, Account, Operation from weboob.tools.application.repl import ReplApplication from weboob.tools.application.formatters.iformatter import IFormatter @@ -142,6 +142,7 @@ class Boobank(ReplApplication): 'list': 'account_list', 'transfer': 'transfer', } + COLLECTION_OBJECTS = (Account, Operation, ) def _complete_account(self, exclude=None): if exclude: diff --git a/weboob/applications/boobtracker/boobtracker.py b/weboob/applications/boobtracker/boobtracker.py index 734aa6ad9b..26ed158fec 100644 --- a/weboob/applications/boobtracker/boobtracker.py +++ b/weboob/applications/boobtracker/boobtracker.py @@ -21,7 +21,7 @@ from datetime import timedelta import sys -from weboob.capabilities.bugtracker import ICapBugTracker, Query, Update +from weboob.capabilities.bugtracker import ICapBugTracker, Query, Update, Project, Issue from weboob.tools.application.repl import ReplApplication from weboob.tools.application.formatters.iformatter import IFormatter from weboob.tools.misc import html2text @@ -93,6 +93,7 @@ class BoobTracker(ReplApplication): 'search': 'issues_list', 'ls': 'issues_list', } + COLLECTION_OBJECTS = (Project, Issue, ) def add_application_options(self, group): group.add_option('--author') diff --git a/weboob/applications/radioob/radioob.py b/weboob/applications/radioob/radioob.py index 87be803657..cf0d65f423 100644 --- a/weboob/applications/radioob/radioob.py +++ b/weboob/applications/radioob/radioob.py @@ -20,7 +20,7 @@ import sys -from weboob.capabilities.radio import ICapRadio +from weboob.capabilities.radio import ICapRadio, Radio from weboob.capabilities.base import NotLoaded from weboob.tools.application.repl import ReplApplication from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound @@ -66,6 +66,7 @@ class Radioob(ReplApplication): COMMANDS_FORMATTERS = {'ls': 'radio_list', 'search': 'radio_list', } + COLLECTION_OBJECTS = (Radio, ) def __init__(self, *args, **kwargs): ReplApplication.__init__(self, *args, **kwargs) diff --git a/weboob/applications/videoob/videoob.py b/weboob/applications/videoob/videoob.py index a7cd08edb4..4ceb70b83a 100644 --- a/weboob/applications/videoob/videoob.py +++ b/weboob/applications/videoob/videoob.py @@ -23,7 +23,7 @@ import sys import os -from weboob.capabilities.video import ICapVideo +from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.base import NotLoaded from weboob.tools.application.repl import ReplApplication from weboob.tools.application.media_player import InvalidMediaPlayer, MediaPlayer, MediaPlayerNotFound @@ -66,6 +66,7 @@ class Videoob(ReplApplication): EXTRA_FORMATTERS = {'video_list': VideoListFormatter} COMMANDS_FORMATTERS = {'search': 'video_list', 'ls': 'video_list'} + COLLECTION_OBJECTS = (BaseVideo, ) nsfw = True diff --git a/weboob/capabilities/bank.py b/weboob/capabilities/bank.py index cfdc08a2a3..df3362ab93 100644 --- a/weboob/capabilities/bank.py +++ b/weboob/capabilities/bank.py @@ -74,11 +74,12 @@ def __init__(self, id): self.add_field('recipient', (int, long, basestring)) class ICapBank(ICapCollection): - def iter_resources(self, split_path): - if len(split_path) > 0: - raise CollectionNotFound(split_path) + def iter_resources(self, objs, split_path): + if Account in objs: + if len(split_path) > 0: + raise CollectionNotFound(split_path) - return self.iter_accounts() + return self.iter_accounts() def iter_accounts(self): raise NotImplementedError() diff --git a/weboob/capabilities/collection.py b/weboob/capabilities/collection.py index fb398e37e5..3138470d3b 100644 --- a/weboob/capabilities/collection.py +++ b/weboob/capabilities/collection.py @@ -91,7 +91,7 @@ def _flatten_resources(self, resources, clean_only=False): lst.append(resource) return lst - def iter_resources(self, split_path): + def iter_resources(self, objs, split_path): """ split_path is a list, either empty (root path) or with one or many components. diff --git a/weboob/tools/application/repl.py b/weboob/tools/application/repl.py index 913a7d7f97..e26e1d70e2 100644 --- a/weboob/tools/application/repl.py +++ b/weboob/tools/application/repl.py @@ -80,6 +80,9 @@ class ReplApplication(Cmd, ConsoleApplication): DEFAULT_FORMATTER = 'multiline' COMMANDS_FORMATTERS = {} + # Objects to allow in do_ls / do_cd + COLLECTION_OBJECTS = tuple() + weboob_commands = set(['backends', 'condition', 'count', 'formatter', 'inspect', 'logging', 'select', 'quit', 'ls', 'cd']) hidden_commands = set(['EOF']) @@ -849,7 +852,7 @@ def do_ls(self, line): List objects in current path. """ - self.objects = self._fetch_objects() + self.objects = self._fetch_objects(objs=self.COLLECTION_OBJECTS) for obj in self.objects: if isinstance(obj, CapBaseObject): @@ -879,21 +882,22 @@ def do_cd(self, line): else: self.working_path.extend(line) - objects = self._fetch_objects() + objects = self._fetch_objects(objs=self.COLLECTION_OBJECTS) if len(objects) == 0: print >>sys.stderr, "Path: %s not found" % self.working_path.tostring() self.working_path.restore() return 1 - self.objects = objects self._change_prompt() - def _fetch_objects(self): + def _fetch_objects(self, objs): objects = [] - path = self.working_path.get() + split_path = self.working_path.get() try: - for backend, res in self.do('iter_resources', path, caps=ICapCollection): + for backend, res in self.do('iter_resources', + objs=objs, split_path=split_path, + caps=ICapCollection): objects.append(res) except CallErrors, errors: for backend, error, backtrace in errors.errors: @@ -910,7 +914,7 @@ def complete_cd(self, text, line, begidx, endidx): offs = len(mline) - len(text) if len(self.objects) == 0: - self.objects = self._fetch_objects() + self.objects = self._fetch_objects(objs=self.COLLECTION_OBJECTS) for obj in self.objects: if isinstance(obj, Collection): -- GitLab