From f69148c57224e46c3e7a23e366a652a8f195318b Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 16 Oct 2012 15:24:47 +0200 Subject: [PATCH] Move loading of MPD protocol modules into a function (#211) --- mopidy/frontends/mpd/dispatcher.py | 21 ++++++++------------- mopidy/frontends/mpd/protocol/__init__.py | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/mopidy/frontends/mpd/dispatcher.py b/mopidy/frontends/mpd/dispatcher.py index 24db6a7a..d7ba8cdf 100644 --- a/mopidy/frontends/mpd/dispatcher.py +++ b/mopidy/frontends/mpd/dispatcher.py @@ -4,19 +4,13 @@ import re from pykka import ActorDeadError from mopidy import settings -from mopidy.frontends.mpd import exceptions -from mopidy.frontends.mpd.protocol import mpd_commands, request_handlers -# Do not remove the following import. The protocol modules must be imported to -# get them registered as request handlers. -# pylint: disable = W0611 -from mopidy.frontends.mpd.protocol import ( - audio_output, command_list, connection, current_playlist, empty, music_db, - playback, reflection, status, stickers, stored_playlists) -# pylint: enable = W0611 +from mopidy.frontends.mpd import exceptions, protocol from mopidy.utils import flatten logger = logging.getLogger('mopidy.frontends.mpd.dispatcher') +protocol.load_protocol_modules() + class MpdDispatcher(object): """ @@ -92,7 +86,7 @@ class MpdDispatcher(object): else: command_name = request.split(' ')[0] command_names_not_requiring_auth = [ - command.name for command in mpd_commands + command.name for command in protocol.mpd_commands if not command.auth_required] if command_name in command_names_not_requiring_auth: return self._call_next_filter(request, response, filter_chain) @@ -172,12 +166,13 @@ class MpdDispatcher(object): return handler(self.context, **kwargs) def _find_handler(self, request): - for pattern in request_handlers: + for pattern in protocol.request_handlers: matches = re.match(pattern, request) if matches is not None: - return (request_handlers[pattern], matches.groupdict()) + return ( + protocol.request_handlers[pattern], matches.groupdict()) command_name = request.split(' ')[0] - if command_name in [command.name for command in mpd_commands]: + if command_name in [command.name for command in protocol.mpd_commands]: raise exceptions.MpdArgError( u'incorrect arguments', command=command_name) raise exceptions.MpdUnknownCommand(command=command_name) diff --git a/mopidy/frontends/mpd/protocol/__init__.py b/mopidy/frontends/mpd/protocol/__init__.py index 590a8ef4..66c8a84a 100644 --- a/mopidy/frontends/mpd/protocol/__init__.py +++ b/mopidy/frontends/mpd/protocol/__init__.py @@ -24,9 +24,10 @@ VERSION = u'0.16.0' MpdCommand = namedtuple('MpdCommand', ['name', 'auth_required']) -#: List of all available commands, represented as :class:`MpdCommand` objects. +#: Set of all available commands, represented as :class:`MpdCommand` objects. mpd_commands = set() +#: Map between request matchers and request handler functions. request_handlers = {} @@ -61,3 +62,15 @@ def handle_request(pattern, auth_required=True): pattern, func.__doc__ or '') return func return decorator + + +def load_protocol_modules(): + """ + The protocol modules must be imported to get them registered in + :attr:`request_handlers` and :attr:`mpd_commands`. + """ + # pylint: disable = W0611 + from . import ( # noqa + audio_output, command_list, connection, current_playlist, empty, + music_db, playback, reflection, status, stickers, stored_playlists) + # pylint: enable = W0611