Skip to content
Snippets Groups Projects
Commit 162dd1eb authored by Alexander Cherniuk's avatar Alexander Cherniuk
Browse files

Minor fixes for the command system dispatching

parent 09e1742b
No related branches found
No related tags found
No related merge requests found
...@@ -28,63 +28,63 @@ class Dispatcher(type): ...@@ -28,63 +28,63 @@ class Dispatcher(type):
commands = {} commands = {}
@classmethod @classmethod
def register_host(klass, host): def register_host(cls, host):
klass.containers[host] = [] cls.containers[host] = []
@classmethod @classmethod
def register_container(klass, container): def register_container(cls, container):
for host in container.HOSTS: for host in container.HOSTS:
klass.containers[host].append(container) cls.containers[host].append(container)
@classmethod @classmethod
def register_commands(klass, container): def register_commands(cls, container):
klass.commands[container] = {} cls.commands[container] = {}
for command in klass.traverse_commands(container): for command in cls.traverse_commands(container):
for name in command.names: for name in command.names:
klass.commands[container][name] = command cls.commands[container][name] = command
@classmethod @classmethod
def get_command(klass, host, name): def get_command(cls, host, name):
for container in klass.containers[host]: for container in cls.containers[host]:
command = klass.commands[container].get(name) command = cls.commands[container].get(name)
if command: if command:
return command return command
@classmethod @classmethod
def list_commands(klass, host): def list_commands(cls, host):
for container in klass.containers[host]: for container in cls.containers[host]:
commands = klass.commands[container] commands = cls.commands[container]
for name, command in commands.iteritems(): for name, command in commands.iteritems():
yield name, command yield name, command
@classmethod @classmethod
def traverse_commands(klass, container): def traverse_commands(cls, container):
for name in dir(container): for name in dir(container):
attribute = getattr(container, name) attribute = getattr(container, name)
if klass.is_command(attribute): if cls.is_command(attribute):
yield attribute yield attribute
@staticmethod @staticmethod
def is_root(ns): def is_root(ns):
meta = ns.get('__metaclass__', NoneType) metaclass = ns.get('__metaclass__', NoneType)
return issubclass(meta, Dispatcher) return issubclass(metaclass, Dispatcher)
@staticmethod @staticmethod
def is_command(attribute): def is_command(attribute):
name = attribute.__class__.__name__ from framework import Command
return name == 'Command' return isinstance(attribute, Command)
class HostDispatcher(Dispatcher): class HostDispatcher(Dispatcher):
def __init__(klass, name, bases, ns): def __init__(self, name, bases, ns):
if not Dispatcher.is_root(ns): if not Dispatcher.is_root(ns):
HostDispatcher.register_host(klass) HostDispatcher.register_host(self)
super(HostDispatcher, klass).__init__(name, bases, ns) super(HostDispatcher, self).__init__(name, bases, ns)
class ContainerDispatcher(Dispatcher): class ContainerDispatcher(Dispatcher):
def __init__(klass, name, bases, ns): def __init__(self, name, bases, ns):
if not Dispatcher.is_root(ns): if not Dispatcher.is_root(ns):
ContainerDispatcher.register_container(klass) ContainerDispatcher.register_container(self)
ContainerDispatcher.register_commands(klass) ContainerDispatcher.register_commands(self)
super(ContainerDispatcher, klass).__init__(name, bases, ns) super(ContainerDispatcher, self).__init__(name, bases, ns)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment