def init(): """Read, compile and register default commands. These are those contained by :data:`.default_cmds` plus those defined by the :mod:`.commands.stdcmd` module, parse docstrings of all functions in :mod:`.handlers` namespace for `command ...` syntax specifications and :func:`.register` those that have some. """ # default_cmds dictionarty for command, handler in default_cmds.items(): register(command, handler) # functions in handlers-namespace # for all functions within this namespace, parse # docstrings for `command` specification and register # functions as handlers in case of matches. funcs = [(n,f) for n,f in handlers.__dict__.items() if hasattr(f, '__call__')] for fname, func in funcs: sntxs = handlers.extract_cmd_syntax(fname) if sntxs != None: for syntax in sntxs: register(syntax, func) else: print "Not found!" # read, compile, register stdcmd.py # ok. read commands from stdcmd.py import stdcmd print 'parse', stdcmd.__file__ for fn, cc in stdcmd.__dict__.items(): if hasattr(handlers, fn): f = handlers.__dict__.get(fn) if hasattr(f, '__call__'): for c in cc: #print fn, 'invoked by:', c register(c, f) # TODO: arguments! del stdcmd print 'done.'
def init(): """Read, compile and register default commands. These are those contained by :data:`.default_cmds` plus those defined by the :mod:`.commands.stdcmd` module, parse docstrings of all functions in :mod:`.handlers` namespace for `command ...` syntax specifications and :func:`.register` those that have some. """ # default_cmds dictionarty for command, handler in default_cmds.items(): register(command, handler) # functions in handlers-namespace # for all functions within this namespace, parse # docstrings for `command` specification and register # functions as handlers in case of matches. funcs = [(n, f) for n, f in handlers.__dict__.items() if hasattr(f, '__call__')] for fname, func in funcs: sntxs = handlers.extract_cmd_syntax(fname) if sntxs != None: for syntax in sntxs: register(syntax, func) else: print "Not found!" # read, compile, register stdcmd.py # ok. read commands from stdcmd.py import stdcmd print 'parse', stdcmd.__file__ for fn, cc in stdcmd.__dict__.items(): if hasattr(handlers, fn): f = handlers.__dict__.get(fn) if hasattr(f, '__call__'): for c in cc: #print fn, 'invoked by:', c register(c, f) # TODO: arguments! del stdcmd print 'done.'
def register_handler(func): """\ Decorator for command handler functions. Function will be copied into the namespace of :mod:`.commands.handlers` and registered as a handler for all commands whose syntaxes it specifies in its docstring. The package root module, :mod:`..kathaireo`, defines an alias of this function, the decorator ``@cmd_handler``. This should make it easy to extend ``kathaireo``'s functionality, since implementation, invocation syntax definition and deployment of custom commands can all be taken care of in the same place. :param func: function to be registered as a command handler in the :mod:`handlers` module namespace. Handler functions **must accept** both ``\*args`` and ``\*\*kwargs`` parameter collections. `(Ist das so, ja?)` :returns: function passed on call, i.e. functions with ``@cmd_handler`` decorator imported from root module (:mod:`..kathaireo``). """ hnd_ns = handlers.__dict__ fname = func.func_name if fname in hnd_ns: print 'Overwrite handler module pointer {} with'.format(fname), print 'new command handler function at {}.'.format( func.func_code.co_filename) hnd_ns[fname] = func # if `command ...` is defined in doc line, register sntxs = handlers.extract_cmd_syntax(fname) if sntxs: for syntax in sntxs: register(syntax, func) else: print "Couldn't find handler function {}!".format(fname) return func
def register_handler(func): """\ Decorator for command handler functions. Function will be copied into the namespace of :mod:`.commands.handlers` and registered as a handler for all commands whose syntaxes it specifies in its docstring. The package root module, :mod:`..kathaireo`, defines an alias of this function, the decorator ``@cmd_handler``. This should make it easy to extend ``kathaireo``'s functionality, since implementation, invocation syntax definition and deployment of custom commands can all be taken care of in the same place. :param func: function to be registered as a command handler in the :mod:`handlers` module namespace. Handler functions **must accept** both ``\*args`` and ``\*\*kwargs`` parameter collections. `(Ist das so, ja?)` :returns: function passed on call, i.e. functions with ``@cmd_handler`` decorator imported from root module (:mod:`..kathaireo``). """ hnd_ns = handlers.__dict__ fname = func.func_name if fname in hnd_ns: print 'Overwrite handler module pointer {} with'.format(fname), print 'new command handler function at {}.'.format(func.func_code.co_filename) hnd_ns[fname] = func # if `command ...` is defined in doc line, register sntxs = handlers.extract_cmd_syntax(fname) if sntxs: for syntax in sntxs: register(syntax, func) else: print "Couldn't find handler function {}!".format(fname) return func