mpd: Remove old command handlers
This commit is contained in:
parent
dc8d311bc3
commit
86f5602023
@ -90,17 +90,7 @@ class MpdDispatcher(object):
|
||||
else:
|
||||
command_name = request.split(' ')[0]
|
||||
command = protocol.commands.handlers.get(command_name)
|
||||
if command:
|
||||
if command.auth_required:
|
||||
raise exceptions.MpdPermissionError(command=command_name)
|
||||
else:
|
||||
return self._call_next_filter(
|
||||
request, response, filter_chain)
|
||||
|
||||
# TODO: remove
|
||||
command_names_not_requiring_auth = [
|
||||
c.name for c in protocol.mpd_commands if not c.auth_required]
|
||||
if command_name in command_names_not_requiring_auth:
|
||||
if command and not command.auth_required:
|
||||
return self._call_next_filter(request, response, filter_chain)
|
||||
else:
|
||||
raise exceptions.MpdPermissionError(command=command_name)
|
||||
@ -181,28 +171,7 @@ class MpdDispatcher(object):
|
||||
exc.command = tokens[0]
|
||||
raise
|
||||
except LookupError:
|
||||
pass # Command has not been converted, i.e. fallback...
|
||||
|
||||
request = request.decode('string_escape')
|
||||
(command_name, handler, kwargs) = self._find_handler(request)
|
||||
try:
|
||||
return handler(self.context, **kwargs)
|
||||
except exceptions.MpdAckError as exc:
|
||||
if exc.command is None:
|
||||
exc.command = command_name
|
||||
raise
|
||||
|
||||
def _find_handler(self, request):
|
||||
command_name = request.split(' ')[0]
|
||||
for pattern in protocol.request_handlers:
|
||||
matches = re.match(pattern, request)
|
||||
if matches is not None:
|
||||
handler = protocol.request_handlers[pattern]
|
||||
return (command_name, handler, matches.groupdict())
|
||||
if command_name in [command.name for command in protocol.mpd_commands]:
|
||||
raise exceptions.MpdArgError(
|
||||
'incorrect arguments', command=command_name)
|
||||
raise exceptions.MpdUnknownCommand(command=command_name)
|
||||
raise exceptions.MpdUnknownCommand(command=tokens[0])
|
||||
|
||||
def _format_response(self, response):
|
||||
formatted_response = []
|
||||
|
||||
@ -12,11 +12,7 @@ implement our own MPD server which is compatible with the numerous existing
|
||||
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import collections
|
||||
import inspect
|
||||
import re
|
||||
|
||||
from mopidy.utils import formatting
|
||||
|
||||
#: The MPD protocol uses UTF-8 for encoding all data.
|
||||
ENCODING = 'UTF-8'
|
||||
@ -27,63 +23,6 @@ LINE_TERMINATOR = '\n'
|
||||
#: The MPD protocol version is 0.17.0.
|
||||
VERSION = '0.17.0'
|
||||
|
||||
MpdCommand = collections.namedtuple('MpdCommand', ['name', 'auth_required'])
|
||||
|
||||
#: Set of all available commands, represented as :class:`MpdCommand` objects.
|
||||
mpd_commands = set()
|
||||
|
||||
#: Map between request matchers and request handler functions.
|
||||
request_handlers = {}
|
||||
|
||||
|
||||
def handle_request(pattern, auth_required=True):
|
||||
"""
|
||||
Decorator for connecting command handlers to command requests.
|
||||
|
||||
If you use named groups in the pattern, the decorated method will get the
|
||||
groups as keyword arguments. If the group is optional, remember to give the
|
||||
argument a default value.
|
||||
|
||||
For example, if the command is ``do that thing`` the ``what`` argument will
|
||||
be ``this thing``::
|
||||
|
||||
@handle_request('do\ (?P<what>.+)$')
|
||||
def do(what):
|
||||
...
|
||||
|
||||
Note that the patterns are compiled with the :attr:`re.VERBOSE` flag. Thus,
|
||||
you must escape any space characters you want to match, but you're also
|
||||
free to add non-escaped whitespace to format the pattern for easier
|
||||
reading.
|
||||
|
||||
:param pattern: regexp pattern for matching commands
|
||||
:type pattern: string
|
||||
"""
|
||||
def decorator(func):
|
||||
match = re.search('([a-z_]+)', pattern)
|
||||
if match is not None:
|
||||
mpd_commands.add(
|
||||
MpdCommand(name=match.group(), auth_required=auth_required))
|
||||
compiled_pattern = re.compile(pattern, flags=(re.UNICODE | re.VERBOSE))
|
||||
if compiled_pattern in request_handlers:
|
||||
raise ValueError('Tried to redefine handler for %s with %s' % (
|
||||
pattern, func))
|
||||
request_handlers[compiled_pattern] = func
|
||||
func.__doc__ = """
|
||||
*Pattern:*
|
||||
|
||||
.. code-block:: text
|
||||
|
||||
%(pattern)s
|
||||
|
||||
%(docs)s
|
||||
""" % {
|
||||
'pattern': formatting.indent(pattern, places=8, singles=True),
|
||||
'docs': func.__doc__ or '',
|
||||
}
|
||||
return func
|
||||
return decorator
|
||||
|
||||
|
||||
def load_protocol_modules():
|
||||
"""
|
||||
@ -92,8 +31,7 @@ def load_protocol_modules():
|
||||
"""
|
||||
from . import ( # noqa
|
||||
audio_output, channels, command_list, connection, current_playlist,
|
||||
empty, music_db, playback, reflection, status, stickers,
|
||||
stored_playlists)
|
||||
music_db, playback, reflection, status, stickers, stored_playlists)
|
||||
|
||||
|
||||
def INT(value):
|
||||
|
||||
@ -33,13 +33,6 @@ def commands(context):
|
||||
if context.dispatcher.authenticated or not handler.auth_required:
|
||||
command_names.add(name)
|
||||
|
||||
# TODO: remove
|
||||
if context.dispatcher.authenticated:
|
||||
command_names.update(c.name for c in protocol.mpd_commands)
|
||||
else:
|
||||
command_names.update(c.name for c in protocol.mpd_commands
|
||||
if not c.auth_required)
|
||||
|
||||
return [
|
||||
('command', command_name) for command_name in sorted(command_names)]
|
||||
|
||||
@ -87,11 +80,6 @@ def notcommands(context):
|
||||
if not context.dispatcher.authenticated and handler.auth_required:
|
||||
command_names.add(name)
|
||||
|
||||
# TODO: remove
|
||||
if not context.dispatcher.authenticated:
|
||||
command_names.update(command.name for command in protocol.mpd_commands
|
||||
if command.auth_required)
|
||||
|
||||
return [
|
||||
('command', command_name) for command_name in sorted(command_names)]
|
||||
|
||||
|
||||
@ -8,7 +8,6 @@ from mopidy import core
|
||||
from mopidy.backend import dummy
|
||||
from mopidy.mpd.dispatcher import MpdDispatcher
|
||||
from mopidy.mpd.exceptions import MpdAckError
|
||||
from mopidy.mpd.protocol import request_handlers, handle_request
|
||||
|
||||
|
||||
class MpdDispatcherTest(unittest.TestCase):
|
||||
@ -25,42 +24,15 @@ class MpdDispatcherTest(unittest.TestCase):
|
||||
def tearDown(self):
|
||||
pykka.ActorRegistry.stop_all()
|
||||
|
||||
def test_register_same_pattern_twice_fails(self):
|
||||
func = lambda: None
|
||||
def test_call_handler_for_unknown_command_raises_exception(self):
|
||||
try:
|
||||
handle_request('a pattern')(func)
|
||||
handle_request('a pattern')(func)
|
||||
self.fail('Registering a pattern twice shoulde raise ValueError')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
def test_finding_handler_for_unknown_command_raises_exception(self):
|
||||
try:
|
||||
self.dispatcher._find_handler('an_unknown_command with args')
|
||||
self.dispatcher._call_handler('an_unknown_command with args')
|
||||
self.fail('Should raise exception')
|
||||
except MpdAckError as e:
|
||||
self.assertEqual(
|
||||
e.get_mpd_ack(),
|
||||
'ACK [5@0] {} unknown command "an_unknown_command"')
|
||||
|
||||
def test_find_handler_for_known_command_return_name_handler_and_args(self):
|
||||
expected_handler = lambda x: None
|
||||
request_handlers['known_command (?P<arg1>.+)'] = \
|
||||
expected_handler
|
||||
(name, handler, kwargs) = self.dispatcher._find_handler(
|
||||
'known_command an_arg')
|
||||
self.assertEqual(handler, expected_handler)
|
||||
self.assertEqual('known_command', name)
|
||||
self.assertIn('arg1', kwargs)
|
||||
self.assertEqual(kwargs['arg1'], 'an_arg')
|
||||
|
||||
def test_handling_unknown_request_yields_error(self):
|
||||
result = self.dispatcher.handle_request('an unhandled request')
|
||||
self.assertEqual(result[0], 'ACK [5@0] {} unknown command "an"')
|
||||
|
||||
def test_handling_known_request(self):
|
||||
expected = 'magic'
|
||||
request_handlers['known request'] = lambda x: expected
|
||||
result = self.dispatcher.handle_request('known request')
|
||||
self.assertIn('OK', result)
|
||||
self.assertIn(expected, result)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user