Add some documentation for decorators

This commit is contained in:
Florian Bruhin 2014-03-04 07:02:45 +01:00
parent 6f093d33c1
commit b6a28aba7b
2 changed files with 45 additions and 3 deletions

View File

@ -29,16 +29,28 @@ class Command(QObject):
"""Base skeleton for a command. """Base skeleton for a command.
Attributes: Attributes:
FIXME ... name: The main name of the command.
split_args: Whether to split the arguments or not.
hide: Whether to hide the arguments or not.
nargs: A (minargs, maxargs) tuple, maxargs = None if there's no limit.
count: Whether the command supports a count, or not.
desc: The description of the command.
instance: How to get to the "self" argument of the handler.
A dotted string as viewed from app.py, or None.
handler: The handler function to call.
Signals:
signal: Gets emitted when something should be called via handle_command
from the app.py context.
""" """
signal = pyqtSignal(tuple)
# FIXME: # FIXME:
# we should probably have some kind of typing / argument casting for args # we should probably have some kind of typing / argument casting for args
# this might be combined with help texts or so as well # this might be combined with help texts or so as well
signal = pyqtSignal(tuple)
def __init__(self, name, split_args, hide, nargs, count, desc, instance, def __init__(self, name, split_args, hide, nargs, count, desc, instance,
handler): handler):
super().__init__() super().__init__()
@ -76,6 +88,10 @@ class Command(QObject):
args: Arguments to the command. args: Arguments to the command.
count: Command repetition count. count: Command repetition count.
Emit:
signal: When the command has an instance and should be handled from
the app.py context.
""" """
dbgout = ["command called:", self.name] dbgout = ["command called:", self.name]
if args: if args:

View File

@ -39,12 +39,26 @@ class register:
This could also be a function, but as a class (with a "wrong" name) it's This could also be a function, but as a class (with a "wrong" name) it's
much cleaner to implement. much cleaner to implement.
Attributes:
instance: The instance to be used as "self", as a dotted string.
name: The name (as string) or names (as list) of the command.
nargs: A (minargs, maxargs) tuple of valid argument counts.
split_args: Whether to split the arguments or not.
hide: Whether to hide the command or not.
""" """
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
def __init__(self, instance=None, name=None, nargs=None, split_args=True, def __init__(self, instance=None, name=None, nargs=None, split_args=True,
hide=False): hide=False):
"""Gets called on parse-time with the decorator arguments.
Arguments:
See class attributes.
"""
self.name = name self.name = name
self.split_args = split_args self.split_args = split_args
self.hide = hide self.hide = hide
@ -52,6 +66,18 @@ class register:
self.instance = instance self.instance = instance
def __call__(self, func): def __call__(self, func):
"""Gets called when a function should be decorated.
Doesn't actually decorate anything, but creates a Command object and
registers it in the cmd_dict.
Arguments:
func: The function to be decorated.
Return:
The original function (unmodified).
"""
names = [] names = []
name = func.__name__.lower() if self.name is None else self.name name = func.__name__.lower() if self.name is None else self.name
if isinstance(name, str): if isinstance(name, str):