From 2fb878df2e346996b6b5a8cf7646245d4dd6f0c2 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Thu, 27 Sep 2012 20:18:22 +0200 Subject: [PATCH] MPD: Rename context.backend to context.core --- mopidy/frontends/mpd/dispatcher.py | 3 +- .../mpd/protocol/current_playlist.py | 84 +++++++++---------- mopidy/frontends/mpd/protocol/music_db.py | 10 +-- mopidy/frontends/mpd/protocol/playback.py | 72 ++++++++-------- mopidy/frontends/mpd/protocol/reflection.py | 2 +- mopidy/frontends/mpd/protocol/status.py | 26 +++--- .../mpd/protocol/stored_playlists.py | 10 +-- 7 files changed, 103 insertions(+), 104 deletions(-) diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index c9dee576..1f2af153 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -235,11 +235,10 @@ class MpdContext(object): self._core = None @property - def backend(self): + def core(self): """ The Mopidy core. An instance of :class:`mopidy.core.Core`. """ - # TODO: Rename property to 'core' if self._core is None: core_refs = ActorRegistry.get_by_class(core.Core) assert len(core_refs) == 1, \ diff --git a/mopidy/frontends/mpd/protocol/current_playlist.py b/mopidy/frontends/mpd/protocol/current_playlist.py index c60cbc4a..622f79c9 100644 --- a/mopidy/frontends/mpd/protocol/current_playlist.py +++ b/mopidy/frontends/mpd/protocol/current_playlist.py @@ -20,11 +20,11 @@ def add(context, uri): """ if not uri: return - for uri_scheme in context.backend.uri_schemes.get(): + for uri_scheme in context.core.uri_schemes.get(): if uri.startswith(uri_scheme): - track = context.backend.library.lookup(uri).get() + track = context.core.library.lookup(uri).get() if track is not None: - context.backend.current_playlist.add(track) + context.core.current_playlist.add(track) return raise MpdNoExistError( u'directory or file not found', command=u'add') @@ -52,12 +52,12 @@ def addid(context, uri, songpos=None): raise MpdNoExistError(u'No such song', command=u'addid') if songpos is not None: songpos = int(songpos) - track = context.backend.library.lookup(uri).get() + track = context.core.library.lookup(uri).get() if track is None: raise MpdNoExistError(u'No such song', command=u'addid') - if songpos and songpos > context.backend.current_playlist.length.get(): + if songpos and songpos > context.core.current_playlist.length.get(): raise MpdArgError(u'Bad song index', command=u'addid') - cp_track = context.backend.current_playlist.add(track, + cp_track = context.core.current_playlist.add(track, at_position=songpos).get() return ('Id', cp_track.cpid) @@ -74,21 +74,21 @@ def delete_range(context, start, end=None): if end is not None: end = int(end) else: - end = context.backend.current_playlist.length.get() - cp_tracks = context.backend.current_playlist.slice(start, end).get() + end = context.core.current_playlist.length.get() + cp_tracks = context.core.current_playlist.slice(start, end).get() if not cp_tracks: raise MpdArgError(u'Bad song index', command=u'delete') for (cpid, _) in cp_tracks: - context.backend.current_playlist.remove(cpid=cpid) + context.core.current_playlist.remove(cpid=cpid) @handle_request(r'^delete "(?P\d+)"$') def delete_songpos(context, songpos): """See :meth:`delete_range`""" try: songpos = int(songpos) - (cpid, _) = context.backend.current_playlist.slice( + (cpid, _) = context.core.current_playlist.slice( songpos, songpos + 1).get()[0] - context.backend.current_playlist.remove(cpid=cpid) + context.core.current_playlist.remove(cpid=cpid) except IndexError: raise MpdArgError(u'Bad song index', command=u'delete') @@ -103,9 +103,9 @@ def deleteid(context, cpid): """ try: cpid = int(cpid) - if context.backend.playback.current_cpid.get() == cpid: - context.backend.playback.next() - return context.backend.current_playlist.remove(cpid=cpid).get() + if context.core.playback.current_cpid.get() == cpid: + context.core.playback.next() + return context.core.current_playlist.remove(cpid=cpid).get() except LookupError: raise MpdNoExistError(u'No such song', command=u'deleteid') @@ -118,7 +118,7 @@ def clear(context): Clears the current playlist. """ - context.backend.current_playlist.clear() + context.core.current_playlist.clear() @handle_request(r'^move "(?P\d+):(?P\d+)*" "(?P\d+)"$') def move_range(context, start, to, end=None): @@ -131,18 +131,18 @@ def move_range(context, start, to, end=None): ``TO`` in the playlist. """ if end is None: - end = context.backend.current_playlist.length.get() + end = context.core.current_playlist.length.get() start = int(start) end = int(end) to = int(to) - context.backend.current_playlist.move(start, end, to) + context.core.current_playlist.move(start, end, to) @handle_request(r'^move "(?P\d+)" "(?P\d+)"$') def move_songpos(context, songpos, to): """See :meth:`move_range`.""" songpos = int(songpos) to = int(to) - context.backend.current_playlist.move(songpos, songpos + 1, to) + context.core.current_playlist.move(songpos, songpos + 1, to) @handle_request(r'^moveid "(?P\d+)" "(?P\d+)"$') def moveid(context, cpid, to): @@ -157,9 +157,9 @@ def moveid(context, cpid, to): """ cpid = int(cpid) to = int(to) - cp_track = context.backend.current_playlist.get(cpid=cpid).get() - position = context.backend.current_playlist.index(cp_track).get() - context.backend.current_playlist.move(position, position + 1, to) + cp_track = context.core.current_playlist.get(cpid=cpid).get() + position = context.core.current_playlist.index(cp_track).get() + context.core.current_playlist.move(position, position + 1, to) @handle_request(r'^playlist$') def playlist(context): @@ -192,8 +192,8 @@ def playlistfind(context, tag, needle): """ if tag == 'filename': try: - cp_track = context.backend.current_playlist.get(uri=needle).get() - position = context.backend.current_playlist.index(cp_track).get() + cp_track = context.core.current_playlist.get(uri=needle).get() + position = context.core.current_playlist.index(cp_track).get() return track_to_mpd_format(cp_track, position=position) except LookupError: return None @@ -212,14 +212,14 @@ def playlistid(context, cpid=None): if cpid is not None: try: cpid = int(cpid) - cp_track = context.backend.current_playlist.get(cpid=cpid).get() - position = context.backend.current_playlist.index(cp_track).get() + cp_track = context.core.current_playlist.get(cpid=cpid).get() + position = context.core.current_playlist.index(cp_track).get() return track_to_mpd_format(cp_track, position=position) except LookupError: raise MpdNoExistError(u'No such song', command=u'playlistid') else: return tracks_to_mpd_format( - context.backend.current_playlist.cp_tracks.get()) + context.core.current_playlist.cp_tracks.get()) @handle_request(r'^playlistinfo$') @handle_request(r'^playlistinfo "-1"$') @@ -243,19 +243,19 @@ def playlistinfo(context, songpos=None, """ if songpos is not None: songpos = int(songpos) - cp_track = context.backend.current_playlist.cp_tracks.get()[songpos] + cp_track = context.core.current_playlist.cp_tracks.get()[songpos] return track_to_mpd_format(cp_track, position=songpos) else: if start is None: start = 0 start = int(start) - if not (0 <= start <= context.backend.current_playlist.length.get()): + if not (0 <= start <= context.core.current_playlist.length.get()): raise MpdArgError(u'Bad song index', command=u'playlistinfo') if end is not None: end = int(end) - if end > context.backend.current_playlist.length.get(): + if end > context.core.current_playlist.length.get(): end = None - cp_tracks = context.backend.current_playlist.cp_tracks.get() + cp_tracks = context.core.current_playlist.cp_tracks.get() return tracks_to_mpd_format(cp_tracks, start, end) @handle_request(r'^playlistsearch "(?P[^"]+)" "(?P[^"]+)"$') @@ -294,9 +294,9 @@ def plchanges(context, version): - Calls ``plchanges "-1"`` two times per second to get the entire playlist. """ # XXX Naive implementation that returns all tracks as changed - if int(version) < context.backend.current_playlist.version: + if int(version) < context.core.current_playlist.version: return tracks_to_mpd_format( - context.backend.current_playlist.cp_tracks.get()) + context.core.current_playlist.cp_tracks.get()) @handle_request(r'^plchangesposid "(?P\d+)"$') def plchangesposid(context, version): @@ -313,10 +313,10 @@ def plchangesposid(context, version): ``playlistlength`` returned by status command. """ # XXX Naive implementation that returns all tracks as changed - if int(version) != context.backend.current_playlist.version.get(): + if int(version) != context.core.current_playlist.version.get(): result = [] for (position, (cpid, _)) in enumerate( - context.backend.current_playlist.cp_tracks.get()): + context.core.current_playlist.cp_tracks.get()): result.append((u'cpos', position)) result.append((u'Id', cpid)) return result @@ -336,7 +336,7 @@ def shuffle(context, start=None, end=None): start = int(start) if end is not None: end = int(end) - context.backend.current_playlist.shuffle(start, end) + context.core.current_playlist.shuffle(start, end) @handle_request(r'^swap "(?P\d+)" "(?P\d+)"$') def swap(context, songpos1, songpos2): @@ -349,15 +349,15 @@ def swap(context, songpos1, songpos2): """ songpos1 = int(songpos1) songpos2 = int(songpos2) - tracks = context.backend.current_playlist.tracks.get() + tracks = context.core.current_playlist.tracks.get() song1 = tracks[songpos1] song2 = tracks[songpos2] del tracks[songpos1] tracks.insert(songpos1, song2) del tracks[songpos2] tracks.insert(songpos2, song1) - context.backend.current_playlist.clear() - context.backend.current_playlist.append(tracks) + context.core.current_playlist.clear() + context.core.current_playlist.append(tracks) @handle_request(r'^swapid "(?P\d+)" "(?P\d+)"$') def swapid(context, cpid1, cpid2): @@ -370,8 +370,8 @@ def swapid(context, cpid1, cpid2): """ cpid1 = int(cpid1) cpid2 = int(cpid2) - cp_track1 = context.backend.current_playlist.get(cpid=cpid1).get() - cp_track2 = context.backend.current_playlist.get(cpid=cpid2).get() - position1 = context.backend.current_playlist.index(cp_track1).get() - position2 = context.backend.current_playlist.index(cp_track2).get() + cp_track1 = context.core.current_playlist.get(cpid=cpid1).get() + cp_track2 = context.core.current_playlist.get(cpid=cpid2).get() + position1 = context.core.current_playlist.index(cp_track1).get() + position2 = context.core.current_playlist.index(cp_track2).get() swap(context, position1, position2) diff --git a/mopidy/frontends/mpd/protocol/music_db.py b/mopidy/frontends/mpd/protocol/music_db.py index d0128a1e..2678714a 100644 --- a/mopidy/frontends/mpd/protocol/music_db.py +++ b/mopidy/frontends/mpd/protocol/music_db.py @@ -70,7 +70,7 @@ def find(context, mpd_query): """ query = _build_query(mpd_query) return playlist_to_mpd_format( - context.backend.library.find_exact(**query).get()) + context.core.library.find_exact(**query).get()) @handle_request(r'^findadd ' r'(?P("?([Aa]lbum|[Aa]rtist|[Ff]ilename|[Tt]itle|[Aa]ny)"? ' @@ -223,7 +223,7 @@ def _list_build_query(field, mpd_query): def _list_artist(context, query): artists = set() - playlist = context.backend.library.find_exact(**query).get() + playlist = context.core.library.find_exact(**query).get() for track in playlist.tracks: for artist in track.artists: artists.add((u'Artist', artist.name)) @@ -231,7 +231,7 @@ def _list_artist(context, query): def _list_album(context, query): albums = set() - playlist = context.backend.library.find_exact(**query).get() + playlist = context.core.library.find_exact(**query).get() for track in playlist.tracks: if track.album is not None: albums.add((u'Album', track.album.name)) @@ -239,7 +239,7 @@ def _list_album(context, query): def _list_date(context, query): dates = set() - playlist = context.backend.library.find_exact(**query).get() + playlist = context.core.library.find_exact(**query).get() for track in playlist.tracks: if track.date is not None: dates.add((u'Date', track.date)) @@ -333,7 +333,7 @@ def search(context, mpd_query): """ query = _build_query(mpd_query) return playlist_to_mpd_format( - context.backend.library.search(**query).get()) + context.core.library.search(**query).get()) @handle_request(r'^update( "(?P[^"]+)")*$') def update(context, uri=None, rescan_unmodified_files=False): diff --git a/mopidy/frontends/mpd/protocol/playback.py b/mopidy/frontends/mpd/protocol/playback.py index 4152f11e..76cefdc3 100644 --- a/mopidy/frontends/mpd/protocol/playback.py +++ b/mopidy/frontends/mpd/protocol/playback.py @@ -16,9 +16,9 @@ def consume(context, state): playlist. """ if int(state): - context.backend.playback.consume = True + context.core.playback.consume = True else: - context.backend.playback.consume = False + context.core.playback.consume = False @handle_request(r'^crossfade "(?P\d+)"$') def crossfade(context, seconds): @@ -87,7 +87,7 @@ def next_(context): order as the first time. """ - return context.backend.playback.next().get() + return context.core.playback.next().get() @handle_request(r'^pause$') @handle_request(r'^pause "(?P[01])"$') @@ -104,14 +104,14 @@ def pause(context, state=None): - Calls ``pause`` without any arguments to toogle pause. """ if state is None: - if (context.backend.playback.state.get() == PlaybackState.PLAYING): - context.backend.playback.pause() - elif (context.backend.playback.state.get() == PlaybackState.PAUSED): - context.backend.playback.resume() + if (context.core.playback.state.get() == PlaybackState.PLAYING): + context.core.playback.pause() + elif (context.core.playback.state.get() == PlaybackState.PAUSED): + context.core.playback.resume() elif int(state): - context.backend.playback.pause() + context.core.playback.pause() else: - context.backend.playback.resume() + context.core.playback.resume() @handle_request(r'^play$') def play(context): @@ -119,7 +119,7 @@ def play(context): The original MPD server resumes from the paused state on ``play`` without arguments. """ - return context.backend.playback.play().get() + return context.core.playback.play().get() @handle_request(r'^playid (?P-?\d+)$') @handle_request(r'^playid "(?P-?\d+)"$') @@ -144,8 +144,8 @@ def playid(context, cpid): if cpid == -1: return _play_minus_one(context) try: - cp_track = context.backend.current_playlist.get(cpid=cpid).get() - return context.backend.playback.play(cp_track).get() + cp_track = context.core.current_playlist.get(cpid=cpid).get() + return context.core.playback.play(cp_track).get() except LookupError: raise MpdNoExistError(u'No such song', command=u'playid') @@ -176,23 +176,23 @@ def playpos(context, songpos): if songpos == -1: return _play_minus_one(context) try: - cp_track = context.backend.current_playlist.slice( + cp_track = context.core.current_playlist.slice( songpos, songpos + 1).get()[0] - return context.backend.playback.play(cp_track).get() + return context.core.playback.play(cp_track).get() except IndexError: raise MpdArgError(u'Bad song index', command=u'play') def _play_minus_one(context): - if (context.backend.playback.state.get() == PlaybackState.PLAYING): + if (context.core.playback.state.get() == PlaybackState.PLAYING): return # Nothing to do - elif (context.backend.playback.state.get() == PlaybackState.PAUSED): - return context.backend.playback.resume().get() - elif context.backend.playback.current_cp_track.get() is not None: - cp_track = context.backend.playback.current_cp_track.get() - return context.backend.playback.play(cp_track).get() - elif context.backend.current_playlist.slice(0, 1).get(): - cp_track = context.backend.current_playlist.slice(0, 1).get()[0] - return context.backend.playback.play(cp_track).get() + elif (context.core.playback.state.get() == PlaybackState.PAUSED): + return context.core.playback.resume().get() + elif context.core.playback.current_cp_track.get() is not None: + cp_track = context.core.playback.current_cp_track.get() + return context.core.playback.play(cp_track).get() + elif context.core.current_playlist.slice(0, 1).get(): + cp_track = context.core.current_playlist.slice(0, 1).get()[0] + return context.core.playback.play(cp_track).get() else: return # Fail silently @@ -240,7 +240,7 @@ def previous(context): ``previous`` should do a seek to time position 0. """ - return context.backend.playback.previous().get() + return context.core.playback.previous().get() @handle_request(r'^random (?P[01])$') @handle_request(r'^random "(?P[01])"$') @@ -253,9 +253,9 @@ def random(context, state): Sets random state to ``STATE``, ``STATE`` should be 0 or 1. """ if int(state): - context.backend.playback.random = True + context.core.playback.random = True else: - context.backend.playback.random = False + context.core.playback.random = False @handle_request(r'^repeat (?P[01])$') @handle_request(r'^repeat "(?P[01])"$') @@ -268,9 +268,9 @@ def repeat(context, state): Sets repeat state to ``STATE``, ``STATE`` should be 0 or 1. """ if int(state): - context.backend.playback.repeat = True + context.core.playback.repeat = True else: - context.backend.playback.repeat = False + context.core.playback.repeat = False @handle_request(r'^replay_gain_mode "(?P(off|track|album))"$') def replay_gain_mode(context, mode): @@ -315,9 +315,9 @@ def seek(context, songpos, seconds): - issues ``seek 1 120`` without quotes around the arguments. """ - if context.backend.playback.current_playlist_position != songpos: + if context.core.playback.current_playlist_position != songpos: playpos(context, songpos) - context.backend.playback.seek(int(seconds) * 1000) + context.core.playback.seek(int(seconds) * 1000) @handle_request(r'^seekid "(?P\d+)" "(?P\d+)"$') def seekid(context, cpid, seconds): @@ -328,9 +328,9 @@ def seekid(context, cpid, seconds): Seeks to the position ``TIME`` (in seconds) of song ``SONGID``. """ - if context.backend.playback.current_cpid != cpid: + if context.core.playback.current_cpid != cpid: playid(context, cpid) - context.backend.playback.seek(int(seconds) * 1000) + context.core.playback.seek(int(seconds) * 1000) @handle_request(r'^setvol (?P[-+]*\d+)$') @handle_request(r'^setvol "(?P[-+]*\d+)"$') @@ -351,7 +351,7 @@ def setvol(context, volume): volume = 0 if volume > 100: volume = 100 - context.backend.playback.volume = volume + context.core.playback.volume = volume @handle_request(r'^single (?P[01])$') @handle_request(r'^single "(?P[01])"$') @@ -366,9 +366,9 @@ def single(context, state): song is repeated if the ``repeat`` mode is enabled. """ if int(state): - context.backend.playback.single = True + context.core.playback.single = True else: - context.backend.playback.single = False + context.core.playback.single = False @handle_request(r'^stop$') def stop(context): @@ -379,4 +379,4 @@ def stop(context): Stops playing. """ - context.backend.playback.stop() + context.core.playback.stop() diff --git a/mopidy/frontends/mpd/protocol/reflection.py b/mopidy/frontends/mpd/protocol/reflection.py index df13b4b4..8cd1337b 100644 --- a/mopidy/frontends/mpd/protocol/reflection.py +++ b/mopidy/frontends/mpd/protocol/reflection.py @@ -84,4 +84,4 @@ def urlhandlers(context): Gets a list of available URL handlers. """ return [(u'handler', uri_scheme) - for uri_scheme in context.backend.uri_schemes.get()] + for uri_scheme in context.core.uri_schemes.get()] diff --git a/mopidy/frontends/mpd/protocol/status.py b/mopidy/frontends/mpd/protocol/status.py index fc24e1e1..4f48265c 100644 --- a/mopidy/frontends/mpd/protocol/status.py +++ b/mopidy/frontends/mpd/protocol/status.py @@ -31,9 +31,9 @@ def currentsong(context): Displays the song info of the current song (same song that is identified in status). """ - current_cp_track = context.backend.playback.current_cp_track.get() + current_cp_track = context.core.playback.current_cp_track.get() if current_cp_track is not None: - position = context.backend.playback.current_playlist_position.get() + position = context.core.playback.current_playlist_position.get() return track_to_mpd_format(current_cp_track, position=position) @handle_request(r'^idle$') @@ -166,18 +166,18 @@ def status(context): decimal places for millisecond precision. """ futures = { - 'current_playlist.length': context.backend.current_playlist.length, - 'current_playlist.version': context.backend.current_playlist.version, - 'playback.volume': context.backend.playback.volume, - 'playback.consume': context.backend.playback.consume, - 'playback.random': context.backend.playback.random, - 'playback.repeat': context.backend.playback.repeat, - 'playback.single': context.backend.playback.single, - 'playback.state': context.backend.playback.state, - 'playback.current_cp_track': context.backend.playback.current_cp_track, + 'current_playlist.length': context.core.current_playlist.length, + 'current_playlist.version': context.core.current_playlist.version, + 'playback.volume': context.core.playback.volume, + 'playback.consume': context.core.playback.consume, + 'playback.random': context.core.playback.random, + 'playback.repeat': context.core.playback.repeat, + 'playback.single': context.core.playback.single, + 'playback.state': context.core.playback.state, + 'playback.current_cp_track': context.core.playback.current_cp_track, 'playback.current_playlist_position': - context.backend.playback.current_playlist_position, - 'playback.time_position': context.backend.playback.time_position, + context.core.playback.current_playlist_position, + 'playback.time_position': context.core.playback.time_position, } pykka.future.get_all(futures.values()) result = [ diff --git a/mopidy/frontends/mpd/protocol/stored_playlists.py b/mopidy/frontends/mpd/protocol/stored_playlists.py index bb39d328..c21f4714 100644 --- a/mopidy/frontends/mpd/protocol/stored_playlists.py +++ b/mopidy/frontends/mpd/protocol/stored_playlists.py @@ -20,7 +20,7 @@ def listplaylist(context, name): file: relative/path/to/file3.mp3 """ try: - playlist = context.backend.stored_playlists.get(name=name).get() + playlist = context.core.stored_playlists.get(name=name).get() return ['file: %s' % t.uri for t in playlist.tracks] except LookupError: raise MpdNoExistError(u'No such playlist', command=u'listplaylist') @@ -40,7 +40,7 @@ def listplaylistinfo(context, name): Album, Artist, Track """ try: - playlist = context.backend.stored_playlists.get(name=name).get() + playlist = context.core.stored_playlists.get(name=name).get() return playlist_to_mpd_format(playlist) except LookupError: raise MpdNoExistError( @@ -68,7 +68,7 @@ def listplaylists(context): Last-Modified: 2010-02-06T02:11:08Z """ result = [] - for playlist in context.backend.stored_playlists.playlists.get(): + for playlist in context.core.stored_playlists.playlists.get(): result.append((u'playlist', playlist.name)) last_modified = (playlist.last_modified or dt.datetime.now()).isoformat() @@ -94,8 +94,8 @@ def load(context, name): - ``load`` appends the given playlist to the current playlist. """ try: - playlist = context.backend.stored_playlists.get(name=name).get() - context.backend.current_playlist.append(playlist.tracks) + playlist = context.core.stored_playlists.get(name=name).get() + context.core.current_playlist.append(playlist.tracks) except LookupError: raise MpdNoExistError(u'No such playlist', command=u'load')