I'm using ConfigParser to load in data from a configuration file as follows:
test.conf:
[myfiles]
fileone: %(datadir)s/somefile.foo
filetwo: %(datadir)s/nudderfile.foo
load.py:
import ConfigParser
config = ConfigParser.ConfigParser({'datadir': '/tmp'})
config.read('test.conf')
print config.items('myfiles')
print config.get('myfiles', 'datadir')
Output:
$ python load.py
[('datadir', '/tmp'), ('filetwo', '/tmp/nudderfile.foo'), ('fileone', '/tmp/somefile.foo')]
/tmp
I'm surprised that the defaults for variable substitution ('datadir', '/tmp') are showing up as part of the .items() and .get() returns, as if they were values in the configuration file. Is this behavior expected? Any work arounds, so that I can simply iterate .items() without getting the default dictionary values in there, but still using the magic interpolation?
Reference: http://docs.python.org/library/configparser.html
Thanks!
Update: It's been pointed out that this is the expected behavior: defaults are just like any other name/value pair in the configuration file. Similarly, the name/value pairs in the configuration file are also available for "magic interpolation", so if I define:
foo: bar
zap: %(foo)snowl
I'll get [... ('zap': 'barnowl')]
That's pretty neat, but I'm still wondering if I can accomplish what I want to accomplish: iterate over the name/value pairs in my configuration files, with interpolation of variables, without the defaults.
My specific scenario is this: I wanted to initialize the config object with something like {basedir: '/foo/bar'}, as the absolute paths to certain files varie