Package mrv :: Module conf :: Class ConfigAccessor
[hide private]
[frames] | no frames]

Class ConfigAccessor

source code

object --+
         |
        ConfigAccessor

Provides full access to the Configuration

Differences to ConfigParser:

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.

Sources and Nodes:
Each input providing configuration data is stored in a node. This node knows about its writable state. Nodes that are not writable can be altered in memory, but the changes cannot be written back to the source. This does not impose a problem though as changes will be applied as long as there is one writable node in the chain - due to the inheritance scheme applied by the configmanager, the final configuration result will match the changes applied at runtime.
Additional Information:
The term configuration is rather complex though configuration is based on an extended INI file format its not fully compatible, but slightly more narrow regarding allowed input to support extended functionality configuration is read from file-like objects a list of file-like objects creates a configuration chain keys have properties attached to them defining how they behave when being overridden once all the INI configurations have been read and processed, one can access the configuration as if it was just in one file. Direct access is obtained though Key and Section objects Keys and Sections have property attributes of type Section Their keys and values are used to further define key merging behaviour for example

Note: The configaccessor should only be used in conjunction with the ConfigManager

Instance Methods [hide private]
 
__init__(self)
Initialize instance variables
source code
 
__repr__(self)
repr(x)
source code
 
_parseProperties(self)
Analyse the freshly parsed configuration chain and add the found properties to the respective sections and keys
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

    IO Interface
 
readfp(self, filefporlist, close_fp=True)
Read the configuration from the file like object(s) representing INI files.
source code
 
write(self, close_fp=True)
During initialization in readfp, ExtendedFileInterface objects have been passed in - these will now be used to write back the current state of the configuration - the files will be opened for writing if possible.
source code
    Transformations
 
flatten(self, fp)
Copy all our members into a new ConfigAccessor which only has one node, instead of N nodes
source code
    Iterators
 
sectionIterator(self)
Returns: iterator returning all sections
source code
 
keyIterator(self)
Returns: iterator returning tuples of (Key,`Section`) pairs
source code
    Utitlities
 
isEmpty(self)
Returns: True if the accessor does not stor information
source code
    General Access (disregarding writable state)
 
hasSection(self, name)
Returns: True if the given section exists
source code
 
section(self, section)
Returns: first section with name
source code
 
keyDefault(self, sectionname, keyname, value)
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.
source code
 
keysByName(self, name)
Returns: List of (Key,`Section`) tuples of key(s) matching name found in section, or empty list
source code
 
iterateKeysByName(self, name)
As keysByName, but returns an iterator instead
source code
 
get(self, key_id, default=None)
Convenience function allowing to easily specify the key you wish to retrieve with the option to provide a default value
source code
    Operators
 
__getitem__(self, key) source code
    Structure Adjustments Respecting Writable State
 
sectionDefault(self, section)
Returns: section with given name.
source code
 
removeSection(self, name)
Completely remove the given section name from all nodes in our configuration
source code
 
mergeSection(self, section)
Merge and/or add the given section into our chain of nodes.
source code
Class Methods [hide private]
 
_isProperty(cls, propname)
Returns: true if propname appears to be an attribute
source code
 
_getNameTuple(cls, propname)
Returns: [sectionname,keyname], sectionname can be None
source code
Properties [hide private]
  _configChain

Inherited from object: __class__

Method Details [hide private]

__init__(self)
(Constructor)

source code 
Initialize instance variables
Overrides: object.__init__

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

_isProperty(cls, propname)
Class Method

source code 
Returns:
true if propname appears to be an attribute

_getNameTuple(cls, propname)
Class Method

source code 
Returns:
[sectionname,keyname], sectionname can be None

_parseProperties(self)

source code 
Analyse the freshly parsed configuration chain and add the found properties to the respective sections and keys
Raises:

Note: we are userfriendly regarding the error handling - if there is an invlid property, we warn and simply ignore it - for the system it will stay just a key and will thus be written back to the file as required

readfp(self, filefporlist, close_fp=True)

source code 
Read the configuration from the file like object(s) representing INI files.
Parameters:
  • filefporlist - single file like object or list of such
  • close_fp - if True, the file-like object will be closed before the method returns, but only for file-like objects that have actually been processed
Raises:

Note: This will overwrite and discard all existing configuration.

write(self, close_fp=True)

source code 
Write current state back to files.
During initialization in readfp, ExtendedFileInterface objects have been passed in - these will now be used to write back the current state of the configuration - the files will be opened for writing if possible.
Parameters:
  • 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.

flatten(self, fp)

source code 

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.

Parameters:
  • fp - file-like object that will be used as storage once the configuration is written
Returns:
Flattened copy of self

sectionIterator(self)

source code 
Returns:
iterator returning all sections

keyIterator(self)

source code 
Returns:
iterator returning tuples of (Key,`Section`) pairs

isEmpty(self)

source code 
Returns:
True if the accessor does not stor information

hasSection(self, name)

source code 
Returns:
True if the given section exists

section(self, section)

source code 
Returns:
first section with name
Raises:
  • NoSectionError - if the requested section name does not exist

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.

keyDefault(self, sectionname, keyname, value)

source code 
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:
  • sectionname - the name of the sectionname the key is supposed to be in - it will be created if needed
  • keyname - the name of the key you wish to find
  • value - the value you wish to receive as as default if the key has to be created. It can be a list of values as well, basically anything that Key allows as value
Returns:
Key

keysByName(self, name)

source code 
Parameters:
  • 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

get(self, key_id, default=None)

source code 
Convenience function allowing to easily specify the key you wish to retrieve with the option to provide a default value
Parameters:
  • key_id - string specifying a key, either as sectionname.keyname or keyname. In case you specify a section, the key must reside in the given section, if only a keyname is given, it may reside in any section
  • default - Default value to be given to a newly created key in case there is no existing value. If None, the method may raise in case the given key_id does not exist.
Returns:
Key instance whose value may be queried through its value or values attributes

sectionDefault(self, section)

source code 
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

removeSection(self, name)

source code 
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

mergeSection(self, section)

source code 
Merge and/or add the given section into our chain of nodes. The first writable node will be used
Returns:
name of the file source that has received the section
Raises:
  • IOError - if no writable node was found