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.
"""
IS_COMMAND_PROCESSOR = True
DISPATCH = True
HOSTED_BY = ChatCommands, PrivateChatCommands, GroupChatCommands
@command
@ -52,7 +52,7 @@ class CustomChatCommands(ChatCommands):
only to a chat.
"""
IS_COMMAND_PROCESSOR = True
DISPATCH = True
HOSTED_BY = ChatCommands
@command
@ -66,7 +66,7 @@ class CustomPrivateChatCommands(PrivateChatCommands):
available only to a private chat.
"""
IS_COMMAND_PROCESSOR = True
DISPATCH = True
HOSTED_BY = PrivateChatCommands
@command
@ -80,7 +80,7 @@ class CustomGroupChatCommands(GroupChatCommands):
available only to a group chat.
"""
IS_COMMAND_PROCESSOR = True
DISPATCH = True
HOSTED_BY = GroupChatCommands
@command

View File

@ -201,8 +201,8 @@ class Dispatcher(type):
@classmethod
def is_suitable(cls, proc, dct):
is_not_root = dct.get('__metaclass__') is not cls
is_processor = bool(dct.get('IS_COMMAND_PROCESSOR'))
return is_not_root and is_processor
to_be_dispatched = bool(dct.get('DISPATCH'))
return is_not_root and to_be_dispatched
@classmethod
def check_if_dispatchable(cls, bases, dct):
@ -231,18 +231,18 @@ class Dispatcher(type):
@classmethod
def register_processor(cls, proc):
cls.table[proc] = {}
inherited = proc.__dict__.get('INHERITED')
inherit = proc.__dict__.get('INHERIT')
if 'HOSTED_BY' in proc.__dict__:
cls.register_adhocs(proc)
commands = cls.traverse_commands(proc, inherited)
commands = cls.traverse_commands(proc, inherit)
cls.register_commands(proc, commands)
@classmethod
def sanitize_names(cls, proc):
inherited = proc.__dict__.get('INHERITED')
commands = cls.traverse_commands(proc, inherited)
inherit = proc.__dict__.get('INHERIT')
commands = cls.traverse_commands(proc, inherit)
for key, command in commands:
if not proc.SAFE_NAME_SCAN_PATTERN.match(key):
setattr(proc, proc.SAFE_NAME_SUBS_PATTERN % key, command)
@ -252,8 +252,8 @@ class Dispatcher(type):
pass
@classmethod
def traverse_commands(cls, proc, inherited=True):
keys = dir(proc) if inherited else proc.__dict__.iterkeys()
def traverse_commands(cls, proc, inherit=True):
keys = dir(proc) if inherit else proc.__dict__.iterkeys()
for key in keys:
value = getattr(proc, key)
if isinstance(value, Command):
@ -295,8 +295,8 @@ class Dispatcher(type):
commands = dict(cls.traverse_commands(proc.DISPATCHED_BY))
if proc.DISPATCHED_BY in cls.hosted:
for adhoc in cls.hosted[proc.DISPATCHED_BY]:
inherited = adhoc.__dict__.get('INHERITED')
commands.update(dict(cls.traverse_commands(adhoc, inherited)))
inherit = adhoc.__dict__.get('INHERIT')
commands.update(dict(cls.traverse_commands(adhoc, inherit)))
return commands.values()
class CommandProcessor(object):
@ -311,8 +311,7 @@ class CommandProcessor(object):
to an object you are adding commands to.
Your subclass, which will contain commands should define in its body
IS_COMMAND_PROCESSOR = True in order to be included in the dispatching
table.
DISPATCH = True in order to be included in the dispatching table.
Every class you will drop the processor in should define DISPATCHED_BY set
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
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
by-default because it leads to unpredictable consequences when used in adhoc
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.
"""
IS_COMMAND_PROCESSOR = True
INHERITED = True
DISPATCH = True
INHERIT = True
@command
def ping(self):
@ -111,8 +111,8 @@ class PrivateChatCommands(CommonCommands):
self is set to an instance of PrivateChatControl when command is being called.
"""
IS_COMMAND_PROCESSOR = True
INHERITED = True
DISPATCH = True
INHERIT = True
class GroupChatCommands(CommonCommands):
"""
@ -121,8 +121,8 @@ class GroupChatCommands(CommonCommands):
self is set to an instance of GroupchatControl when command is being called.
"""
IS_COMMAND_PROCESSOR = True
INHERITED = True
DISPATCH = True
INHERIT = True
@command(raw=True)
def nick(self, new_nick):