|
From: Fuzzyman <fuz...@vo...> - 2005-12-05 12:06:54
|
Setting defaults.
There are two easy ways that spring to mind to set default values in a
config file. (Sorry about the top post - but it seemed the most
appropriate way to respond).
First off you could create an external config file (or dictionary) with
all your default values.
Create the initial config file from the defaults. Then *update* with the
user one. Only values et in the user config will over write the values
from the default one. You may wish to change the filename of the
resulting config : ::
cfg = ConfigObj(default_filename) # filename or dictionary
cfg2 = ConfigObj(user_config)
cfg.update(cfg2)
cfg.filename = cfg2.filename
That *probably* achieves what you want.
*However* - validation includes a system for setting default values. You
do this by including the default in the configspec.
See :
http://www.voidspace.org.uk/python/configobj.html#mentioning-default-values
http://www.voidspace.org.uk/python/validate.html#default-values
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml
Matthew Brett wrote:
>Hi,
>
>Just another thought. I have been browsing the different suggested
>replacements for ConfigParser listed on the shootout page:
>
>http://wiki.python.org/moin/ConfigParserShootout
>
>ConfigObj certainly seems to get me most of the way there, but one
>feature that I really miss is the ability to merge different
>configuration files, while stlll falling back to defaults for settings
>not found in the files. This is one of the desirable features listed
>for the ConfigParser replacement:
>
>http://wiki.python.org/moin/ConfigParserShootout#head-a1bf82289c05f5749e5b0c6bea97f6b1d35c05cd
>
>For example, let us say I have a system configuration file, a local
>user configuration file and a file in the current working directory:
>
>cfg_files = [sys_cfg, user_cfg, pwd_cfg]
>
>I would like to be able to take any settings from pwd_cfg, then get
>any settings missing in pwd_cfg from user_cfg, then any still missing
>from sys_cfg, and any remaining options from my hardcoded defaults.
>
>In fact, this is very nice feature of cfgparse:
>
>http://cfgparse.sourceforge.net/
>
> - you can do something like:
>
>p = cfgparse.ConfigParser()
>p.add_option('some_option', default=True)
># etc
>for f in cfg_files:
> if os.path.exists(_f):
> p.add_file(_f)
>opts = p. parse()
>
>I would really like to be able to do something similar with ConfigObj,
>but my current approach is a little cludgy. At the moment, I think I
>have a) avoid setting defaults in my configspec b) make a separate
>options string list with my defaults, and c) apply my own merge
>function (see code snippet below). Is there a better way to do this?
>
>Thanks again,
>
>Matthew
>
>
># Code snippet:
>
>def soft_merge(a, b):
> """
> Fills values recursively in dictionary a from values in dictionary
> b. Any fields missing or None in a but present in b will be
> filled from b. Fields which are dictionaries (objects with
> iteritems method) will be filled recursively. We prefer a
> dictionary field in either a or b to a non-dictionary.
>
> """
> if a is None: return b
> try:
> b_iter = b.iteritems()
> except:
> return a
>
> try:
> a_iter = a.iteritems()
> except:
> return b
>
> for (k,v) in b_iter:
> if a.has_key(k):
> a[k] = soft_merge(a[k], v)
> else:
> a[k] = v
>
> return a
>
>cfg_spec = '''
>[my_section]
>setting1 = boolean()
>setting2 = boolean()
>""".split('\n')
>
>default_cfg = """
>[my_section]
>setting1 = True
>setting2 = False
>""".split('\n')
>
>cfg_obj = configobj.ConfigObj;
>opts = cfg_obj(None, configspec=cfg_spec)
>cfg_files.append(default_cfg)
>for f in cfg_files:
> opts = soft_merge(opts, cfg_obj(f))
>
>
>-------------------------------------------------------
>This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
>for problems? Stop! Download the new AJAX search engine that makes
>searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
>http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
>_______________________________________________
>Configobj-develop mailing list
>Con...@li...
>https://lists.sourceforge.net/lists/listinfo/configobj-develop
>
>
>
|