lazyloader
Deprecated (13 Aug 2006)
The lazyloader module is accessible via the initools module.
A config file loader that can load and nest multiple config files, the config files can have structure, and the values can be tracked back to their original file and line number.
To start, you'd do something like:
>>> config = LazyLoader()
You could use .load(filename) to load a config file; for the examples it is convenient to instead use loadstring, and give a fake filename:
>>> config_data = """
... [server]
... port = 8000
... host = localhost
... document_root = /var/www
... """
>>> config.loadstring(config_data, filename='config_data.conf')
>>> config['server']['port']
'8000'
>>> config['server'].convert('port', int)
8000
>>> config['server'].convert('host', int)
Traceback (most recent call last):
...
ValueError: Error in config_data.conf (section [server]), line 4 ('localhost'):
ValueError: invalid literal for int(): localhost
Note that names are normalized, removing case, underscores, and spaces. So to get to the document root:
>>> config['server']['documentroot'] '/var/www'
You can also merge in values; for instance, consider a virtual host that overrides global values:
>>> vhost_data = """ ... [vhost(my.host.com)] ... document_root = /path/to/root ... """ >>> vhost_config = LazyLoader() >>> vhost_config.loadstring(vhost_data, filename='vhost_data.conf') >>> vhost_config['vhost'].keys() ['my.host.com']
Note that key and section names can be nested with .'s, and ()'s quote the values (so the key is ['my.host.com'] instead of ['my']['host']['com']). Then we may want to merge this in, based on a condition (e.g., the hostname matches my.host.com):
>>> config['server'].merge(vhost_config['vhost']['my.host.com']) >>> config['server']['documentroot'] '/path/to/root'
Attributes
a __warningregistry__
{('initools.lazyloader is not supported or recommended for use.', <class exceptions.DeprecationWarning at 0xb7d31efc>, 65): 1}
Classes
C LazyLoader(...) ...
This class contains 15 members.
See the source for more information.