backend: Add translate_uri for simpler API for the simple case.

change_track(track) simply calls translate_uri(uri) by default now so that 90%
of clients with custom URIs should be able to just implement this one method on
the backend any ignore everything else.
This commit is contained in:
Thomas Adamcik 2015-03-21 11:43:46 +01:00
parent bbf52eede9
commit a6ef1bb8d9
2 changed files with 23 additions and 14 deletions

View File

@ -199,23 +199,38 @@ class PlaybackProvider(object):
"""
self.audio.prepare_change().get()
def translate_uri(self, uri):
"""
Convert custom URI scheme to real playable uri.
This is very likely the *only* thing you need to override as a backend
author. Typically this is where you convert any mopidy specific URIs
to real URIs and then return it.
:param uri: the URI to translate.
:type uri: string
:rtype: string
"""
return uri
def change_track(self, track):
"""
Swith to provided track.
*MAY be reimplemented by subclass.*
This is very likely the *only* thing you need to override as a backend
author. Typically this is where you convert any mopidy specific URIs
to real URIs and then return::
It is unlikely it makes sense for any backends to override
this. For most practical purposes it should be considered an internal
call between backends and core that backend authors should not touch.
return super(MyBackend, self).change_track(track.copy(uri=new_uri))
The default implementation will call :method:`translate_uri` which
is what you want to implement.
:param track: the track to play
:type track: :class:`mopidy.models.Track`
:rtype: :class:`True` if successful, else :class:`False`
"""
self.audio.set_uri(track.uri).get()
self.audio.set_uri(self.translate_uri(track.uri)).get()
return True
def resume(self):

View File

@ -1,16 +1,10 @@
from __future__ import absolute_import, unicode_literals
import logging
from mopidy import backend
from mopidy.local import translator
logger = logging.getLogger(__name__)
class LocalPlaybackProvider(backend.PlaybackProvider):
def change_track(self, track):
track = track.copy(uri=translator.local_track_uri_to_file_uri(
track.uri, self.backend.config['local']['media_dir']))
return super(LocalPlaybackProvider, self).change_track(track)
def translate_uri(self, uri):
return translator.local_track_uri_to_file_uri(
uri, self.backend.config['local']['media_dir'])