Package pology :: Module getfunc

Module getfunc

Fetch Pology modules, functions, data, etc. by various handles.


Author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>

License: GPLv3

Functions
module or (module, string)
get_module(modpath, lang=None, proj=None, abort=False, wpath=False)
Import a Pology module.
(string, string or None, string or None, string or None, string or None)
split_ireq(ireq, abort=False)
Split item request string into distinct elements.
 
get_hook(modpath, lang=None, proj=None, func=None, args=None, abort=False)
Fetch a hook function.
 
get_hook_ireq(ireq, abort=False)
Like get_hook, but the hook is specified by item request.
 
get_result(modpath, lang=None, proj=None, func=None, args='', abort=False)
Fetch the result of a function evaluation.
 
get_result_ireq(ireq, abort=False)
Like get_result, but the function is specified by item request.
Variables
  __package__ = 'pology'
Function Details

get_module(modpath, lang=None, proj=None, abort=False, wpath=False)

 

Import a Pology module.

Module is specified by its dotted path in Pology's package structure relative to (optional) language and project. For example:

   get_module("remove")

will try to import the pology.remove, while:

   get_module("wconv", lang="sr")

will try to import the pology.lang.sr.wconv module, and:

   get_module("header", proj="kde")

will try to import the pology.proj.kde.header module.

Elements of the relative path can also contain hyphens, which will be converted into underscores when looking for the module.

If the module cannot be imported, if abort is True the execution will abort with an error message; otherwise an exception is raised.

If wpath is True, the resolved module path is returned as well.

Parameters:
  • modpath (string) - relative module location
  • lang (string) - language code
  • proj (string) - project code
  • abort (bool) - whether to abort execution if the module cannot be imported
  • wpath (bool) - whether to also return resolve module path
Returns: module or (module, string)
imported module, possibly with its path

split_ireq(ireq, abort=False)

 

Split item request string into distinct elements.

The item request is a string of the form [lang:][proj%]path[/item][~args] (or [proj%][lang:]...), which this function parses into (path, lang, proj, item, args) tuple. If language, project, item or argument strings are not not stated, their value in the tuple will be None. The language should be a proper language code, the project an identifier-like string, the path a sequence of identifier-like strings connected by dots (though hyphens are accepted an taken as synonymous to underscores), item an identifier-like string, and arguments can be an arbitrary string.

If the item request cannot be parsed, either the execution is aborted with an error message, or an exception is raised, depending on value of abort.

Parameters:
  • ireq (string) - item request
  • abort (bool) - whether to abort execution or if the request cannot be parsed
Returns: (string, string or None, string or None, string or None, string or None)
parsed request elements

get_hook(modpath, lang=None, proj=None, func=None, args=None, abort=False)

 

Fetch a hook function.

Loads a hook function from a module obtained by applying get_module to modpath, lang, and proj parameters. If func is None, the function name defaults to module name; if func is not None, but function of that name is not found, then the function named <modname>_<func> is additionally tried (where <modname> is the last element in modpath). If args is not None, then the loaded function is considered a hook factory, and the hook is created by calling it with args string as argument list (it should have no surrounding parenthesis).

Parameters:
  • modpath (string) - hook module
  • lang (string) - language code
  • proj (string) - project code
  • func (string) - function name of hook or hook factory
  • args (string) - argument string to hook factory
  • abort (bool) - whether to abort execution or raise exception if the hook cannot be loaded
Returns:
the hook

get_hook_ireq(ireq, abort=False)

 

Like get_hook, but the hook is specified by item request.

For a module pology.FOO which defines the FOO() hook function, the hook specification is simply FOO. If the hook function is named BAR() instead of FOO(), the hook specification is given as FOO/BAR; if the hook function is named FOO_BAR(), i.e. the specification would be FOO/FOO_BAR, it can be folded to FOO/BAR. Language-specific hooks (pology.lang.LANG.FOO) are aditionally preceded by the language code and colon, as LANG:FOO or LANG:FOO/BAR. Project-specific hooks (pology.proj.PROJ.FOO) are aditionally preceded by the project code and percent, as PROJ%FOO or LANG%FOO/BAR. If the hook is both language- and project- specific, language and project qualifiers can both be added: LANG:PROJ%FOO or LANG:PROJ%FOO/BAR; ordering, LANG:PROJ%... or PROJ%LANG:..., is not significant.

If the hook is not a plain hook, but a hook factory function, the factory arguments are supplied after the basic hook specification, separated by tilde: LANG:PROJ%FOO/BAR~ARGLIST (where LANG:, PROJ% and /BAR may be omitted under previously listed conditions). Argument list is formatted just like it would be passed in Python code to the factory function, omitting the surrounding parenthesis.

get_result(modpath, lang=None, proj=None, func=None, args='', abort=False)

 

Fetch the result of a function evaluation.

Executes function from the module loaded by applying get_module to modpath, lang, and proj parameters. If func is not given, the function name defaults to module name. args is the string representing the argument list to the function call (without surrounding parenthesis).

Parameters:
  • modpath (string) - function module
  • lang (string) - language code
  • proj (string) - project code
  • func (string) - function name within the module
  • args (string) - argument string to function call
  • abort (bool) - if the function is not found, abort or report None
Returns:
the value returned by the function call