This module provides structured, YAML based, deep dictionary configuration objects. The objects have a built-in deep-update function and use deep-update behavior by default. They act otherwise like dictionaries, and handle thier internal operation using a storage dictionary. The objects also provide a YAML configuration file reading and writing interface.
Recursive extraction method for changing the type of nested dictionary objects.
Parameters: |
|
---|
This method forces the PyYAML library to construct unicode objects when reading YAML instead of producing regular strings.
It is designed to imporove compatibility in Python2.x using unicode objects.
Merge deep collection-like structures.
When this function encounters a sequence, the entire sequence from u is considered a single value which replaces any value from d. To allow for merging sequences in u and d, see function advanceddeepmerge().
Parameters: |
|
---|
Inverse Merge causes u to only update missing values of d, but does so in a deep fashion.
Merge deep collection-like structures.
This function will merge sequence structures when they are found. When used with sequence=False, it behaves like deepmerge().
Parameters: |
---|
Inverse Merge causes u to only update missing values of d, but does so in a deep fashion.
Flatten a given nested dictionary.
Parameters: |
|
---|
Each nested key will become a root level key in the final dictionary. The root level keys will be the set of nested keys, joined by the separator keyword argument.
Expand a flattened dictionary into a nested one.
Parameters: |
|
---|
Each key with the separator will become a nested dictionary key in the final dictionary.
Adds extra methods to dictionary for configuration
The name/type of this configuration.
Merge another configuration into this one (the master).
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.merge({'c':'d'})
>>> a
{'a': 'b', 'c': 'd'}
Inverse merge(), where other will be considered original, and this object will be canonical.
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.imerge({'c':'d'})
>>> a
{'a': 'b', 'c': 'e'}
Save this configuration as a YAML file. YAML files generally have the .yaml or .yml extension. If the filename ends in .dat, the configuration will be saved as a raw dictionary literal.
Parameters: |
---|
Loads a configuration from a yaml file, and merges it into the master configuration.
Parameters: | |
---|---|
Raises : | IOError if the file can’t be found. |
Returns: | boolean, whether the file was loaded. |
Dictionary representing this configuration. This property should be used if you wish to have a ‘true’ dictionary object. It is used internally to write this configuration to a YAML file.
Re-nest this object. This method applies the dt deep-storage attribute to each nesting level in the configuration object.
Parameters: | deep_store_type – mapping nesting type, will set dn. |
---|
This method does not return anything.
Turn a list of literals into configuration items.
Parameters: |
|
---|
Keywords are parsed where foo=bar becomes self["foo"] = "bar". If bar can be parsed as a python literal (float, int, dict, list etc..), the literal value will be used in place of the string. For which literals will be parsed, see ast.literal_eval() from the Abstract-Syntax Tree features in python. There is great power in using this method with dotted configurations, as foo.bat=bar will get parsed to self["foo.bat"] = "bar". This is useful for parsing configuration command line options.
The configuration loads (starting with a blank configuration):
- The list of supercfg ‘s. This list should contain tuples of (module,name) pairs.
- The module configuration file named for defaultcfg
- The cfg file from the user’s home folder ~/config.yml
- The cfg file from the working directory.
If the fourth file is not found, and the user specified a new name for the configuration file (i.e. cfg != defaultcfg), then the user is warned that no configuration file could be found. This way the user is only warned about a missing configuration file if they requested a file specifically (and so intended to use a customized file).
Parameters: |
|
---|
Create a configuration from a series of YAML files.
See configure() for a detailed description of the resolution order of configuration files for this method.
Create a configuration from a resource filename pair.
Parameters: |
|
---|
Make a configuration from the input object base.
Acceptable Inputs:
A configuration which can use dotted accessor methods.
Configuration variables can be accessed and set with dot-qualified names. E.g.:
>>> Config = DottedConfiguration( { "Data": { "Value": { "ResultA" : 10 }, }, })
>>> Config["Data"]["Value"]["ResultA"]
10
>>> Config["Data.Value.ResultA"]
10
By default, this will not work for doubly nested values:
>>> Config["Data"]["Value.ResultA"]
KeyError
However, this behavior can be changed by specifying a new default nesting structure:
>>> Config.dn = DottedConfiguration
>>> Config.merge(Config)
>>> Config["Data"]["Value.ResultA"]
10
The deep nesting separator character(s).
Returns this dictionary, flattened so that all dotted names are at the root level.
The configuration loads (starting with a blank configuration):
- The list of supercfg ‘s. This list should contain tuples of (module,name) pairs.
- The module configuration file named for defaultcfg
- The cfg file from the user’s home folder ~/config.yml
- The cfg file from the working directory.
If the fourth file is not found, and the user specified a new name for the configuration file (i.e. cfg != defaultcfg), then the user is warned that no configuration file could be found. This way the user is only warned about a missing configuration file if they requested a file specifically (and so intended to use a customized file).
Parameters: |
|
---|
Create a configuration from a series of YAML files.
See configure() for a detailed description of the resolution order of configuration files for this method.
Deep nesting attribute reader
Deep storage type.
The filename which has been used to save/load this configuration most recently
Create a configuration from a single YAML file.
Create a configuration from a resource filename pair.
Parameters: |
|
---|
Return the HexDigest hash
Inverse merge(), where other will be considered original, and this object will be canonical.
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.imerge({'c':'d'})
>>> a
{'a': 'b', 'c': 'e'}
Loads a configuration from a yaml file, and merges it into the master configuration.
Parameters: | |
---|---|
Raises : | IOError if the file can’t be found. |
Returns: | boolean, whether the file was loaded. |
Load from a resource filename
Make a configuration from the input object base.
Acceptable Inputs:
Merge another configuration into this one (the master).
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.merge({'c':'d'})
>>> a
{'a': 'b', 'c': 'd'}
Turn a list of literals into configuration items.
Parameters: |
|
---|
Keywords are parsed where foo=bar becomes self["foo"] = "bar". If bar can be parsed as a python literal (float, int, dict, list etc..), the literal value will be used in place of the string. For which literals will be parsed, see ast.literal_eval() from the Abstract-Syntax Tree features in python. There is great power in using this method with dotted configurations, as foo.bat=bar will get parsed to self["foo.bat"] = "bar". This is useful for parsing configuration command line options.
If key is not found, d is returned if given, otherwise KeyError is raised.
as a 2-tuple; but raise KeyError if D is empty.
Re-nest this object. This method applies the dt deep-storage attribute to each nesting level in the configuration object.
Parameters: | deep_store_type – mapping nesting type, will set dn. |
---|
This method does not return anything.
Save this configuration as a YAML file. YAML files generally have the .yaml or .yml extension. If the filename ends in .dat, the configuration will be saved as a raw dictionary literal.
Parameters: |
---|
Dictionary representing this configuration. This property should be used if you wish to have a ‘true’ dictionary object. It is used internally to write this configuration to a YAML file.
A structured configuration with some basic defaults.
This class does two things differently for configurations:
Configurations are stored in a “Configurations” variable set. They can then be loaded by configuration key instead of filename, using the setFile() method to set the filename, and then calling load() or meth:save with no arguments.
Configuration variables can be accessed and set with dot-qualified names. E.g.:
>>> Config = StructuredConfigruation( { "Data": { "Value": { "ResultA" : 10 }, }, })
>>> Config["Data"]["Value"]["ResultA"]
10
>>> Config["Data.Value.ResultA"]
10
By default, this will not work for doubly nested values:
>>> Config["Data"]["Value.ResultA"]
KeyError
However, this behavior can be changed by specifying a new default nesting structure:
>>> Config.dn = DottedConfiguration
The configuration loads (starting with a blank configuration):
- The list of supercfg ‘s. This list should contain tuples of (module,name) pairs.
- The module configuration file named for defaultcfg
- The cfg file from the user’s home folder ~/config.yml
- The cfg file from the working directory.
If the fourth file is not found, and the user specified a new name for the configuration file (i.e. cfg != defaultcfg), then the user is warned that no configuration file could be found. This way the user is only warned about a missing configuration file if they requested a file specifically (and so intended to use a customized file).
Parameters: |
|
---|
Create a configuration from a series of YAML files.
See configure() for a detailed description of the resolution order of configuration files for this method.
Deep nesting attribute reader
Deep storage type.
The filename which has been used to save/load this configuration most recently
Returns this dictionary, flattened so that all dotted names are at the root level.
Create a configuration from a single YAML file.
Create a configuration from a resource filename pair.
Parameters: |
|
---|
Return the HexDigest hash
Inverse merge(), where other will be considered original, and this object will be canonical.
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.imerge({'c':'d'})
>>> a
{'a': 'b', 'c': 'e'}
Load from a resource filename
Make a configuration from the input object base.
Acceptable Inputs:
Merge another configuration into this one (the master).
Parameters: | other (dict-like) – The other dictionary to be merged. |
---|
See deepmerge().
>>> a = Configuration(**{'a':'b','c':'e'})
>>> a.merge({'c':'d'})
>>> a
{'a': 'b', 'c': 'd'}
Turn a list of literals into configuration items.
Parameters: |
|
---|
Keywords are parsed where foo=bar becomes self["foo"] = "bar". If bar can be parsed as a python literal (float, int, dict, list etc..), the literal value will be used in place of the string. For which literals will be parsed, see ast.literal_eval() from the Abstract-Syntax Tree features in python. There is great power in using this method with dotted configurations, as foo.bat=bar will get parsed to self["foo.bat"] = "bar". This is useful for parsing configuration command line options.
If key is not found, d is returned if given, otherwise KeyError is raised.
as a 2-tuple; but raise KeyError if D is empty.
Re-nest this object. This method applies the dt deep-storage attribute to each nesting level in the configuration object.
Parameters: | deep_store_type – mapping nesting type, will set dn. |
---|
This method does not return anything.
Dictionary representing this configuration. This property should be used if you wish to have a ‘true’ dictionary object. It is used internally to write this configuration to a YAML file.
Set the default/current configuration file for this configuration.
The configuration file set by this method will be used next time load() or save() is called with no filename.
Parameters: | filename (string) – The filename to load from. |
---|
Save the configuration to a YAML file. If filename is not provided, the configuration will use the file set by setFile().
Parameters: | filename (string) – Destination filename. |
---|
Uses Configuration.save().
Load the configuration to a YAML file. If filename is not provided, the configuration will use the file set by setFile().
Parameters: |
---|
Uses Configuration.load().
There are a few design decisions in pyshell.config that are worth explaining.
The configuration items work with an internal storage object. That storage object is a dictionary of dictionaries, in its purest form. Configuration classes are responsible for re-casting any returned value as the correct external configuration object when accessed. This should be done as lightly as possible (i.e. only on the first layer, not renested).
There is some tricky handling of dictionary keys which contain the default separator, ".". The following rules apply:
When writing to YAML files, there are two callback functions which can be used to encapsulate metadata, or multiple YAML documents in a single document. By default, the included YAML reader reads documents separated by ---. Only the last document is the configuration interpreted by the Configuration object. The other documents are passed to _load_yaml_callback(), which accepts many arguments as individual YAML documents. To save multiple YAML documents as output, the extra YAML documents should be returned as a list by _save_yaml_callback(). If you don’t want to save any extra documents, _save_yaml_callback() may return an empty list (as it does by default). These callback functions could be used to silently change the underlying configuration object before serialization to YAML. This behavior has not been tested.