libics.core.cfg

class libics.core.cfg.CfgBase(**kwargs)

Bases: libics.core.io.base.FileBase

Configuration file base class.

Attributes must be of built-in/primitive types or (subclasses of) CfgBase.

Methods

attributes()

Default saved attributes getter.

get_items([use_all])

Gets configuration attributes.

load(file_path, **kwargs)

Wrapper for load() function.

load_cfg(file_path[, fmt, preserve_case])

Loads a configuration text file to object.

map_recursive([item_func, cfg_func, use_all])

Applies a function recursively on all configuration items.

save(file_path, **kwargs)

Wrapper for save() function.

save_cfg(file_path[, fmt, serialize_all])

Saves the configuration object to a text file.

LOGGER = <Logger libics.core.cfg.CfgBase (WARNING)>
get_items(use_all=False)

Gets configuration attributes.

Returns
sub_cfgsdict(str)

Keys of attributes which are configurations themselves.

item_cfgsdict(str)

Configuration item keys.

load_cfg(file_path, fmt=None, preserve_case=True, **kwargs)

Loads a configuration text file to object.

In contrast to the load() method, this method assumes no stored metadata.

Parameters
file_pathstr

Save file path.

fmtstr

File format: “json”, “ini”, “yml”.

**kwargs

Keyword arguments passed to the parsers. Notable arguments include: “ini”:

interpolation: None to raw parse %. strict: False to allow non-safe files. preserve_case: False to lowercase option names.

map_recursive(item_func=None, cfg_func=None, use_all=False)

Applies a function recursively on all configuration items.

Parameters
item_funccallable

Function which is recursively called on configuration items Sets its value to the function return value. Call signature: func(key, old_val)->new_val.

cfg_funccallable

Function which is called on attributes which themselves are CfgBase objects. If the return value is False, recursively calls the map_recursive method on the attribute. If the return value is True, continues without further handling of the attribute. Call signature: func(key, sub_cfg)->bool.

use_allbool

Flag whether to map all attributes or only the ones stated in SER_KEYS.

save_cfg(file_path, fmt=None, serialize_all=False, **kwargs)

Saves the configuration object to a text file.

In contrast to the save() method, this method does not save any metadata.

Parameters
file_pathstr

Save file path.

fmtstr

File format: “json”, “ini”, “yml”.

serialize_allbool

Flag whether to serialize all attributes or only the ones stated in SER_KEYS.

**kwargs

Keyword arguments passed to parsers. Notable arguments include: “json”: indent.

libics.core.cfg.NamedTuple(mapping)

Recursively converts mappings to nested named tuples.

Named tuples are mapping objects (dicts) whose key-value pairs are accessible with attribute notation (e.g. object.attr_lvl0.attr_lvl1.key). Warning: converts all numeric keys to strings!

Parameters
mappingcollections.Mapping

Mapping object like dictionaries.

Returns
named_tuplecollections.namedtuple

Nested named tuple.

class libics.core.cfg.ProtectedDict(dict=None, /, **kwargs)

Bases: collections.UserDict

Dictionary wrapper to make NamedTuple not convert it to a named tuple (to keep it as dict).

Methods

clear()

get(k[,d])

items()

keys()

pop(k[,d])

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem()

as a 2-tuple; but raise KeyError if D is empty.

setdefault(k[,d])

update([E, ]**F)

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values()

copy

fromkeys

libics.core.cfg.cv_mapping_to_namedtuple(mapping, name='NamedTuple')

Converts a mapping to a named tuple (non-recursively).