mpd: playlist name disambiguation

This commit is contained in:
Thomas Refis 2013-04-06 21:27:59 +02:00
parent 837db09aff
commit 87ce309749
3 changed files with 29 additions and 4 deletions

View File

@ -232,9 +232,33 @@ class MpdContext(object):
#: The subsytems that we want to be notified about in idle mode.
subscriptions = None
#: a map from playlists printing names to uris (necessary as mpd requires
#: playlists names to be unique)
to_uri = None
#: the invert map (uri to printing name)
from_uri = None
def __init__(self, dispatcher, session=None, core=None):
self.dispatcher = dispatcher
self.session = session
self.core = core
self.events = set()
self.subscriptions = set()
self.to_uri = dict()
self.from_uri = dict()
self.refresh_maps()
def refresh_maps(self):
self.to_uri.clear()
self.from_uri.clear()
for playlist in self.core.playlists.playlists.get():
if not playlist.name:
continue
name = playlist.name
i = 1
while name in self.to_uri:
name = '%s [%d]' % playlist.name % i
i += 1
self.to_uri[name] = playlist.uri
self.from_uri[playlist.uri] = name

View File

@ -381,9 +381,8 @@ def searchaddpl(context, playlist_name, mpd_query):
return
results = context.core.library.search(**query).get()
playlists = context.core.playlists.filter(name=playlist_name).get()
if playlists:
playlist = playlists[0]
if playlist_name in context.to_uri:
playlist = context.core.playlists.lookup(context.to_uri[playlist_name])
else:
playlist = context.core.playlists.create(playlist_name).get()
tracks = list(playlist.tracks) + _get_tracks(results)

View File

@ -80,7 +80,9 @@ def listplaylists(context):
for playlist in context.core.playlists.playlists.get():
if not playlist.name:
continue
result.append(('playlist', playlist.name))
if playlist.uri not in context.from_uri:
context.refresh_maps() # the maps are not synced, we refresh them
result.append(('playlist', context.from_uri[playlist.uri]))
last_modified = (
playlist.last_modified or dt.datetime.utcnow()).isoformat()
# Remove microseconds