From d3db5c4fe1eeee88edbf7d1bbf998ea684bfe12e Mon Sep 17 00:00:00 2001 From: Thomas Adamcik Date: Wed, 22 Jan 2014 22:44:40 +0100 Subject: [PATCH] mpd: Install new commands helpers in _call_handler This means we now tokenize every request, and then try in call the appropriate handler. If none is found we simply fall back to the old handlers. --- mopidy/mpd/dispatcher.py | 11 ++++++++++- mopidy/mpd/protocol/__init__.py | 9 ++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/mopidy/mpd/dispatcher.py b/mopidy/mpd/dispatcher.py index 69b5e839..8a3602d4 100644 --- a/mopidy/mpd/dispatcher.py +++ b/mopidy/mpd/dispatcher.py @@ -5,7 +5,7 @@ import re import pykka -from mopidy.mpd import exceptions, protocol +from mopidy.mpd import exceptions, protocol, tokenize logger = logging.getLogger(__name__) @@ -164,6 +164,15 @@ class MpdDispatcher(object): raise exceptions.MpdSystemError(e) def _call_handler(self, request): + tokens = tokenize.split(request) + try: + return protocol.commands.call(tokens, context=self.context) + except exceptions.MpdAckError as exc: + if exc.command is None: + exc.command = tokens[0] + raise + except LookupError: + pass # Command has not been converted, i.e. fallback... (command_name, handler, kwargs) = self._find_handler(request) try: return handler(self.context, **kwargs) diff --git a/mopidy/mpd/protocol/__init__.py b/mopidy/mpd/protocol/__init__.py index 8aa5f3d1..03ac6dda 100644 --- a/mopidy/mpd/protocol/__init__.py +++ b/mopidy/mpd/protocol/__init__.py @@ -150,7 +150,10 @@ class Commands(object): def call(self, args, context=None): if not args: raise TypeError('No args provided') - command = args.pop(0) - if command not in self.handlers: + if args[0] not in self.handlers: raise LookupError('Unknown command') - return self.handlers[command](context, *args) + return self.handlers[args[0]](context, *args[1:]) + + +#: Global instance to install commands into +commands = Commands()