Share a global MPDUriMappper between all MPD sessions

This commit is contained in:
kingosticks 2015-02-08 23:42:12 +00:00
parent 3dde71ed5e
commit f77b73a260
3 changed files with 18 additions and 14 deletions

View File

@ -6,7 +6,7 @@ import pykka
from mopidy import exceptions, zeroconf from mopidy import exceptions, zeroconf
from mopidy.core import CoreListener from mopidy.core import CoreListener
from mopidy.mpd import session from mopidy.mpd import dispatcher, session
from mopidy.utils import encoding, network, process from mopidy.utils import encoding, network, process
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -18,6 +18,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener):
self.hostname = network.format_hostname(config['mpd']['hostname']) self.hostname = network.format_hostname(config['mpd']['hostname'])
self.port = config['mpd']['port'] self.port = config['mpd']['port']
self.uri_map = dispatcher.MpdUriMapper(core)
self.zeroconf_name = config['mpd']['zeroconf'] self.zeroconf_name = config['mpd']['zeroconf']
self.zeroconf_service = None self.zeroconf_service = None
@ -29,6 +30,7 @@ class MpdFrontend(pykka.ThreadingActor, CoreListener):
protocol_kwargs={ protocol_kwargs={
'config': config, 'config': config,
'core': core, 'core': core,
'uri_map': self.uri_map,
}, },
max_connections=config['mpd']['max_connections'], max_connections=config['mpd']['max_connections'],
timeout=config['mpd']['connection_timeout']) timeout=config['mpd']['connection_timeout'])

View File

@ -21,7 +21,7 @@ class MpdDispatcher(object):
_noidle = re.compile(r'^noidle$') _noidle = re.compile(r'^noidle$')
def __init__(self, session=None, config=None, core=None): def __init__(self, session=None, config=None, core=None, uri_map=None):
self.config = config self.config = config
self.authenticated = False self.authenticated = False
self.command_list_receiving = False self.command_list_receiving = False
@ -29,7 +29,7 @@ class MpdDispatcher(object):
self.command_list = [] self.command_list = []
self.command_list_index = None self.command_list_index = None
self.context = MpdContext( self.context = MpdContext(
self, session=session, config=config, core=core) self, session=session, config=config, core=core, uri_map=uri_map)
def handle_request(self, request, current_command_list_index=None): def handle_request(self, request, current_command_list_index=None):
"""Dispatch incoming requests to the correct handler.""" """Dispatch incoming requests to the correct handler."""
@ -227,9 +227,10 @@ class MpdContext(object):
#: The subsytems that we want to be notified about in idle mode. #: The subsytems that we want to be notified about in idle mode.
subscriptions = None subscriptions = None
_mapping = None _uri_map = None
def __init__(self, dispatcher, session=None, config=None, core=None): def __init__(self, dispatcher, session=None, config=None, core=None,
uri_map=None):
self.dispatcher = dispatcher self.dispatcher = dispatcher
self.session = session self.session = session
if config is not None: if config is not None:
@ -237,19 +238,19 @@ class MpdContext(object):
self.core = core self.core = core
self.events = set() self.events = set()
self.subscriptions = set() self.subscriptions = set()
self._mapping = MpdUriMapper(core) self._uri_map = uri_map
def lookup_playlist_from_name(self, name): def lookup_playlist_from_name(self, name):
""" """
Helper function to retrieve a playlist from its unique MPD name. Helper function to retrieve a playlist from its unique MPD name.
""" """
return self._mapping.playlist_from_name(name) return self._uri_map.playlist_from_name(name)
def lookup_playlist_name_from_uri(self, uri): def lookup_playlist_name_from_uri(self, uri):
""" """
Helper function to retrieve the unique MPD playlist name from its uri. Helper function to retrieve the unique MPD playlist name from its uri.
""" """
return self._mapping.playlist_name_from_uri(uri) return self._uri_map.playlist_name_from_uri(uri)
def browse(self, path, recursive=True, lookup=True): def browse(self, path, recursive=True, lookup=True):
""" """
@ -273,7 +274,7 @@ class MpdContext(object):
path_parts = re.findall(r'[^/]+', path or '') path_parts = re.findall(r'[^/]+', path or '')
root_path = '/'.join([''] + path_parts) root_path = '/'.join([''] + path_parts)
uri = self._mapping.uri_from_name(root_path) uri = self._uri_map.uri_from_name(root_path)
if uri is None: if uri is None:
for part in path_parts: for part in path_parts:
for ref in self.core.library.browse(uri).get(): for ref in self.core.library.browse(uri).get():
@ -282,7 +283,7 @@ class MpdContext(object):
break break
else: else:
raise exceptions.MpdNoExistError('Not found') raise exceptions.MpdNoExistError('Not found')
root_path = self._mapping.insert(root_path, uri) root_path = self._uri_map.insert(root_path, uri)
if recursive: if recursive:
yield (root_path, None) yield (root_path, None)
@ -292,7 +293,7 @@ class MpdContext(object):
base_path, future = path_and_futures.pop() base_path, future = path_and_futures.pop()
for ref in future.get(): for ref in future.get():
path = '/'.join([base_path, ref.name.replace('/', '')]) path = '/'.join([base_path, ref.name.replace('/', '')])
path = self._mapping.insert(path, ref.uri) path = self._uri_map.insert(path, ref.uri)
if ref.type == ref.TRACK: if ref.type == ref.TRACK:
if lookup: if lookup:
@ -305,6 +306,7 @@ class MpdContext(object):
path_and_futures.append( path_and_futures.append(
(path, self.core.library.browse(ref.uri))) (path, self.core.library.browse(ref.uri)))
class MpdUriMapper(object): class MpdUriMapper(object):
""" """
Maintains the mappings between uniquified MPD names and URIs. Maintains the mappings between uniquified MPD names and URIs.
@ -379,4 +381,4 @@ class MpdUriMapper(object):
""" """
if uri not in self._name_from_uri: if uri not in self._name_from_uri:
self.refresh_playlists_mapping() self.refresh_playlists_mapping()
return self._name_from_uri[uri] return self._name_from_uri[uri]

View File

@ -18,10 +18,10 @@ class MpdSession(network.LineProtocol):
encoding = protocol.ENCODING encoding = protocol.ENCODING
delimiter = r'\r?\n' delimiter = r'\r?\n'
def __init__(self, connection, config=None, core=None): def __init__(self, connection, config=None, core=None, uri_map=None):
super(MpdSession, self).__init__(connection) super(MpdSession, self).__init__(connection)
self.dispatcher = dispatcher.MpdDispatcher( self.dispatcher = dispatcher.MpdDispatcher(
session=self, config=config, core=core) session=self, config=config, core=core, uri_map=uri_map)
def on_start(self): def on_start(self):
logger.info('New MPD connection from [%s]:%s', self.host, self.port) logger.info('New MPD connection from [%s]:%s', self.host, self.port)