1
0
Fork 0

plugin commands: add numeric priority to enforce execution order

This commit is contained in:
Jan Vcelak 2013-01-23 18:49:45 +01:00
parent fcc0c5c6f4
commit 63558e96c1
2 changed files with 10 additions and 3 deletions

View file

@ -227,7 +227,7 @@ class Plugin(object):
"""
Initialize commands.
"""
self._commands = {}
self._commands = collections.OrderedDict()
self._autoregister_commands()
self._check_commands()
@ -249,14 +249,19 @@ class Plugin(object):
info["custom"] = None
info["set"] = member
info["per_device"] = member._command["per_device"]
info["priority"] = member._command["priority"]
elif "get" in member._command:
info["get"] = member
elif "custom" in member._command:
info["custom"] = member
info["per_device"] = member._command["per_device"]
info["priority"] = member._command["priority"]
self._commands[command_name] = info
# sort commands by priority
self._commands = collections.OrderedDict(sorted(self._commands.iteritems(), key=lambda (name, info): info["priority"]))
def _check_commands(self):
"""
Check if all commands are defined correctly.

View file

@ -17,12 +17,13 @@ __all__ = ["command_set", "command_get", "command_custom"]
# return current_foo
#
def command_set(name, per_device=False):
def command_set(name, per_device=False, priority=0):
def wrapper(method):
method._command = {
"set": True,
"name": name,
"per_device": per_device,
"priority": priority,
}
return method
@ -37,12 +38,13 @@ def command_get(name):
return method
return wrapper
def command_custom(name, per_device=False):
def command_custom(name, per_device=False, priority=0):
def wrapper(method):
method._command = {
"custom": True,
"name": name,
"per_device": per_device,
"priority": priority,
}
return method
return wrapper