Contains implementation of the configuration system allowing to flexibly control the programs behaviour.
- read and write sections with key=value pairs from and to INI style file-like objects !
- Wrappers for these file-like objects allow virtually any source for the operation
- configuration inheritance
- allow precise control over the inheritance behaviour and inheritance defaults
- final results of the inheritance operation will be cached into the ConfigManager
- Environment Variables can serve as final instance to override values using the DictConfigINIFile
- Creation and Maintenance of individual configuration files as controlled by submodules of the application
- These configuration go to a default location, or to the given file-like object
- embed more complex data to be read by specialised classes using URLs
- its safe and easy to write back possibly altered values even if complex inheritance schemes are applied
Bases: set
Set with ability to return the key which matches the requested one
This functionality is the built-in in default STL sets, and I do not understand why it is not provided here ! Of course I want to define custom objects with overridden hash functions, put them into a set, and finally retrieve the same object again !
Note: | indexing a set is not the fastest because the matching key has to be searched. Good news is that the actual ‘is k in set’ question can be answered quickly |
---|
Add an element to a set.
This has no effect if the element is already present.
Return the difference of two or more sets as a new set.
(i.e. all elements that are in this set but not the others.)
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
Return the intersection of two or more sets as a new set.
(i.e. elements that are common to all of the sets.)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
Epydoc: mrv.conf.ConfigAccessor
Bases: object
Provides full access to the Configuration
As the functionality and featureset is very different from the original ConfigParser implementation, this class does not support the interface directly. It contains functions to create original ConfigParser able to fully write and alter the contained data in an unchecked manner.
Additional Exceptions have been defined to cover extended functionality.
Note: | The configaccessor should only be used in conjunction with the ConfigManager |
---|
Copy all our members into a new ConfigAccessor which only has one node, instead of N nodes
By default, a configuration can be made up of several different sources that create a chain. Each source can redefine and alter values previously defined by other sources.
A flattened chain though does only conist of one of such node containing concrete values that can quickly be accessed.
Flattened configurations are provided by the ConfigManager.
Parameter: | fp – file-like object that will be used as storage once the configuration is written |
---|---|
Returns: | Flattened copy of self |
Convenience function allowing to easily specify the key you wish to retrieve with the option to provide a default value
Parameters: |
|
---|---|
Returns: | Key instance whose value may be queried through its value or values attributes |
Returns: | True if the given section exists |
---|
Returns: | True if the accessor does not stor information |
---|
Convenience Function: get key with keyname in first section with sectionname with the key’s value being initialized to value if it did not exist.
Parameters: |
|
---|---|
Returns: | Key |
Returns: | iterator returning tuples of (Key,`Section`) pairs |
---|
Parameter: | name – the name of the key you wish to find |
---|---|
Returns: | List of (Key,`Section`) tuples of key(s) matching name found in section, or empty list |
Merge and/or add the given section into our chain of nodes. The first writable node will be used
Raises IOError: | if no writable node was found |
---|---|
Returns: | name of the file source that has received the section |
Read the configuration from the file like object(s) representing INI files.
Note: | This will overwrite and discard all existing configuration. |
---|---|
Parameters: |
|
Raises ConfigParsingError: | |
Completely remove the given section name from all nodes in our configuration
Returns: | the number of nodes that did not allow the section to be removed as they are read-only, thus 0 will be returned if everything was alright |
---|
Returns: | first section with name |
---|---|
Note: | as there might be several nodes defining the section for inheritance, you might not get the desired results unless this config accessor acts on a flatten ed list. |
Raises NoSectionError: | |
if the requested section name does not exist |
Returns: | section with given name. |
---|---|
Raises IOError: | If section does not exist and it cannot be created as the configuration is readonly |
Note: | the section will be created if it does not yet exist |
Returns: | iterator returning all sections |
---|
Parameter: | close_fp – close the file-object after writing to it |
---|---|
Returns: | list of names of files that have actually been written - as files can be read-only this list might be smaller than the amount of nodes in the accessor. |
Bases: list
A chain of config nodes
This utility class keeps several ConfigNode objects, but can be operated like any other list.
Note: | this solution is mainly fast to implement, but a linked-list like behaviour is intended |
---|
Raises NotImplementedError: | |
---|---|
Parameter: | name – the name of the key you wish to find |
---|---|
Returns: | Iterator yielding (Key,`Section`) of key matching name found in section |
Returns: | iterator returning tuples of (key,section) pairs |
---|
Returns: | section iterator for whole configuration chain |
---|
Raises NotImplementedError: | |
---|---|
Bases: mrv.conf.DiffData
Compares two configuration objects and allows retrieval of differences
Use this class to find added/removed sections or keys or differences in values and properties.
- DiffSection.added|removed|unchanged: Key objects that have been added, removed or kept unchanged respectively
- DiffSection.changed: DiffKey objects that indicate the changes in the repsective key
- DiffKey.added|removed: the key’s values that have been added and/or removed respectively
- DiffKey.properties: see DiffSection.properties
- DiffSection.properties:None if this is a section diff, otherwise it contains a DiffSection with the respective differences
Apply the stored differences in this ConfigDiffer instance to the given ConfigAccessor
If our diff contains the changes of A to B, then applying ourselves to A would make A equal B.
Note: | individual nodes reqpresenting an input source (like a file) can be marked read-only. This means they cannot be altered - thus it can be that section or key removal fails for them. Addition of elements normally works as long as there is one writable node. |
---|---|
Parameter: | ca – The configacceesor to apply our differences to |
Returns: | tuple of lists containing the sections that could not be added, removed or get their changes applied
|
Returns: | true if we have stored differences (A is not equal to B) |
---|
Bases: mrv.conf.ExtendedFileInterface
file object implementation of the ExtendedFileInterface
Returns: | True if the file is truly writable |
---|
Epydoc: mrv.conf.ConfigManager
Bases: object
Cache Configurations for fast access and provide a convenient interface
The the ConfigAccessor has limited speed due to the hierarchical nature of configuration chains. The config manager flattens the chain providing fast access. Once it is being deleted or if asked, it will find the differences between the fast cached configuration and the original one, and apply the changes back to the original chain, which will then write the changes back (if possible).
This class should be preferred over the direct congiguration accessor. This class mimics the ConfigAccessor inteface as far as possible to improve ease of use. Use self.config to directly access the configuration through the ConfigAccessor interface
To use this class, read a list of ini files and use configManager.config to access the configuration.
For convenience, it will wire through all calls it cannot handle to its ConfigAccessor stored at .config
Read the configuration from the file pointers.
Raises ConfigParsingError: | |
---|---|
Parameter: | filefporlist – single file like object or list of such |
Returns: | the configuration that is meant to be used for accessing the configuration |
Finds tagged configuration files in given directories and return them.
The files retrieved can be files like “file.ext” or can contain tags. Tags are ‘.’ separated files tags that are to be matched with the tags in taglist in order.
All tags must match to retrieve a filepointer to the respective file.
Example Usage: you could give two paths, one is a global one in a read-only location, another is a local one in the user’s home (where you might have precreated a file already).
The list of filepointers returned would be all matching files from the global path and all matching files from the local one, sorted such that the file with the smallest amount of tags come first, files with more tags (more specialized ones) will come after that.
If fed into the readfp or the __init__ method, the individual file contents can override each other. Once changes have been applied to the configuration, they can be written back to the writable file pointers respectively.
Parameters: |
|
---|
Write the possibly changed configuration back to its sources.
Raises IOError: | if at least one node could not be properly written. |
---|---|
Raises ValueError: | |
if instance is not properly initialized. | |
Note: | It could be the case that all nodes are marked read-only and thus cannot be written - this will also raise as the request to write the changes could not be accomodated. |
Returns: | the names of the files that have been written as string list |
Bases: object
Represents node in the configuration chain
It keeps information about the origin of the configuration and all its data. Additionally, it is aware of it being element of a chain, and can provide next and previous elements respectively
Returns: | True if the given section exists |
---|
Returns: | list() with string names of available sections |
---|---|
Todo: | return an iterator instead |
parse default INI information into the extended structure
Parse the given INI file using a _FixedConfigParser, convert all information in it into an internal format
Raises ConfigParsingError: | |
---|---|
Returns: | Section with name |
---|---|
Raises NoSectionError: | |
Returns: | Section with name, create it if required |
---|
Write our contents to our file-like object
Parameter: | rcp – RawConfigParser to use for writing |
---|---|
Returns: | the name of the written file |
Raises IOError: | if we are read-only |
Epydoc: mrv.conf.ConfigStringIO
Bases: StringIO.StringIO, mrv.conf.ExtendedFileInterface
cStringIO object implementation of ExtendedFileInterface
Retrieve the entire contents of the “file” at any time before the StringIO object’s close() method is called.
The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. If both are used, 8-bit strings that cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause a UnicodeError to be raised when getvalue() is called.
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes).
If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
Read one entire line from the file.
A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned.
An empty string is returned only when EOF is encountered immediately.
Note: Unlike stdio’s fgets(), the returned string contains null characters (‘0’) if they occurred in the input.
Read until EOF using readline() and return a list containing the lines thus read.
If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (or more to accommodate a final whole line).
Set the file’s current position.
The mode argument is optional and defaults to 0 (absolute file positioning); other values are 1 (seek relative to the current position) and 2 (seek relative to the file’s end).
There is no return value.
Truncate the file’s size.
If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed unless the position is beyond the new file size.
If the specified size exceeds the file’s current size, the file remains unchanged.
Write a string to the file.
There is no return value.
Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
(The name is intended to match readlines(); writelines() does not add line separators.)
Epydoc: mrv.conf.DictConfigINIFile
Bases: mrv.conf.DictToINIFile, mrv.conf.ExtendedFileInterface
dict file object implementation of ExtendedFileInterface
Retrieve the entire contents of the “file” at any time before the StringIO object’s close() method is called.
The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. If both are used, 8-bit strings that cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause a UnicodeError to be raised when getvalue() is called.
Returns: | True if the file can be written to |
---|
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes).
If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
Read one entire line from the file.
A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned.
An empty string is returned only when EOF is encountered immediately.
Note: Unlike stdio’s fgets(), the returned string contains null characters (‘0’) if they occurred in the input.
Read until EOF using readline() and return a list containing the lines thus read.
If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (or more to accommodate a final whole line).
Set the file’s current position.
The mode argument is optional and defaults to 0 (absolute file positioning); other values are 1 (seek relative to the current position) and 2 (seek relative to the file’s end).
There is no return value.
Truncate the file’s size.
If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed unless the position is beyond the new file size.
If the specified size exceeds the file’s current size, the file remains unchanged.
Write a string to the file.
There is no return value.
Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
(The name is intended to match readlines(); writelines() does not add line separators.)
Epydoc: mrv.conf.DictToINIFile
Bases: StringIO.StringIO
Wraps a dictionary into an objects returning an INI file when read
This class can be used to make configuration information as supplied by os.environ natively available to the configuration system
Note: | writing back values to the object will not alter the original dict |
---|---|
Note: | the current implementation caches the dict’s INI representation, data is not generated on demand |
Note: | implementation speed has been preferred over runtime speed |
Retrieve the entire contents of the “file” at any time before the StringIO object’s close() method is called.
The StringIO object can accept either Unicode or 8-bit strings, but mixing the two may take some care. If both are used, 8-bit strings that cannot be interpreted as 7-bit ASCII (that use the 8th bit) will cause a UnicodeError to be raised when getvalue() is called.
Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes).
If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately.
Read one entire line from the file.
A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned.
An empty string is returned only when EOF is encountered immediately.
Note: Unlike stdio’s fgets(), the returned string contains null characters (‘0’) if they occurred in the input.
Read until EOF using readline() and return a list containing the lines thus read.
If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (or more to accommodate a final whole line).
Set the file’s current position.
The mode argument is optional and defaults to 0 (absolute file positioning); other values are 1 (seek relative to the current position) and 2 (seek relative to the file’s end).
There is no return value.
Truncate the file’s size.
If the optional size argument is present, the file is truncated to (at most) that size. The size defaults to the current position. The current file position is not changed unless the position is beyond the new file size.
If the specified size exceeds the file’s current size, the file remains unchanged.
Write a string to the file.
There is no return value.
Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
(The name is intended to match readlines(); writelines() does not add line separators.)
Bases: object
Struct keeping data about added, removed and/or changed data Subclasses should override some private methods to automatically utilize some basic functionality
Returns: | true if we have stored differences (A is not equal to B) |
---|
Bases: mrv.conf.DiffData
Implements DiffData on Key level
Returns: | true if we have stored differences (A is not equal to B) |
---|
Bases: mrv.conf.DiffData
Implements DiffData on section level
Returns: | true if we have stored differences (A is not equal to B) |
---|
Epydoc: mrv.conf.ExtendedFileInterface
Bases: object
Define additional methods required by the Configuration System :warning: Additionally, readline and write must be supported - its not mentioned here for reasons of speed :note: override the methods with implementation
Returns: | True if the file has been closed, and needs to be reopened for writing |
---|
Returns: | True if the file can be written to |
---|
Returns: | a name for the file object |
---|
Bases: mrv.conf._PropertyHolderBase
Key with an associated values and an optional set of propterties
Note: | a key’s value will be always be stripped if its a string |
---|---|
Note: | a key’s name will be stored stripped only, must not contain certain chars |
Todo: | add support for escpaing comas within quotes - currently it split at comas, no matter what |
Append the given value or list of values to the list of current values
Parameter: | value – list, tuple or scalar value |
---|---|
Todo: | this implementation could be faster (costing more code) |
Merge self with otherkey according to our properties
Note: | self will be altered |
---|
remove the given value or list of values from the list of current values
Parameter: | value – list, tuple or scalar value |
---|---|
Todo: | this implementation could be faster (costing more code) |
Epydoc: mrv.conf.PropertySection
Bases: mrv.conf.Section
Define a section containing keys that make up properties of somethingI
Returns: | Key with name |
---|---|
Raises NoOptionError: | |
Parameter: | value – anything supported by setKey |
---|---|
Returns: | tuple: 0 = Key with name, create it if required with given value, 1 = true if newly created, false otherwise |
Merge our section with othersection
:note:self will be altered
Set the value to key with name, or create a new key with name and value
Parameter: | value – int, long, float, string or list of any of such |
---|---|
Raises ValueError: | |
if key has incorrect value |
Create a new section in the configuration.
Raise DuplicateSectionError if a section by the specified name already exists. Raise ValueError if name is DEFAULT or any of it’s case-insensitive variants.
Indicate whether the named section is present in the configuration.
The DEFAULT section is not acknowledged.
Read and parse a filename or a list of filenames.
Files that cannot be opened are silently ignored; this is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user’s home directory, systemwide directory), and all existing configuration files in the list will be read. A single filename may also be given.
Return list of successfully read files.
Like read() but the argument must be a file-like object.
The `fp’ argument must have a `readline’ method. Optional second argument is the `filename’, which if not given, is taken from fp.name. If fp has no `name’ attribute, `<???>’ is used.
Bases: mrv.conf._PropertyHolderBase
Class defininig an indivual section of a configuration file including all its keys and section properties
Note: | name will be stored stripped and must not contain certain chars |
---|
Returns: | Key with name |
---|---|
Raises NoOptionError: | |
Parameter: | value – anything supported by setKey |
---|---|
Returns: | tuple: 0 = Key with name, create it if required with given value, 1 = true if newly created, false otherwise |
Merge our section with othersection
:note:self will be altered
Set the value to key with name, or create a new key with name and value
Parameter: | value – int, long, float, string or list of any of such |
---|---|
Raises ValueError: | |
if key has incorrect value |
Epydoc: mrv.conf._FixedConfigParser
Bases: ConfigParser.RawConfigParser
The RawConfigParser stores options lowercase - but we do not want that and keep the case - for this we just need to override a method
Create a new section in the configuration.
Raise DuplicateSectionError if a section by the specified name already exists. Raise ValueError if name is DEFAULT or any of it’s case-insensitive variants.
Indicate whether the named section is present in the configuration.
The DEFAULT section is not acknowledged.
Read and parse a filename or a list of filenames.
Files that cannot be opened are silently ignored; this is designed so that you can specify a list of potential configuration file locations (e.g. current directory, user’s home directory, systemwide directory), and all existing configuration files in the list will be read. A single filename may also be given.
Return list of successfully read files.
Like read() but the argument must be a file-like object.
The `fp’ argument must have a `readline’ method. Optional second argument is the `filename’, which if not given, is taken from fp.name. If fp has no `name’ attribute, `<???>’ is used.
Epydoc: mrv.conf.ConfigParsingError
Bases: mrv.exc.MRVError
Indicates that the parsing failed
Epydoc: mrv.conf.ConfigParsingPropertyError
Bases: mrv.conf.ConfigParsingError
Indicates that the property-parsing encountered a problem
Bases: exceptions.Exception
Base Class for all exceptions that the mrv framework throws
Bases: ConfigParser.Error
A requested option was not found.