Other api/dbconf

Table Of Contents

Previous topic


Next topic


This Page

Daya Bay Links

Content Skeleton



When invoked as a script determines if the configuration named in the single argument exists.

Usage example:

python path/to/dbconf.py configname  && echo configname exists || echo no configname


class DybPython.dbconf.DBConf(sect=None, path=None, user=None, pswd=None, url=None, host=None, db=None, fix=None, fixpass=None, restrict=None, verbose=False, secure=False, from_env=False, nodb=False)[source]

Bases: dict

Reads a section of the Database configuration file, storing key/value pairs into this dict. The default file path is ~/.my.cnf which is formatted like:

host      = dybdb1.ihep.ac.cn
database  = testdb
user      = dayabay
password  = youknowoit

The standard python ConfigParser is used, which supports %(name)s style replacements in other values.

Usage example:

from DybPython import DBConf
dbc = DBConf(sect="client", path="~/.my.cnf" ) 
print dbc['host']

dbo = DBConf("offline_db")
assert dbo['host'] == "dybdb1.ihep.ac.cn"


As passwords are contained DO NOT COMMIT into any repository, and protect the file.

See also Running section of the Offline User Manual

Interpolates the DB connection parameter patterns gleaned from arguments, envvars or defaults (in that precedence order) into usable values using the context supplied by the sect section of the ini format config file at path

Optional keyword arguments:

Keyword Description
sect section in config file
path colon delimited list of paths to config file
user username
pswd password
url connection url
host db host
db db name
fix triggers fixture loading into temporary spawned cascade and specifies paths to fixture files for each member of the cascade (semi-colon delimited)
fixpass skip the DB cascade dropping/creation that is normally done as part of cascade spawning (used in DBWriter/tests)
restrict constrain the names of DB that can connect to starting with a string, eg tmp_ as a safeguard
nodb used to connect without specifying the database this requires greater access privileges and is used to perform database dropping/creation

Correspondingly named envvars can also be used:


The DBCONF existance also triggers the DybPython.dbconf.DBConf.Export() in dybgaudi:Database/DatabaseInterface/src/DbiCascader.cxx

The DBCONF_PATH is a colon delimited list of paths that are user (~) and $envvar OR ${envvar} expanded, some of the paths may not exist. When there are repeated settings in more than one file the last one wins.

In secure mode a single protected config file is required, the security comes with a high price in convenience

classmethod Export(sect=None, **extras)[source]

Exports the environment settings into environment of python process this is invoked by the C++ DbiCascader ctor

configure_cascade(sect, path)[source]

Interpret the sect argument comprised of a either a single section name eg offline_db or a colon delimited list of section names eg tmp_offline_db:offline_db to provide easy cascade configuration. A single section is of course a special case of a cascade. The first(or only) section in zeroth slot is treated specially with its config parameters being propagated into self.

Caution any settings of url, user, pswd, host, db are overridden when the sect argument contains a colon.


Exports the interpolated configuration into corresponding DBI envvars :

ENV_TSQL_USER ENV_TSQL_PSWD ENV_TSQL_URL ENV_TSQL_FIX (added to allow DBConf to survive thru the env-glass )

And DatabaseSvc envvars for access to non-DBI tables via DatabaseSvc :

classmethod from_env()[source]

Construct DBConf objects from environment :

classmethod has_config(name_=None)[source]

Returns if the named config is available in any of the available DBCONF files

For cascade configs (which comprise a colon delimited list of section names) all the config sections must be present.

As this module exposes this in its main, config sections can be tested on command line with:

./dbconf.py offline_db && echo y || echo n 
./dbconf.py offline_dbx && echo y || echo n  
./dbconf.py tmp_offline_db:offline_db && echo y || echo n
./dbconf.py tmp_offline_dbx:offline_db && echo y || echo n

Using the nodb=True option skips database name parameter, this is useful when creating or dropping a database

classmethod prime_parser()[source]

Prime parser with “today” to allow expansion of %(today)s in ~/.my.cnf allowing connection to a daily recovered database named after todays date

classmethod read_cfg(path=None)[source]

Classmethod to read config file(s) as specified by path argument or DBCONF_PATH using ConfigParser