Made some cosmetic API changes to the command system

This commit is contained in:
red-agent 2009-09-14 22:04:49 +03:00
parent 0a1ef72c0d
commit 7bec311cfb
3 changed files with 22 additions and 23 deletions

View file

@ -29,7 +29,7 @@ class CustomCommonCommands(ChatCommands, PrivateChatCommands, GroupChatCommands)
here will be available to all of them. here will be available to all of them.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
HOSTED_BY = ChatCommands, PrivateChatCommands, GroupChatCommands HOSTED_BY = ChatCommands, PrivateChatCommands, GroupChatCommands
@command @command
@ -52,7 +52,7 @@ class CustomChatCommands(ChatCommands):
only to a chat. only to a chat.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
HOSTED_BY = ChatCommands HOSTED_BY = ChatCommands
@command @command
@ -66,7 +66,7 @@ class CustomPrivateChatCommands(PrivateChatCommands):
available only to a private chat. available only to a private chat.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
HOSTED_BY = PrivateChatCommands HOSTED_BY = PrivateChatCommands
@command @command
@ -80,7 +80,7 @@ class CustomGroupChatCommands(GroupChatCommands):
available only to a group chat. available only to a group chat.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
HOSTED_BY = GroupChatCommands HOSTED_BY = GroupChatCommands
@command @command

View file

@ -201,8 +201,8 @@ class Dispatcher(type):
@classmethod @classmethod
def is_suitable(cls, proc, dct): def is_suitable(cls, proc, dct):
is_not_root = dct.get('__metaclass__') is not cls is_not_root = dct.get('__metaclass__') is not cls
is_processor = bool(dct.get('IS_COMMAND_PROCESSOR')) to_be_dispatched = bool(dct.get('DISPATCH'))
return is_not_root and is_processor return is_not_root and to_be_dispatched
@classmethod @classmethod
def check_if_dispatchable(cls, bases, dct): def check_if_dispatchable(cls, bases, dct):
@ -231,18 +231,18 @@ class Dispatcher(type):
@classmethod @classmethod
def register_processor(cls, proc): def register_processor(cls, proc):
cls.table[proc] = {} cls.table[proc] = {}
inherited = proc.__dict__.get('INHERITED') inherit = proc.__dict__.get('INHERIT')
if 'HOSTED_BY' in proc.__dict__: if 'HOSTED_BY' in proc.__dict__:
cls.register_adhocs(proc) cls.register_adhocs(proc)
commands = cls.traverse_commands(proc, inherited) commands = cls.traverse_commands(proc, inherit)
cls.register_commands(proc, commands) cls.register_commands(proc, commands)
@classmethod @classmethod
def sanitize_names(cls, proc): def sanitize_names(cls, proc):
inherited = proc.__dict__.get('INHERITED') inherit = proc.__dict__.get('INHERIT')
commands = cls.traverse_commands(proc, inherited) commands = cls.traverse_commands(proc, inherit)
for key, command in commands: for key, command in commands:
if not proc.SAFE_NAME_SCAN_PATTERN.match(key): if not proc.SAFE_NAME_SCAN_PATTERN.match(key):
setattr(proc, proc.SAFE_NAME_SUBS_PATTERN % key, command) setattr(proc, proc.SAFE_NAME_SUBS_PATTERN % key, command)
@ -252,8 +252,8 @@ class Dispatcher(type):
pass pass
@classmethod @classmethod
def traverse_commands(cls, proc, inherited=True): def traverse_commands(cls, proc, inherit=True):
keys = dir(proc) if inherited else proc.__dict__.iterkeys() keys = dir(proc) if inherit else proc.__dict__.iterkeys()
for key in keys: for key in keys:
value = getattr(proc, key) value = getattr(proc, key)
if isinstance(value, Command): if isinstance(value, Command):
@ -295,8 +295,8 @@ class Dispatcher(type):
commands = dict(cls.traverse_commands(proc.DISPATCHED_BY)) commands = dict(cls.traverse_commands(proc.DISPATCHED_BY))
if proc.DISPATCHED_BY in cls.hosted: if proc.DISPATCHED_BY in cls.hosted:
for adhoc in cls.hosted[proc.DISPATCHED_BY]: for adhoc in cls.hosted[proc.DISPATCHED_BY]:
inherited = adhoc.__dict__.get('INHERITED') inherit = adhoc.__dict__.get('INHERIT')
commands.update(dict(cls.traverse_commands(adhoc, inherited))) commands.update(dict(cls.traverse_commands(adhoc, inherit)))
return commands.values() return commands.values()
class CommandProcessor(object): class CommandProcessor(object):
@ -311,8 +311,7 @@ class CommandProcessor(object):
to an object you are adding commands to. to an object you are adding commands to.
Your subclass, which will contain commands should define in its body Your subclass, which will contain commands should define in its body
IS_COMMAND_PROCESSOR = True in order to be included in the dispatching DISPATCH = True in order to be included in the dispatching table.
table.
Every class you will drop the processor in should define DISPATCHED_BY set Every class you will drop the processor in should define DISPATCHED_BY set
to the same processor you are inheriting from. to the same processor you are inheriting from.
@ -326,7 +325,7 @@ class CommandProcessor(object):
whatever includes the host) you need to inherit you processor from the host whatever includes the host) you need to inherit you processor from the host
and set HOSTED_BY to that host. and set HOSTED_BY to that host.
INHERITED controls whether commands inherited from base classes (which could INHERIT controls whether commands inherited from base classes (which could
include other processors) will be registered or not. This is disabled include other processors) will be registered or not. This is disabled
by-default because it leads to unpredictable consequences when used in adhoc by-default because it leads to unpredictable consequences when used in adhoc
processors which inherit from more then one processor or has such processors processors which inherit from more then one processor or has such processors

View file

@ -92,8 +92,8 @@ class ChatCommands(CommonCommands):
an instance of ChatControl when command is being called. an instance of ChatControl when command is being called.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
INHERITED = True INHERIT = True
@command @command
def ping(self): def ping(self):
@ -111,8 +111,8 @@ class PrivateChatCommands(CommonCommands):
self is set to an instance of PrivateChatControl when command is being called. self is set to an instance of PrivateChatControl when command is being called.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
INHERITED = True INHERIT = True
class GroupChatCommands(CommonCommands): class GroupChatCommands(CommonCommands):
""" """
@ -121,8 +121,8 @@ class GroupChatCommands(CommonCommands):
self is set to an instance of GroupchatControl when command is being called. self is set to an instance of GroupchatControl when command is being called.
""" """
IS_COMMAND_PROCESSOR = True DISPATCH = True
INHERITED = True INHERIT = True
@command(raw=True) @command(raw=True)
def nick(self, new_nick): def nick(self, new_nick):