mpd: Return command name in find handler.

This commit is contained in:
Thomas Adamcik 2014-01-21 20:40:49 +01:00
parent 455f3dd403
commit 335cf4e612
2 changed files with 8 additions and 7 deletions

View File

@ -164,21 +164,21 @@ class MpdDispatcher(object):
raise exceptions.MpdSystemError(e)
def _call_handler(self, request):
(handler, kwargs) = self._find_handler(request)
(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 = handler.__name__.split('__', 1)[0]
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:
return (
protocol.request_handlers[pattern], matches.groupdict())
command_name = request.split(' ')[0]
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)

View File

@ -43,13 +43,14 @@ class MpdDispatcherTest(unittest.TestCase):
e.get_mpd_ack(),
'ACK [5@0] {} unknown command "an_unknown_command"')
def test_find_handler_for_known_command_returns_handler_and_kwargs(self):
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
(handler, kwargs) = self.dispatcher._find_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')