pycnfg.Handler

class pycnfg.Handler(objects, oid, path_id='path__default', logger_id='logger__default')[source]

Bases: pycnfg.producer.Producer

Read and execute configurations in priority.

Interface: read, exec.

Parameters
  • objects (dict) – Dictionary with objects from previous executed producers: {‘section_id__config__id’, object,}

  • oid (str) – Unique identifier of produced object.

  • path_id (str, optional (default='default')) – Project path identifier in objects.

  • logger_id (str, optional (default='default')) – Logger identifier in objects.

objects

Dictionary with objects from previous executed producers: {‘section_id__config__id’, object,}

Type

dict

oid

Unique identifier of produced object.

Type

str

logger

Logger.

Type

logging.Logger

project_path

Absolute path to project dir.

Type

str

See also

pycnfg.Producer

Execute configuration steps.

Methods

dict_api(obj[, method])

Forwarding api for dictionary object.

dump_cache(obj[, prefix, cachedir, pkg])

Dump intermediate object state to IO.

exec(configs[, mutable, debug])

Execute configurations in priority.

load_cache(obj[, prefix, cachedir, pkg])

Load intermediate object state from IO.

read(cnfg[, dcnfg, resolve_none, update_expl])

Read raw configuration and transform to executable.

run(init, steps)

Execute configuration steps.

update(obj, items)

Update key(s) for dictionary object.

__init__(objects, oid, path_id='path__default', logger_id='logger__default')[source]

Initialize self. See help(type(self)) for accurate signature.

Methods

__init__(objects, oid[, path_id, logger_id])

Initialize self.

dict_api(obj[, method])

Forwarding api for dictionary object.

dump_cache(obj[, prefix, cachedir, pkg])

Dump intermediate object state to IO.

exec(configs[, mutable, debug])

Execute configurations in priority.

load_cache(obj[, prefix, cachedir, pkg])

Load intermediate object state from IO.

read(cnfg[, dcnfg, resolve_none, update_expl])

Read raw configuration and transform to executable.

run(init, steps)

Execute configuration steps.

update(obj, items)

Update key(s) for dictionary object.

dict_api(obj, method='update', **kwargs)

Forwarding api for dictionary object.

Could be useful to add/pop keys via configuration steps. For example to proceed update: (‘dict_api’, {‘b’:7} )

dump_cache(obj, prefix=None, cachedir=None, pkg='pickle', **kwargs)

Dump intermediate object state to IO.

Parameters
  • obj (picklable) – Object to dump.

  • prefix (str, optional (default=None)) – File identifier, added to filename. If None, ‘self.oid’ is used.

  • cachedir (str, optional(default=None)) – Absolute path to dump dir or relative to ‘project_path’ started with ‘./’. Created, if not exists. If None, “sproject_path/ .temp/objects” is used.

  • pkg (str, optional (default='pickle')) – Import package and try pkg.dump(obj, file, **kwargs).

  • **kwargs (kwargs) – Additional parameters to pass in .dump().

Returns

obj – Unchanged input for compliance with producer logic.

Return type

picklable

exec(configs, mutable=False, debug=False)[source]

Execute configurations in priority.

For each configuration:

  • Initialize producer

producer(objects,``section_id__configuration_id, **kwargs)``, where kwargs taken from (‘__init__’, kwargs) step if provided.

  • call producer.run(init, steps).

  • store result under section_id__configuration_id in objects.

Parameters
  • configs (list of tuple) – List of configurations, prepared for execution: [(‘section_id__config__id’, config), …]

  • mutable (bool, optional (default=False)) – If True, rewrite existed object when configuration id already in objects. Otherwise skip execution and remain original.

  • debug (bool, optional (default=False)) – If True, print debug information.

Returns

objects – Dictionary with resulted objects from configs execution” {‘section_id__config__id’: object}

Return type

dict

Notes

producer/init auto initialized if needed.

load_cache(obj, prefix=None, cachedir=None, pkg='pickle', **kwargs)

Load intermediate object state from IO.

Parameters
  • obj (picklable) – Object template, for producer logic only (ignored).

  • prefix (str, optional (default=None)) – File identifier. If None, ‘self.oid’ is used.

  • pkg (str, optional default('pickle')) – Import package and try obj = pkg.load(file, **kwargs).

  • cachedir (str, optional(default=None)) – Absolute path to load dir or relative to ‘project_path’ started with ‘./’. If None, ‘project_path/.temp/objects’ is used.

  • **kwargs (kwargs) – Additional parameters to pass in .load().

Returns

obj – Loaded cache.

Return type

picklable object

read(cnfg, dcnfg=None, resolve_none=False, update_expl=False)[source]

Read raw configuration and transform to executable.

Parameters
  • cnfg (dict or str) – Set of configurations: {‘section_id’: {‘configuration_id’: configuration,},}. If str, absolute path to file with CNFG variable.

  • dcnfg (dict, str, optional (default=None)) – Set of default configurations: {‘section_id’: {‘configuration_id’: configuration, },}. If str, absolute path to file with CNFG variable. If None, use pycnfg.CNFG .

  • resolve_none (bool, optional (default=False)) – If True, try to resolve None values for step kwargs (if any remains after global substitution). If kwarg name matches with section name, substitute either with conf_id on zero position or val, depending on if _id prefix in kwarg_name.

  • update_expl (bool, optional (default=True)) – If True apply global values to update explicitly set kwargs for target step, otherwise update only unset kwargs.

Returns

configs – List of configurations, prepared for execution.

Return type

list of tuple [(‘section_id__configuration_id’, config),]

Notes

Apply default configuration dcnfg:

  • Copy default sections that not in conf.

  • If sub-keys in some section`s sub-configuration are skipped:

Try to find match section_id__configuration_id in default, if can`t copy from zero position sub-configuration. If default section not exist at all, use default values for sub-keys: {'init': {}, 'priority': 1, 'class': pycnfg.Producer, 'global': {}, 'patch': {}, 'steps': [],}.

=> skipped subkeys from ‘global’ key for most outer and section default levels will be also copy.

Resolve None:

  • If any step kwarg is None => use value from global.

  • If not in global => search ‘kwarg_id’ in ‘section_id’s.

    • If no section => remain None.

    • If section exist:

    If more then one configurations in section => ValueError. If ‘kwarg_id’ contains postfix ‘_id’, substitute None with section_id__configuration_id, otherwise with object.

run(init, steps)

Execute configuration steps.

Consecutive call (with decorators):

init = getattr(self, 'method_id')(init, objects=objects, **kwargs)

Parameters
  • init (object) – Will be passed as arg in each step and get back as result.

  • steps (list of tuples) – List of self methods to run consecutive with kwargs: (‘method_id’, kwargs, decorators ).

Returns

configs – List of configurations, prepared for execution: [(‘section_id__config__id’, config), …].

Return type

list of tuple

Notes

Object identifier oid auto added, if produced object has oid attribute.

update(obj, items)

Update key(s) for dictionary object.

Parameters
  • obj (dict) – Object to update.

  • items (dict, list, optional (default=None)) – Either dictionary or items [(key,val),] to update obj.

Returns

obj – Updated input.

Return type

dict