use CapResources for radios

This commit is contained in:
Romain Bignon 2011-04-21 12:07:37 +02:00
commit 7098e44d9b
8 changed files with 60 additions and 49 deletions

View file

@ -63,9 +63,9 @@ class Radioob(ReplApplication):
'like the current song.'
CAPS = ICapRadio
EXTRA_FORMATTERS = {'radio_list': RadioListFormatter}
COMMANDS_FORMATTERS = {'list': 'radio_list'}
radios = []
COMMANDS_FORMATTERS = {'ls': 'radio_list',
'search': 'radio_list',
}
def __init__(self, *args, **kwargs):
ReplApplication.__init__(self, *args, **kwargs)
@ -75,29 +75,10 @@ class Radioob(ReplApplication):
self.load_config()
return ReplApplication.main(self, argv)
def _get_radio(self, _id, fields=None):
if self.interactive:
try:
radio = self.radios[int(_id) - 1]
except (IndexError,ValueError):
pass
else:
for backend, radio in self.do('fillobj', radio, fields, backends=[radio.backend]):
if radio:
return radio
_id, backend_name = self.parse_id(_id)
backend_names = (backend_name,) if backend_name is not None else self.enabled_backends
for backend, radio in self.do('get_radio', _id, backends=backend_names):
if radio:
return radio
def _complete_id(self):
return ['%s@%s' % (radio.id, radio.backend) for radio in self.radios]
def complete_play(self, text, line, *ignored):
args = line.split(' ')
if len(args) == 2:
return self._complete_id()
return self._complete_object()
def do_play(self, _id):
"""
@ -109,7 +90,7 @@ class Radioob(ReplApplication):
print 'This command takes an argument: %s' % self.get_command_help('play', short=True)
return
radio = self._get_radio(_id, ['streams'])
radio = self.get_object(_id, 'get_radio', ['streams'])
if not radio:
print >>sys.stderr, 'Radio not found: ' % _id
return
@ -125,7 +106,7 @@ class Radioob(ReplApplication):
def complete_info(self, text, line, *ignored):
args = line.split(' ')
if len(args) == 2:
return self._complete_id()
return self._complete_object()
def do_info(self, _id):
"""
@ -137,24 +118,25 @@ class Radioob(ReplApplication):
print 'This command takes an argument: %s' % self.get_command_help('info', short=True)
return
radio = self._get_radio(_id)
radio = self.get_object(_id, 'get_radio')
if not radio:
print 'Radio not found:', _id
return
self.format(radio)
self.flush()
def do_list(self, pattern=None):
def do_search(self, pattern=None):
"""
list [PATTERN]
search PATTERN
List radios matching a PATTERN.
If PATTERN is not given, this command will list all the radios.
"""
self.set_formatter_header(u'Search pattern: %s' % pattern if pattern else u'All radios')
self.radios = []
self.change_path('/search')
for backend, radio in self.do('iter_radios_search', pattern=pattern):
self.radios.append(radio)
self.add_object(radio)
self.format(radio)
self.flush()

View file

@ -64,7 +64,8 @@ class Videoob(ReplApplication):
'play and download them and get information.'
CAPS = ICapVideo
EXTRA_FORMATTERS = {'video_list': VideoListFormatter}
COMMANDS_FORMATTERS = {'search': 'video_list', 'ls': 'video_list'}
COMMANDS_FORMATTERS = {'search': 'video_list',
'ls': 'video_list'}
nsfw = True

View file

@ -19,6 +19,7 @@
from weboob.capabilities.radio import ICapRadio, Radio, Stream, Emission
from weboob.capabilities.collection import ICapCollection, CollectionNotFound
from weboob.tools.backend import BaseBackend
from .browser import FranceInterBrowser
@ -26,7 +27,7 @@ from .browser import FranceInterBrowser
__all__ = ['FranceInterBackend']
class FranceInterBackend(BaseBackend, ICapRadio):
class FranceInterBackend(BaseBackend, ICapRadio, ICapCollection):
NAME = 'franceinter'
MAINTAINER = 'Johann Broudin'
EMAIL = 'johann.broudin@6-8.fr'
@ -37,13 +38,16 @@ class FranceInterBackend(BaseBackend, ICapRadio):
_RADIOS = {'franceinter': (u'france inter', u'france inter', u'http://mp3.live.tv-radio.com/franceinter/all/franceinterhautdebit.mp3')}
def iter_radios(self):
def iter_resources(self, splited_path):
if len(splited_path) > 0:
raise CollectionNotFound()
for id in self._RADIOS.iterkeys():
yield self.get_radio(id)
def iter_radios_search(self, pattern):
for radio in self.iter_radios():
if pattern in radio.title or pattern in radio.description:
for radio in self.iter_resources([]):
if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower():
yield radio
def get_radio(self, radio):
@ -59,7 +63,7 @@ class FranceInterBackend(BaseBackend, ICapRadio):
emission = self.browser.get_current(radio.id)
current = Emission(0)
current.title = emission
current.title = unicode(emission)
current.artist = None
radio.current = current

View file

@ -19,6 +19,7 @@
from weboob.capabilities.radio import ICapRadio, Radio, Stream, Emission
from weboob.capabilities.collection import ICapCollection, CollectionNotFound
from weboob.tools.backend import BaseBackend
from .browser import lemouvBrowser
@ -26,7 +27,7 @@ from .browser import lemouvBrowser
__all__ = ['lemouvBackend']
class lemouvBackend(BaseBackend, ICapRadio):
class lemouvBackend(BaseBackend, ICapRadio, ICapCollection):
NAME = 'lemouv'
MAINTAINER = 'Johann Broudin'
EMAIL = 'johann.broudin@6-8.fr'
@ -37,13 +38,16 @@ class lemouvBackend(BaseBackend, ICapRadio):
_RADIOS = {'lemouv': (u'le mouv\'', u'le mouv', u'http://mp3.live.tv-radio.com/lemouv/all/lemouvhautdebit.mp3')}
def iter_radios(self):
def iter_resources(self, splited_path):
if len(splited_path) > 0:
raise CollectionNotFound()
for id in self._RADIOS.iterkeys():
yield self.get_radio(id)
def iter_radios_search(self, pattern):
for radio in self.iter_radios():
if pattern in radio.title or pattern in radio.description:
for radio in self.iter_resources([]):
if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower():
yield radio
def get_radio(self, radio):

View file

@ -19,6 +19,7 @@
from weboob.capabilities.radio import ICapRadio, Radio, Stream, Emission
from weboob.capabilities.collection import ICapCollection, CollectionNotFound
from weboob.tools.backend import BaseBackend
from .browser import OuiFMBrowser
@ -27,7 +28,7 @@ from .browser import OuiFMBrowser
__all__ = ['OuiFMBackend']
class OuiFMBackend(BaseBackend, ICapRadio):
class OuiFMBackend(BaseBackend, ICapRadio, ICapCollection):
NAME = 'ouifm'
MAINTAINER = 'Romain Bignon'
EMAIL = 'romain@weboob.org'
@ -43,13 +44,16 @@ class OuiFMBackend(BaseBackend, ICapRadio):
'inde': (u'OUÏ FM Indé', u'OUI FM - Rock Indé', u'http://ouifm.ice.infomaniak.ch/ouifm5.mp3'),
}
def iter_radios(self):
def iter_resources(self, splited_path):
if len(splited_path) > 0:
raise CollectionNotFound()
for id in self._RADIOS.iterkeys():
yield self.get_radio(id)
def iter_radios_search(self, pattern):
for radio in self.iter_radios():
if pattern in radio.title or pattern in radio.description:
for radio in self.iter_resources([]):
if pattern.lower() in radio.title.lower() or pattern.lower() in radio.description.lower():
yield radio
def get_radio(self, radio):

View file

@ -61,9 +61,6 @@ class Radio(CapBaseObject):
self.add_field('streams', list)
class ICapRadio(IBaseCap):
def iter_radios(self):
raise NotImplementedError()
def iter_radios_search(self, pattern):
raise NotImplementedError()

View file

@ -137,13 +137,18 @@ class ReplApplication(Cmd, ConsoleApplication):
def interactive(self):
return self._interactive
def change_path(self, path):
def _change_prompt(self):
path = self.working_path.tostring()
if len(path) > 0 and path != '/':
self.prompt = '%s:%s> ' % (self.APPNAME, path)
else:
self.prompt = '%s> ' % (self.APPNAME)
self.objects = []
def change_path(self, path):
self.working_path.fromstring(path)
self._change_prompt()
def add_object(self, obj):
self.objects.append(obj)
@ -178,6 +183,14 @@ class ReplApplication(Cmd, ConsoleApplication):
if obj:
return obj
def unload_backends(self, *args, **kwargs):
self.objects = []
return ConsoleApplication.unload_backends(self, *args, **kwargs)
def load_backends(self, *args, **kwargs):
self.objects = []
return ConsoleApplication.load_backends(self, *args, **kwargs)
def main(self, argv):
cmd_args = argv[1:]
if cmd_args:
@ -810,7 +823,7 @@ class ReplApplication(Cmd, ConsoleApplication):
return 1
self.objects = objects
self.change_path(self.working_path.tostring())
self._change_prompt()
def _fetch_objects(self):
objects = []

View file

@ -55,6 +55,12 @@ class Path(object):
def get(self):
return copy.copy(self._working_path)
def fromstring(self, path):
if path[0] == '/':
path = path[1:]
escape = lambda s: s.replace('\/', '/')
self._working_path = map(escape, path.split('/'))
def tostring(self):
escape = lambda s: s.replace('/', '\/')
path = map(escape, self._working_path)