cmiputil.config module

Maintain config file for cmiputil package.

Config file format for this package is so called ‘INI file’, handled by python standard configparser module, so you can use interpolation of values.

Default name of config file is cmiputil.conf (set as conf_name), and is searched in the order below:

  1. specified by the argument of Conf()

  2. specified by the environment variable CMIPUTIL_CONFFILE

  3. $CMIPUTIL_CONFDIR/cmiputil.conf

  4. $cwd/cmiputil.conf

  5. $HOME/cmiputil.conf

Once found, the rest is skipped.

If file given to the Conf() is None, no config file is read and blank instance is created. If you want to read the default config file only, leave file="" as a default.

The name of “Common” section, which any module in this package may access, is common_sect_name and the key=value pair is common_config.

You can create sample(default) config file by createSampleConf program, that collects default configuration of each module by getDefaultConf() method defined in each module(class) in this package, and write them to the file by Conf.read_dict() and Conf.writeConf().

class config.Conf(file='')[source]

Bases: configparser.ConfigParser

Config parser for this package.

If file is given, this must be a string or a path-like object, specifying config file to be read first (See above).

See configparser.ConfigParser for other methods and details.

file

config file had read in.

Type

Path

commonSection

“Common” section for this package.

Type

SectionProxy

setCommonSection()[source]

Set default “common” section.

Warning

Do not call this after reading real config file.

writeConf(file, overwrite=False)[source]

Write current attributes to the file, this must be a string or a path-like.

If given file exists, FileExistsError is raised unless overwrite is True.

Do not forget to call setCommonSection() to include common section to write.

Examples

>>> from cmiputil import config, esgfsearch
>>> conf = config.Conf(None)
>>> conf.setCommonSection()
>>> d = esgfsearch.getDefaultConf()
>>> conf.read_dict(d)
>>> conf.writeConf('/tmp/cmiputil.conf', overwrite=True)

After above example, /tmp/cmiputil.conf is as below:

[cmiputil]
cmip6_data_dir = /data

[ESGFSearch]
search_service = http://esgf-node.llnl.gov/esg-search/
aggregate = True

[ESGFSearch.keywords]
replica = false
latest = true

[ESGFSearch.facets]
table_id = Amon
config.common_config = {'cmip6_data_dir': '/data'}

Default configuration of the ‘common’ section.

config.common_sect_name = 'cmiputil'

Name of the ‘common’ section.

config.conf_dir = ['~/', './']

Directory list for searching a config file.

Overriden by an environment variable CMIPUTIL_CONFDIR if set. The order is important since the latter searched first.

config.conf_name = 'cmiputil.conf'

Name of the config file.