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

Class ConfigManager

source code

object --+
         |
        ConfigManager

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

Instance Methods [hide private]
 
__init__(self, filePointers=[], write_back_on_desctruction=True, close_fp=True)
Initialize the class with a list of Extended File Classes
source code
 
__del__(self)
If we are supposed to write back the configuration, after merging the differences back into the original configuration chain
source code
 
__getattr__(self, attr)
Wire all queries we cannot handle to our config accessor
source code

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

    IO Methods
 
write(self)
Write the possibly changed configuration back to its sources.
source code
 
readfp(self, filefporlist, close_fp=True)
Read the configuration from the file pointers.
source code
Class Methods [hide private]
    Utilities
 
taggedFileDescriptors(cls, directories, taglist, pattern=None)
Finds tagged configuration files in given directories and return them.
source code
Properties [hide private]
  __config
  _closeFp
  _writeBackOnDestruction
  config

Inherited from object: __class__

Method Details [hide private]

__init__(self, filePointers=[], write_back_on_desctruction=True, close_fp=True)
(Constructor)

source code 
Initialize the class with a list of Extended File Classes
Parameters:
  • filePointers (ExtendedFileInterface) - Point to the actual configuration to use If not given, you have to call the readfp function with filePointers respectively
  • close_fp - if true, the files will be closed and can thus be changed. This should be the default as files might be located on the network as shared resource
  • write_back_on_desctruction - if True, the config chain and possible changes will be written once this instance is being deleted. If false, the changes must explicitly be written back using the write method
Overrides: object.__init__

write(self)

source code 
Write the possibly changed configuration back to its sources.
Returns:
the names of the files that have been written as string list
Raises:
  • IOError - if at least one node could not be properly written.
  • 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.

readfp(self, filefporlist, close_fp=True)

source code 
Read the configuration from the file pointers.
Parameters:
  • filefporlist - single file like object or list of such
Returns:
the configuration that is meant to be used for accessing the configuration
Raises:

taggedFileDescriptors(cls, directories, taglist, pattern=None)
Class Method

source code 

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:
  • directories - [string(path) ...] of directories to look in for files
  • taglist - [string(tag) ...] of tags, like a tag for the operating system, or the user name
  • pattern - simple fnmatch pattern as used for globs or a list of them (allowing to match several different patterns at once)