mpd: Store auth required and if command should be listed

This commit is contained in:
Thomas Adamcik 2014-01-22 23:05:13 +01:00
parent d3db5c4fe1
commit eb85f92d96
2 changed files with 24 additions and 4 deletions

View File

@ -112,10 +112,10 @@ class Commands(object):
def __init__(self):
self.handlers = {}
def add(self, command, **validators):
def add(self, name, auth_required=True, list_command=True, **validators):
def wrapper(func):
if command in self.handlers:
raise Exception('%s already registered' % command)
if name in self.handlers:
raise Exception('%s already registered' % name)
args, varargs, keywords, defaults = inspect.getargspec(func)
defaults = dict(zip(args[-len(defaults or []):], defaults or []))
@ -143,7 +143,9 @@ class Commands(object):
callargs[key] = validators[key](value)
return func(**callargs)
self.handlers[command] = validate
validate.auth_required = auth_required
validate.list_command = list_command
self.handlers[name] = validate
return func
return wrapper

View File

@ -185,3 +185,21 @@ class TestCommands(unittest.TestCase):
with self.assertRaises(TypeError):
func = lambda context: True
self.commands.add('bar', context=lambda v: v)(func)
def test_auth_required_gets_stored(self):
func1 = lambda context: context
func2 = lambda context: context
self.commands.add('foo')(func1)
self.commands.add('bar', auth_required=False)(func2)
self.assertTrue(self.commands.handlers['foo'].auth_required)
self.assertFalse(self.commands.handlers['bar'].auth_required)
def test_list_command_gets_stored(self):
func1 = lambda context: context
func2 = lambda context: context
self.commands.add('foo')(func1)
self.commands.add('bar', list_command=False)(func2)
self.assertTrue(self.commands.handlers['foo'].list_command)
self.assertFalse(self.commands.handlers['bar'].list_command)