Commit bfb36894 authored by Laurent Bachelier's avatar Laurent Bachelier 🐧

Add object type filtering to iter_resources

parent 63da39e0
......@@ -22,7 +22,7 @@ from __future__ import with_statement
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 @@ class CanalplusBackend(BaseBackend, ICapVideo, ICapCollection):
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)
......@@ -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 @@ class CanalPlusTest(BackendTest):
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)
......@@ -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
......
......@@ -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)
......@@ -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
......
......@@ -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)
......@@ -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
......
......@@ -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
......
......@@ -94,17 +94,18 @@ class RedmineBackend(BaseBackend, ICapContent, ICapBugTracker, ICapCollection):
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 ###################################################
......
......@@ -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:
......
......@@ -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')
......
......@@ -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)
......
......@@ -23,7 +23,7 @@ import subprocess
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
......
......@@ -74,11 +74,12 @@ class Transfer(CapBaseObject):
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()
......
......@@ -91,7 +91,7 @@ class ICapCollection(IBaseCap):
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.
......
......@@ -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 @@ class ReplApplication(Cmd, ConsoleApplication):
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 @@ class ReplApplication(Cmd, ConsoleApplication):
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 @@ class ReplApplication(Cmd, ConsoleApplication):
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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment