Use helper function lookup_playlist_from_name() to resolve uniquified MPD
playlist names to mopidy playlists in all MPD playlist handling commands. Also make playlist_uri_from_name map private.
This commit is contained in:
parent
1debaf3276
commit
15875b092c
@ -236,7 +236,7 @@ class MpdContext(object):
|
||||
#: The subsytems that we want to be notified about in idle mode.
|
||||
subscriptions = None
|
||||
|
||||
playlist_uri_from_name = None
|
||||
_playlist_uri_from_name = None
|
||||
playlist_name_from_uri = None
|
||||
|
||||
def __init__(self, dispatcher, session=None, config=None, core=None):
|
||||
@ -246,14 +246,14 @@ class MpdContext(object):
|
||||
self.core = core
|
||||
self.events = set()
|
||||
self.subscriptions = set()
|
||||
self.playlist_uri_from_name = {}
|
||||
self._playlist_uri_from_name = {}
|
||||
self.playlist_name_from_uri = {}
|
||||
self.refresh_playlists_mapping()
|
||||
|
||||
def create_unique_name(self, playlist_name):
|
||||
name = playlist_name
|
||||
i = 2
|
||||
while name in self.playlist_uri_from_name:
|
||||
while name in self._playlist_uri_from_name:
|
||||
name = '%s [%d]' % (playlist_name, i)
|
||||
i += 1
|
||||
return name
|
||||
@ -264,11 +264,23 @@ class MpdContext(object):
|
||||
MPD
|
||||
"""
|
||||
if self.core is not None:
|
||||
self.playlist_uri_from_name.clear()
|
||||
self._playlist_uri_from_name.clear()
|
||||
self.playlist_name_from_uri.clear()
|
||||
for playlist in self.core.playlists.playlists.get():
|
||||
if not playlist.name:
|
||||
continue
|
||||
name = self.create_unique_name(playlist.name)
|
||||
self.playlist_uri_from_name[name] = playlist.uri
|
||||
self._playlist_uri_from_name[name] = playlist.uri
|
||||
self.playlist_name_from_uri[playlist.uri] = name
|
||||
logger.info("Refreshed name mappings for %u playlists" % len(self.playlist_name_from_uri))
|
||||
|
||||
def lookup_playlist_from_name(self, name):
|
||||
"""
|
||||
Helper function to retrieve a playlist from it's unique MPD name.
|
||||
"""
|
||||
if len(self._playlist_uri_from_name) == 0:
|
||||
self.refresh_playlists_mapping()
|
||||
if name not in self._playlist_uri_from_name:
|
||||
return None;
|
||||
uri = self._playlist_uri_from_name[name]
|
||||
return self.core.playlists.lookup(uri).get()
|
||||
|
||||
@ -381,13 +381,8 @@ def searchaddpl(context, playlist_name, mpd_query):
|
||||
return
|
||||
results = context.core.library.search(**query).get()
|
||||
|
||||
if len(context.playlist_uri_from_name) == 0:
|
||||
context.refresh_playlists_mapping()
|
||||
|
||||
if playlist_name in context.playlist_uri_from_name:
|
||||
uri = context.playlist_uri_from_name[playlist_name]
|
||||
playlist = context.core.playlists.lookup(uri).get()
|
||||
else:
|
||||
playlist = context.lookup_playlist_from_name(playlist_name)
|
||||
if not playlist:
|
||||
playlist = context.core.playlists.create(playlist_name).get()
|
||||
tracks = list(playlist.tracks) + _get_tracks(results)
|
||||
playlist = playlist.copy(tracks=tracks)
|
||||
|
||||
@ -23,10 +23,10 @@ def listplaylist(context, name):
|
||||
file: relative/path/to/file2.ogg
|
||||
file: relative/path/to/file3.mp3
|
||||
"""
|
||||
playlists = context.core.playlists.filter(name=name).get()
|
||||
if not playlists:
|
||||
playlist = context.lookup_playlist_from_name(name)
|
||||
if not playlist:
|
||||
raise MpdNoExistError('No such playlist', command='listplaylist')
|
||||
return ['file: %s' % t.uri for t in playlists[0].tracks]
|
||||
return ['file: %s' % t.uri for t in playlist.tracks]
|
||||
|
||||
|
||||
@handle_request(r'^listplaylistinfo (?P<name>\w+)$')
|
||||
@ -44,10 +44,10 @@ def listplaylistinfo(context, name):
|
||||
Standard track listing, with fields: file, Time, Title, Date,
|
||||
Album, Artist, Track
|
||||
"""
|
||||
playlists = context.core.playlists.filter(name=name).get()
|
||||
if not playlists:
|
||||
playlist = context.lookup_playlist_from_name(name)
|
||||
if not playlist:
|
||||
raise MpdNoExistError('No such playlist', command='listplaylistinfo')
|
||||
return playlist_to_mpd_format(playlists[0])
|
||||
return playlist_to_mpd_format(playlist)
|
||||
|
||||
|
||||
@handle_request(r'^listplaylists$')
|
||||
@ -117,14 +117,14 @@ def load(context, name, start=None, end=None):
|
||||
- MPD 0.17.1 does not fail if the specified range is outside the playlist,
|
||||
in either or both ends.
|
||||
"""
|
||||
playlists = context.core.playlists.filter(name=name).get()
|
||||
if not playlists:
|
||||
playlist = context.lookup_playlist_from_name(name)
|
||||
if not playlist:
|
||||
raise MpdNoExistError('No such playlist', command='load')
|
||||
if start is not None:
|
||||
start = int(start)
|
||||
if end is not None:
|
||||
end = int(end)
|
||||
context.core.tracklist.add(playlists[0].tracks[start:end])
|
||||
context.core.tracklist.add(playlist.tracks[start:end])
|
||||
|
||||
|
||||
@handle_request(r'^playlistadd "(?P<name>[^"]+)" "(?P<uri>[^"]+)"$')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user