Use a decorator to register the request handlers
This commit is contained in:
parent
8c7c2a65c9
commit
69cee7bd08
@ -5,42 +5,51 @@ from mopidy import settings
|
|||||||
|
|
||||||
logger = logging.getLogger('handler')
|
logger = logging.getLogger('handler')
|
||||||
|
|
||||||
class MpdHandler(object):
|
global _request_handlers
|
||||||
def __init__(self):
|
_request_handlers = {}
|
||||||
self.request_handlers = [
|
|
||||||
(r'^currentsong$', self._currentsong),
|
|
||||||
(r'^listplaylists$', self._listplaylists),
|
|
||||||
(r'^lsinfo( "(?P<uri>[^"]*)")*$', self._lsinfo),
|
|
||||||
(r'^ping$', self._ping),
|
|
||||||
(r'^plchanges "(?P<version>\d+)"$', self._plchanges),
|
|
||||||
(r'^status$', self._status),
|
|
||||||
]
|
|
||||||
|
|
||||||
|
def register(pattern):
|
||||||
|
def decorator(func):
|
||||||
|
global _request_handlers
|
||||||
|
if pattern in _request_handlers:
|
||||||
|
raise ValueError(u'Tried to redefine handler for %s with %s' % (
|
||||||
|
pattern, func))
|
||||||
|
_request_handlers[pattern] = func
|
||||||
|
return func
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
class MpdHandler(object):
|
||||||
def handle_request(self, request):
|
def handle_request(self, request):
|
||||||
for request_pattern, request_handling_method in self.request_handlers:
|
for pattern in _request_handlers:
|
||||||
matches = re.match(request_pattern, request)
|
matches = re.match(pattern, request)
|
||||||
if matches is not None:
|
if matches is not None:
|
||||||
groups = matches.groupdict()
|
groups = matches.groupdict()
|
||||||
return request_handling_method(**groups)
|
return _request_handlers[pattern](self, **groups)
|
||||||
logger.warning(u'Unhandled request: %s', request)
|
logger.warning(u'Unhandled request: %s', request)
|
||||||
|
|
||||||
|
@register(r'^currentsong$')
|
||||||
def _currentsong(self):
|
def _currentsong(self):
|
||||||
return None # TODO
|
return None # TODO
|
||||||
|
|
||||||
|
@register(r'^listplaylists$')
|
||||||
def _listplaylists(self):
|
def _listplaylists(self):
|
||||||
return None # TODO
|
return None # TODO
|
||||||
|
|
||||||
|
@register(r'^lsinfo( "(?P<uri>[^"]*)")*$')
|
||||||
def _lsinfo(self, uri):
|
def _lsinfo(self, uri):
|
||||||
if uri == u'/':
|
if uri == u'/':
|
||||||
return self._listplaylists()
|
return self._listplaylists()
|
||||||
return None # TODO
|
return None # TODO
|
||||||
|
|
||||||
|
@register(r'^ping$')
|
||||||
def _ping(self):
|
def _ping(self):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@register(r'^plchanges "(?P<version>\d+)"$')
|
||||||
def _plchanges(self, version):
|
def _plchanges(self, version):
|
||||||
return None # TODO
|
return None # TODO
|
||||||
|
|
||||||
|
@register(r'^status$')
|
||||||
def _status(self):
|
def _status(self):
|
||||||
# TODO
|
# TODO
|
||||||
return [
|
return [
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user