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

View File

@ -21,7 +21,7 @@ class MpdDispatcher(object):
_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.authenticated = False
self.command_list_receiving = False
@ -29,7 +29,7 @@ class MpdDispatcher(object):
self.command_list = []
self.command_list_index = None
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):
"""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.
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.session = session
if config is not None:
@ -237,19 +238,19 @@ class MpdContext(object):
self.core = core
self.events = set()
self.subscriptions = set()
self._mapping = MpdUriMapper(core)
self._uri_map = uri_map
def lookup_playlist_from_name(self, 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):
"""
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):
"""
@ -273,7 +274,7 @@ class MpdContext(object):
path_parts = re.findall(r'[^/]+', path or '')
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:
for part in path_parts:
for ref in self.core.library.browse(uri).get():
@ -282,7 +283,7 @@ class MpdContext(object):
break
else:
raise exceptions.MpdNoExistError('Not found')
root_path = self._mapping.insert(root_path, uri)
root_path = self._uri_map.insert(root_path, uri)
if recursive:
yield (root_path, None)
@ -292,7 +293,7 @@ class MpdContext(object):
base_path, future = path_and_futures.pop()
for ref in future.get():
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 lookup:
@ -305,6 +306,7 @@ class MpdContext(object):
path_and_futures.append(
(path, self.core.library.browse(ref.uri)))
class MpdUriMapper(object):
"""
Maintains the mappings between uniquified MPD names and URIs.
@ -379,4 +381,4 @@ class MpdUriMapper(object):
"""
if uri not in self._name_from_uri:
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
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)
self.dispatcher = dispatcher.MpdDispatcher(
session=self, config=config, core=core)
session=self, config=config, core=core, uri_map=uri_map)
def on_start(self):
logger.info('New MPD connection from [%s]:%s', self.host, self.port)