core.config
Component configuration classes.
The ConfigMixin mixin class is used with every component to
provide a hierarchical tree of named configuration values. Each
configuration value node has a fixed type and can be configured to have
constraints such as maximum and minimum values.
Configuration values are accessed in a dictionary-like manner. During a component’s initialisation you should create the required configuration nodes like this:
self.config['zlen'] = ConfigInt(value=100, min_value=1)
self.config['looping'] = ConfigEnum(choices=('off', 'repeat'))
Subsequently the config object behaves more like a dictionary:
self.config['zlen'] = 250
...
zlen = self.config['zlen']
Users of a component can initialise its configuration by passing a
config dict or key-value pairs to the component’s
constructor:
resize = Resize(config={'xup': xup}, xdown=xdown)
The configuration can be changed, even when the component is running,
with the get_config and
set_config methods:
cfg = resize.get_config()
cfg['xup'] = 3
cfg['xdown'] = 4
resize.set_config(cfg)
Or, more simply:
resize.set_config(xup=3, xdown=4)
Note that these methods are thread-safe and make a copy of the
configuration tree. This ensures that all your configuration changes
are applied together, some time after calling
set_config.
Add a config tree to a pyctools component. |
|
Parent configuration node. |
|
Configuration node for |
|
Mixin class for configuration nodes. |
|
Numerical configuration node with min and/or max bounds. |
|
Integer configuration node. |
|
Float configuration node. |
|
Boolean configuration node. |
|
String configuration node. |
|
File pathname configuration node. |
|
Configuration node with a list of choices. |
|
String 'enum' configuration node. |
|
Integer 'enum' configuration node. |
- class ConfigLeafNode(value, **kwds)[source]
Bases:
objectMixin class for configuration nodes.
This can be used with immutable Python types such as
intorstrto define a class that stores data of the Python type but has some extra attributes and methods.- Parameters:
value – Initial (default) value of the node.
kwds – Any other node attributes.
- has_default = True
The node has a meaningful default value.
- enabled = True
The node is currently enabled.
- class BoundedConfigLeafNode(value, min_value=None, max_value=None, **kwds)[source]
Bases:
ConfigLeafNodeNumerical configuration node with min and/or max bounds.
- class ConfigInt(value=0, **kwds)[source]
Bases:
BoundedConfigLeafNode,intInteger configuration node.
- Parameters:
value (int) – Initial (default) value of the node.
- class ConfigBool(value=False, **kwds)[source]
Bases:
ConfigLeafNode,intBoolean configuration node.
- Parameters:
value (
objectoronoroff) – Initial (default) value of the node.
- class ConfigFloat(value=0.0, **kwds)[source]
Bases:
BoundedConfigLeafNode,floatFloat configuration node.
- Parameters:
value (float) – Initial (default) value of the node.
- decimals = 8
How many decimal places to use when displaying the value.
- class ConfigStr(value='', **kwds)[source]
Bases:
ConfigLeafNode,strString configuration node.
- Parameters:
value (str) – Initial (default) value of the node.
- class ConfigPath(value='', exists=True, **kwds)[source]
Bases:
ConfigStrFile pathname configuration node.
- class ChoicesConfigLeafNode(value=None, choices=[], extendable=False, **kwds)[source]
Bases:
ConfigLeafNodeConfiguration node with a list of choices.
- class ConfigEnum(value=None, choices=[], extendable=False, **kwds)[source]
Bases:
ChoicesConfigLeafNode,strString ‘enum’ configuration node.
- class ConfigIntEnum(value=None, choices=[], extendable=False, **kwds)[source]
Bases:
ChoicesConfigLeafNode,intInteger ‘enum’ configuration node.
- class ConfigParent[source]
Bases:
objectParent configuration node.
Stores a set of child nodes in a
dict.- audit_string()[source]
Generate a string suitable for use in an audit trail.
Converts a component’s configuration settings to a
Metadataaudit trail string. This is a convenient way to add to the audit trail in a “standard” format. Only the non-default settings are shown, to keep the audit trail short but useful.
- parser_add(parser, prefix='')[source]
Add config to an
argparse.ArgumentParserobject.The parser’s
add_argumentmethod is called for each config item. The argument name is constructed from the parent and item names, with a dot separator.- Parameters:
parser (argparse.ArgumentParser) – The argument parser object.
prefix (str) – The parent node name.
- parser_set(args)[source]
Set config from an
argparse.Namespaceobject.Call this method with the return value from
parse_args.- Parameters:
args (argparse.Namespace) – The populated
argparse.Namespaceobject.
- class CompoundConfig(config_map={})[source]
Bases:
ConfigParentConfiguration node for
Compoundcomponents.Stores a set of
ConfigParentnodes in adict. This allows components to be nested to any depth whilst making their configuration accessible from the top level.The
config_mapis used to allow multiple child components to be controlled by one config value.
- class ConfigMixin(**kwds)[source]
Bases:
objectAdd a config tree to a pyctools component.
- get_config()[source]
Get a copy of the component’s current configuration.
Using a copy allows the config to be updated in a threadsafe manner while the component is running. Use the
set_configmethod to update the component’s configuration after making changes to the copy.- Returns:
Copy of component’s configuration.
- Return type:
- set_config(config={}, **kwds)[source]
Update the component’s configuration.
Use the
get_configmethod to get a copy of the component’s configuration, update that copy then callset_configto update the component. This enables the configuration to be changed in a threadsafe manner while the component is running, and allows several values to be changed at once.- Parameters:
config (ConfigParent) – New configuration.
- update_config()[source]
Pull any changes made with
set_config.Call this from within your component before using any config values to ensure you have the latest values set by the user.
Comments or questions? Please email jim@jim-easterbrook.me.uk.