1 """A simple wrapper around optparse and configobj
3 >>> from cfg import o, setup_cfg
5 o('-c', '--conf', dest='conf',
6 help='Configuration file'),
9 >>> print cfg['bar.yaht']
13 from os
.path
import dirname
, abspath
16 from optparse
import make_option
as o
, OptionParser
21 # ConfigObj does not support passing custom template variables like in
22 # most HTML templating systems. This is bad.
23 # ConfigObj code is also not very extensible. This is very bad.
24 # So we hack around the `interpolation_engines` global variable to
27 def configobj_namespace(namespace
):
31 class GoodConfigParserInterpolation(configobj
.ConfigParserInterpolation
):
32 """Resolve custom template variables manually passed"""
34 def interpolate(self
, key
, value
):
36 return super(GoodConfigParserInterpolation
,
37 self
).interpolate(key
, value
)
38 except configobj
.InterpolationError
:
39 # eg: extract 'var' out of '%(var)s'
40 match
= self
._KEYCRE
.search(value
)
44 return re
.sub(self
._KEYCRE
, namespace
[var
], value
)
47 # There is another engine 'template' which, for me, never seems to
49 configobj
.interpolation_engines
['configparser'] = \
50 GoodConfigParserInterpolation
53 def married(options
, configobj
):
55 Marry optparse and configobj
56 http://wiki.python.org/moin/ConfigParserShootout
61 def __setitem__(self
, item
, value
):
62 return setattr(options
, item
, value
)
64 def __getitem__(self
, item
):
65 # If `item` is not found in `options` read from `configobj`
67 value
= getattr(options
, item
)
72 except AttributeError:
75 for a
in item
.split('.'):
78 return None # optparse, too, returns None in this case.
82 return "<cfg {\n%s,\n\n%s\n}>" % (options
, configobj
)
86 def setup_cfg(option_list
, defaults
, namespace
={}):
89 oparser
= OptionParser(option_list
=option_list
)
90 oparser
.set_defaults(**defaults
)
91 (options
, args
) = oparser
.parse_args()
93 if options
.conf
is None:
94 raise SystemExit, "You must specify the --conf option."
96 # `pwd` contains the directory where the conf file lies.
97 namespace
['pwd'] = abspath(dirname(abspath(options
.conf
)))
100 configobj_namespace(namespace
)
102 cobj
= configobj
.ConfigObj(options
.conf
)
103 cfg
= married(options
, cobj
)
106 __all__
= ['setup_cfg', 'o', 'cfg']