mpd: minor style commit

This commit is contained in:
Thomas Refis 2013-04-09 11:44:53 +02:00
parent 698f505da6
commit 7d59d03ec0
3 changed files with 29 additions and 23 deletions

View File

@ -232,12 +232,9 @@ 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
playlist_uri_from_name = None
#: the invert map (uri to printing name)
from_uri = None
playlist_name_from_uri = None
def __init__(self, dispatcher, session=None, core=None):
self.dispatcher = dispatcher
@ -245,21 +242,28 @@ class MpdContext(object):
self.core = core
self.events = set()
self.subscriptions = set()
self.to_uri = {}
self.from_uri = {}
self.playlist_uri_from_name = {}
self.playlist_name_from_uri = {}
self.refresh_playlists_mapping()
def create_unique_name(self, name):
i = 1
while name in self.playlist_uri_from_name:
name = '%s [%d]' % playlist.name % i
i += 1
return name
def refresh_playlists_mapping(self):
"""
Maintain map between playlists and unique playlist names to be used by
MPD
"""
if self.core is not None:
self.to_uri.clear()
self.from_uri.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 = 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
name = self.create_unique_name(playlist.name)
self.playlist_uri_from_name[name] = playlist.uri
self.playlist_name_from_uri[playlist.uri] = name

View File

@ -381,11 +381,12 @@ def searchaddpl(context, playlist_name, mpd_query):
return
results = context.core.library.search(**query).get()
if len(context.to_uri) == 0:
if len(context.playlist_uri_from_name) == 0:
context.refresh_playlists_mapping()
if playlist_name in context.to_uri:
playlist = context.core.playlists.lookup(context.to_uri[playlist_name]).get()
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.core.playlists.create(playlist_name).get()
tracks = list(playlist.tracks) + _get_tracks(results)

View File

@ -80,9 +80,11 @@ def listplaylists(context):
for playlist in context.core.playlists.playlists.get():
if not playlist.name:
continue
if playlist.uri not in context.from_uri:
context.refresh_playlists_mapping() # the maps are not synced, we refresh them
result.append(('playlist', context.from_uri[playlist.uri]))
if playlist.uri not in context.playlist_name_from_uri:
# the maps are not synced, we refresh them
context.refresh_playlists_mapping()
name = context.playlist_name_from_uri[playlist.uri]
result.append(('playlist', name))
last_modified = (
playlist.last_modified or dt.datetime.utcnow()).isoformat()
# Remove microseconds
@ -92,7 +94,6 @@ def listplaylists(context):
result.append(('Last-Modified', last_modified))
return result
@handle_request(r'^load "(?P<name>[^"]+)"( "(?P<start>\d+):(?P<end>\d+)*")*$')
def load(context, name, start=None, end=None):
"""