2010-03-18 07:32:47 +01:00
|
|
|
# Copyright (C) 2009-2010 Alexander Cherniuk <ts33kr@gmail.com>
|
2009-10-02 22:57:11 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Provides a tiny framework with simple, yet powerful and extensible
|
2017-03-07 16:36:43 +01:00
|
|
|
architecture to implement commands in a straight and flexible,
|
2010-02-26 11:35:09 +01:00
|
|
|
declarative way.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
from types import FunctionType
|
2010-08-04 21:31:47 +02:00
|
|
|
from inspect import getargspec, getdoc
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2013-01-01 21:06:16 +01:00
|
|
|
from .dispatcher import Host, Container
|
|
|
|
from .dispatcher import get_command, list_commands
|
2013-01-02 13:54:02 +01:00
|
|
|
from .mapping import parse_arguments, adapt_arguments
|
2013-01-01 21:06:16 +01:00
|
|
|
from .errors import DefinitionError, CommandError, NoCommandError
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2013-01-27 19:51:11 +01:00
|
|
|
class CommandHost(metaclass=Host):
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Command host is a hub between numerous command processors and
|
|
|
|
command containers. Aimed to participate in a dispatching process in
|
|
|
|
order to provide clean and transparent architecture.
|
2010-08-05 20:47:46 +02:00
|
|
|
|
|
|
|
The AUTOMATIC class variable, which must be defined by a command
|
|
|
|
host, specifies whether the command host should be automatically
|
|
|
|
dispatched and enabled by the dispatcher or not.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2010-08-05 20:47:46 +02:00
|
|
|
__metaclass__ = Host
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2013-01-27 19:51:11 +01:00
|
|
|
class CommandContainer(metaclass=Container):
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Command container is an entity which holds defined commands,
|
|
|
|
allowing them to be dispatched and proccessed correctly. Each
|
|
|
|
command container may be bound to a one or more command hosts.
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2010-08-05 20:47:46 +02:00
|
|
|
The AUTOMATIC class variable, which must be defined by a command
|
|
|
|
processor, specifies whether the command processor should be
|
|
|
|
automatically dispatched and enabled by the dispatcher or not.
|
|
|
|
|
|
|
|
Bounding is controlled by the HOSTS class variable, which must be
|
|
|
|
defined by the command container. This variable should contain a
|
|
|
|
sequence of hosts to bound to, as a tuple or list.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2010-08-05 20:47:46 +02:00
|
|
|
__metaclass__ = Container
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
class CommandProcessor(object):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Command processor is an immediate command emitter. It does not
|
|
|
|
participate in the dispatching process directly, but must define a
|
|
|
|
host to bound to.
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
Bounding is controlled by the COMMAND_HOST variable, which must be
|
|
|
|
defined in the body of the command processor. This variable should
|
|
|
|
be set to a specific command host.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# This defines a command prefix (or an initializer), which should
|
2017-03-07 16:36:43 +01:00
|
|
|
# precede a text in order for it to be processed as a command.
|
2009-10-02 22:57:11 +02:00
|
|
|
COMMAND_PREFIX = '/'
|
|
|
|
|
|
|
|
def process_as_command(self, text):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Try to process text as a command. Returns True if it has been
|
|
|
|
processed as a command and False otherwise.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2009-11-03 09:14:23 +01:00
|
|
|
prefix = text.startswith(self.COMMAND_PREFIX)
|
|
|
|
length = len(text) > len(self.COMMAND_PREFIX)
|
|
|
|
if not (prefix and length):
|
2009-10-02 22:57:11 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
body = text[len(self.COMMAND_PREFIX):]
|
|
|
|
body = body.strip()
|
|
|
|
|
|
|
|
parts = body.split(None, 1)
|
|
|
|
name, arguments = parts if len(parts) > 1 else (parts[0], None)
|
|
|
|
|
|
|
|
flag = self.looks_like_command(text, body, name, arguments)
|
|
|
|
if flag is not None:
|
|
|
|
return flag
|
|
|
|
|
|
|
|
self.execute_command(name, arguments)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def execute_command(self, name, arguments):
|
|
|
|
command = self.get_command(name)
|
|
|
|
|
|
|
|
args, opts = parse_arguments(arguments) if arguments else ([], [])
|
|
|
|
args, kwargs = adapt_arguments(command, arguments, args, opts)
|
|
|
|
|
|
|
|
if self.command_preprocessor(command, name, arguments, args, kwargs):
|
|
|
|
return
|
|
|
|
value = command(self, *args, **kwargs)
|
|
|
|
self.command_postprocessor(command, name, arguments, args, kwargs, value)
|
|
|
|
|
|
|
|
def command_preprocessor(self, command, name, arguments, args, kwargs):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Redefine this method in the subclass to execute custom code
|
|
|
|
before command gets executed.
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
If returns True then command execution will be interrupted and
|
|
|
|
command will not be executed.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def command_postprocessor(self, command, name, arguments, args, kwargs, value):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Redefine this method in the subclass to execute custom code
|
|
|
|
after command gets executed.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def looks_like_command(self, text, body, name, arguments):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
This hook is being called before any processing, but after it
|
|
|
|
was determined that text looks like a command.
|
2009-10-02 22:57:11 +02:00
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
If returns value other then None - then further processing will
|
|
|
|
be interrupted and that value will be used to return from
|
2009-10-02 22:57:11 +02:00
|
|
|
process_as_command.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def get_command(self, name):
|
2010-08-05 20:47:46 +02:00
|
|
|
command = get_command(self.COMMAND_HOST, name)
|
2009-10-02 22:57:11 +02:00
|
|
|
if not command:
|
2010-03-18 06:37:49 +01:00
|
|
|
raise NoCommandError("Command does not exist", name=name)
|
2009-10-02 22:57:11 +02:00
|
|
|
return command
|
|
|
|
|
|
|
|
def list_commands(self):
|
2010-08-05 20:47:46 +02:00
|
|
|
commands = list_commands(self.COMMAND_HOST)
|
2009-10-02 22:57:11 +02:00
|
|
|
commands = dict(commands)
|
2013-01-27 19:51:11 +01:00
|
|
|
return sorted(list(commands.values()), key=lambda k: k.__repr__())
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
class Command(object):
|
|
|
|
|
|
|
|
def __init__(self, handler, *names, **properties):
|
|
|
|
self.handler = handler
|
|
|
|
self.names = names
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# Automatically set all the properties passed to a constructor
|
|
|
|
# by the command decorator.
|
2013-01-02 13:54:02 +01:00
|
|
|
for key, value in properties.items():
|
2009-10-02 22:57:11 +02:00
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
return self.handler(*args, **kwargs)
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# This allows to use a shortcuted way of raising an exception
|
|
|
|
# inside a handler. That is to raise a CommandError without
|
|
|
|
# command or name attributes set. They will be set to a
|
|
|
|
# corresponding values right here in case if they was not set by
|
|
|
|
# the one who raised an exception.
|
2013-01-01 23:18:36 +01:00
|
|
|
except CommandError as error:
|
2009-10-02 22:57:11 +02:00
|
|
|
if not error.command and not error.name:
|
2009-11-01 13:50:34 +01:00
|
|
|
raise CommandError(error.message, self)
|
2009-10-02 22:57:11 +02:00
|
|
|
raise
|
|
|
|
|
|
|
|
# This one is a little bit too wide, but as Python does not have
|
2010-02-26 11:35:09 +01:00
|
|
|
# anything more constrained - there is no other choice. Take a
|
|
|
|
# look here if command complains about invalid arguments while
|
|
|
|
# they are ok.
|
2009-10-02 22:57:11 +02:00
|
|
|
except TypeError:
|
|
|
|
raise CommandError("Command received invalid arguments", self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Command %s>" % ', '.join(self.names)
|
|
|
|
|
|
|
|
def __cmp__(self, other):
|
2013-01-05 15:32:53 +01:00
|
|
|
if self.first_name > other.first_name:
|
|
|
|
return 1
|
|
|
|
if self.first_name < other.first_name:
|
|
|
|
return -1
|
|
|
|
return 0
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def first_name(self):
|
|
|
|
return self.names[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def native_name(self):
|
|
|
|
return self.handler.__name__
|
|
|
|
|
|
|
|
def extract_documentation(self):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Extract handler's documentation which is a doc-string and
|
|
|
|
transform it to a usable format.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
2010-08-04 21:31:47 +02:00
|
|
|
return getdoc(self.handler)
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
def extract_description(self):
|
|
|
|
"""
|
|
|
|
Extract handler's description (which is a first line of the
|
|
|
|
documentation). Try to keep them simple yet meaningful.
|
|
|
|
"""
|
|
|
|
documentation = self.extract_documentation()
|
|
|
|
return documentation.split('\n', 1)[0] if documentation else None
|
|
|
|
|
|
|
|
def extract_specification(self):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Extract handler's arguments specification, as it was defined
|
|
|
|
preserving their order.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
names, var_args, var_kwargs, defaults = getargspec(self.handler)
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# Behavior of this code need to be checked. Might yield
|
|
|
|
# incorrect results on some rare occasions.
|
2009-10-02 22:57:11 +02:00
|
|
|
spec_args = names[:-len(defaults) if defaults else len(names)]
|
|
|
|
spec_kwargs = list(zip(names[-len(defaults):], defaults)) if defaults else {}
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# Removing self from arguments specification. Command handler
|
|
|
|
# should receive the processors as a first argument, which
|
|
|
|
# should be self by the canonical means.
|
2009-10-02 22:57:11 +02:00
|
|
|
if spec_args.pop(0) != 'self':
|
|
|
|
raise DefinitionError("First argument must be self", self)
|
|
|
|
|
|
|
|
return spec_args, spec_kwargs, var_args, var_kwargs
|
|
|
|
|
|
|
|
def command(*names, **properties):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
A decorator for defining commands in a declarative way. Provides
|
|
|
|
facilities for setting command's names and properties.
|
|
|
|
|
|
|
|
Names should contain a set of names (aliases) by which the command
|
|
|
|
can be reached. If no names are given - the the native name (the one
|
|
|
|
extracted from the command handler) will be used.
|
|
|
|
|
2010-08-05 13:06:36 +02:00
|
|
|
If native=True is given (default) and names is non-empty - then the
|
|
|
|
native name of the command will be prepended in addition to the
|
|
|
|
given names.
|
2010-02-26 11:35:09 +01:00
|
|
|
|
|
|
|
If usage=True is given (default) - then command help will be
|
|
|
|
appended with autogenerated usage info, based of the command handler
|
|
|
|
arguments introspection.
|
|
|
|
|
|
|
|
If source=True is given - then the first argument of the command
|
|
|
|
will receive the source arguments, as a raw, unprocessed string. The
|
|
|
|
further mapping of arguments and options will not be affected.
|
|
|
|
|
|
|
|
If raw=True is given - then command considered to be raw and should
|
|
|
|
define positional arguments only. If it defines only one positional
|
|
|
|
argument - this argument will receive all the raw and unprocessed
|
|
|
|
arguments. If the command defines more then one positional argument
|
|
|
|
- then all the arguments except the last one will be processed
|
|
|
|
normally; the last argument will get what is left after the
|
|
|
|
processing as raw and unprocessed string.
|
|
|
|
|
|
|
|
If empty=True is given - this will allow to call a raw command
|
|
|
|
without arguments.
|
|
|
|
|
|
|
|
If extra=True is given - then all the extra arguments passed to a
|
|
|
|
command will be collected into a sequence and given to the last
|
|
|
|
positional argument.
|
|
|
|
|
|
|
|
If overlap=True is given - then all the extra arguments will be
|
|
|
|
mapped as if they were values for the keyword arguments.
|
|
|
|
|
2010-08-05 13:06:36 +02:00
|
|
|
If expand=True is given (default) - then short, one-letter options
|
|
|
|
will be expanded to a verbose ones, based of the comparison of the
|
|
|
|
first letter. If more then one option with the same first letter is
|
|
|
|
given - then only first one will be used in the expansion.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
names = list(names)
|
|
|
|
|
2010-08-05 13:06:36 +02:00
|
|
|
native = properties.get('native', True)
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
usage = properties.get('usage', True)
|
|
|
|
source = properties.get('source', False)
|
|
|
|
raw = properties.get('raw', False)
|
|
|
|
empty = properties.get('empty', False)
|
|
|
|
extra = properties.get('extra', False)
|
|
|
|
overlap = properties.get('overlap', False)
|
2010-08-05 13:06:36 +02:00
|
|
|
expand = properties.get('expand', True)
|
2009-10-02 22:57:11 +02:00
|
|
|
|
|
|
|
if empty and not raw:
|
|
|
|
raise DefinitionError("Empty option can be used only with raw commands")
|
|
|
|
|
|
|
|
if extra and overlap:
|
|
|
|
raise DefinitionError("Extra and overlap options can not be used together")
|
|
|
|
|
|
|
|
properties = {
|
|
|
|
'usage': usage,
|
|
|
|
'source': source,
|
|
|
|
'raw': raw,
|
|
|
|
'extra': extra,
|
|
|
|
'overlap': overlap,
|
|
|
|
'empty': empty,
|
2010-08-05 13:06:36 +02:00
|
|
|
'expand': expand
|
2009-10-02 22:57:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def decorator(handler):
|
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
Decorator which receives handler as a first argument and then
|
|
|
|
wraps it in the command which then returns back.
|
2009-10-02 22:57:11 +02:00
|
|
|
"""
|
|
|
|
command = Command(handler, *names, **properties)
|
|
|
|
|
|
|
|
# Extract and inject a native name if either no other names are
|
2010-08-05 13:06:36 +02:00
|
|
|
# specified or native property is enabled, while making
|
2010-02-26 11:35:09 +01:00
|
|
|
# sure it is going to be the first one in the list.
|
2010-08-05 13:06:36 +02:00
|
|
|
if not names or native:
|
2009-10-02 22:57:11 +02:00
|
|
|
names.insert(0, command.native_name)
|
|
|
|
command.names = tuple(names)
|
|
|
|
|
|
|
|
return command
|
|
|
|
|
2010-02-26 11:35:09 +01:00
|
|
|
# Workaround if we are getting called without parameters. Keep in
|
|
|
|
# mind that in that case - first item in the names will be the
|
|
|
|
# handler.
|
2009-10-02 22:57:11 +02:00
|
|
|
if names and isinstance(names[0], FunctionType):
|
|
|
|
return decorator(names.pop(0))
|
|
|
|
|
|
|
|
return decorator
|
2009-10-04 19:43:51 +02:00
|
|
|
|
2010-03-19 02:22:17 +01:00
|
|
|
def doc(text):
|
2009-10-04 19:43:51 +02:00
|
|
|
"""
|
2010-02-26 11:35:09 +01:00
|
|
|
This decorator is used to bind a documentation (a help) to a
|
|
|
|
command.
|
2009-10-04 19:43:51 +02:00
|
|
|
"""
|
2009-10-05 11:55:34 +02:00
|
|
|
def decorator(target):
|
|
|
|
if isinstance(target, Command):
|
|
|
|
target.handler.__doc__ = text
|
|
|
|
else:
|
|
|
|
target.__doc__ = text
|
|
|
|
return target
|
2009-10-04 19:43:51 +02:00
|
|
|
|
|
|
|
return decorator
|