Public Member Functions | |
def | Export |
def | __init__ |
def | mysqldb_parameters |
def | configure_cascade |
def | dump_env |
def | from_env |
def | export_ |
def | read_cfg |
def | has_config |
def | prime_parser |
def | export_to_env |
Static Public Attributes | |
dictionary | defaults |
tuple | Export = classmethod( Export ) |
tuple | urls = property( lambda self:(self.url % self).split(";") ) |
tuple | users = property( lambda self:(self.user % self).split(";") ) |
tuple | pswds = property( lambda self:(self.pswd % self).split(";") ) |
tuple | fixs = property( lambda self:(self.fix % self).split(";") ) |
tuple | from_env = classmethod(from_env) |
tuple | read_cfg = classmethod( read_cfg ) |
tuple | has_config = classmethod( has_config ) |
tuple | prime_parser = classmethod( prime_parser ) |
Private Member Functions | |
def | _check_path |
Static Private Attributes | |
string | __str__ = "\n" |
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:: [testdb] host = dybdb1.ihep.ac.cn database = testdb user = dayabay password = youknowoit The standard python :py:mod:`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" .. warning:: As passwords are contained **DO NOT COMMIT** into any repository, and protect the file.
Definition at line 13 of file dbconf.py.
def DybPython::dbconf::DBConf::Export | ( | cls, | ||
sect = None , |
||||
extras | ||||
) |
def DybPython::dbconf::DBConf::__init__ | ( | self, | ||
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 | ||||
) |
Definition at line 45 of file dbconf.py.
00045 :"%(user)s", 00046 'db':"%(database)s", 00047 'pswd':"%(password)s", 00048 'url':"mysql://%(host)s/%(database)s", 00049 'fix':None, 00050 'fixpass':None, 00051 'restrict':None, 00052 } 00053 00054 def Export(cls, sect=None , **extras ): 00055 """ 00056 Exports the environment settings into environment of python process 00057 this is invoked by the C++ *DbiCascader* ctor 00058 """ 00059 cnf = DBConf( sect=sect ) 00060 if cnf.fix == None: 00061 cnf.export_to_env(**extras) 00062 else: 00063 from dbcas import DBCas 00064 cas = DBCas(cnf) 00065 tas = cas.spawn() 00066 cnf.export_to_env( supplier=tas ) 00067 return cnf 00068 #print dbc.dump_env() 00069 Export = classmethod( Export ) 00070 00071 def __init__(self, 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 ): 00072 """ 00073 00074 See also :ref:`dbi:running` section of the Offline User Manual 00075 00076 Interpolates the DB connection parameter patterns gleaned 00077 from arguments, envvars or defaults (in that precedence order) 00078 into usable values using the context supplied by the 00079 *sect* section of the ini format config file at *path* 00080 00081 Optional keyword arguments: 00082 00083 ================ ======================================================= 00084 Keyword Description 00085 ================ ======================================================= 00086 *sect* section in config file 00087 *path* colon delimited list of paths to config file 00088 00089 *user* username 00090 *pswd* password 00091 *url* connection url 00092 *host* db host 00093 *db* db name 00094 00095 *fix* triggers fixture loading into temporary 00096 spawned cascade and specifies paths to fixture files 00097 for each member of the cascade (semi-colon delimited) 00098 *fixpass* skip the DB cascade dropping/creation that is 00099 normally done 00100 as part of cascade spawning (used in DBWriter/tests) 00101 *restrict* constrain the names of DB that can connect to 00102 starting with a string, eg ``tmp_`` as a safeguard 00103 *nodb* used to connect without specifying the database 00104 this requires greater access privileges and is used 00105 to perform database dropping/creation 00106 ================ ======================================================= 00107 00108 00109 Correspondingly named envvars can also be used: 00110 00111 .. envvar:: DBCONF 00112 .. envvar:: DBCONF_PATH 00113 00114 .. envvar:: DBCONF_USER 00115 .. envvar:: DBCONF_PWSD 00116 .. envvar:: DBCONF_URL 00117 .. envvar:: DBCONF_HOST 00118 .. envvar:: DBCONF_DB 00119 00120 .. envvar:: DBCONF_FIX 00121 .. envvar:: DBCONF_FIXPASS 00122 .. envvar:: DBCONF_RESTRICT 00123 00124 The :envvar:`DBCONF` existance also triggers the 00125 :meth:`DybPython.dbconf.DBConf.Export` in :dybgaudi:`Database/DatabaseInterface/src/DbiCascader.cxx` 00126 00127 The :envvar:`DBCONF_PATH` is a colon delimited list of paths that are 00128 user (~) and $envvar OR ${envvar} expanded, some of the paths 00129 may not exist. When there are repeated settings in more than one 00130 file the last one wins. 00131 00132 In secure mode a single protected config file is required, the security 00133 comes with a high price in convenience 00134 00135 """ 00136 00137 self.secure = secure 00138 self.verbose = verbose 00139 00140 esect = os.environ.get('DBCONF', None ) 00141 if esect == "" or esect == None: 00142 esect = DBConf.defaults['sect'] 00143 00144 sect = sect or esect 00145 path = path or os.environ.get('DBCONF_PATH', DBConf.defaults['path'] ) 00146 00147 user = user or os.environ.get('DBCONF_USER', DBConf.defaults['user'] ) 00148 pswd = pswd or os.environ.get('DBCONF_PSWD', DBConf.defaults['pswd'] ) 00149 url = url or os.environ.get('DBCONF_URL', DBConf.defaults['url'] ) 00150 host = host or os.environ.get('DBCONF_HOST', DBConf.defaults['host'] ) 00151 db = db or os.environ.get('DBCONF_DB' , DBConf.defaults['db'] ) 00152 00153 fix = fix or os.environ.get('DBCONF_FIX' , DBConf.defaults['fix'] ) 00154 fixpass = fixpass or os.environ.get('DBCONF_FIXPASS' , DBConf.defaults['fixpass'] )
def DybPython::dbconf::DBConf::mysqldb_parameters | ( | self, | ||
nodb = False | ||||
) |
Using the `nodb=True` option skips database name parameter, this is useful when creating or dropping a database
Definition at line 155 of file dbconf.py.
00157 : 00158 self._check_path( path ) 00159 if not from_env: 00160 user,pswd,host,db,url = self.configure_cascade( sect, path ) 00161 00162 if restrict: 00163 dbns = db.split(";") 00164 dbok = filter( lambda _:_.startswith(restrict) , dbns ) 00165 assert len(dbns) == len(dbok), "DBCONF_RESTRICTion on DB names violated : all DB names must be prefixed with \"%s\" DB names :\"%s\" DB ok names: \"%s\" " % ( restrict , dbns , dbok ) 00166 00167 00168 fsect = sect.split(":")[0] ## first or only
def DybPython::dbconf::DBConf::_check_path | ( | self, | ||
path | ||||
) | [private] |
def DybPython::dbconf::DBConf::configure_cascade | ( | self, | ||
sect, | ||||
path | ||||
) |
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.
Definition at line 179 of file dbconf.py.
00181 : 00182 """ 00183 Using the `nodb=True` option skips database name parameter, this is useful 00184 when creating or dropping a database 00185 """ 00186 #return dict(read_default_file=self.path, read_default_group=self.sect) 00187 d = dict(host=self.host % self, user=self.user % self, passwd=self.pswd % self ) 00188 if not nodb: 00189 d.update( db=self.db % self ) 00190 if self.verbose: 00191 print "dbconf : connecting to %s " % dict(d, passwd="***" ) 00192 return d 00193 00194 00195 def _check_path(self, path ): 00196 """ 00197 Check existance and permissions of path 00198 """ 00199 assert os.path.exists( path ), "config path %s does not exist " % path 00200 from stat import S_IMODE, S_IRUSR, S_IWUSR 00201 s = os.stat(path) 00202 assert S_IMODE( s.st_mode ) == S_IRUSR | S_IWUSR , "incorrect permissions, config file must be protected with : chmod go-rw \"%s\" " % path 00203 00204 00205 def configure_cascade(self, sect , path): 00206 """ 00207 Interpret the `sect` argument comprised of a either a single section name eg `offline_db` 00208 or a colon delimited list of section names eg `tmp_offline_db:offline_db` 00209 to provide easy cascade configuration. A single section is of course a special case of a 00210 cascade. The first(or only) section in zeroth slot is treated specially with its config 00211 parameters being propagated into `self`. 00212 Caution any settings of `url`, `user`, `pswd`, `host`, `db` are overridden when
def DybPython::dbconf::DBConf::dump_env | ( | self, | ||
epfx = 'env_' | ||||
) |
def DybPython::dbconf::DBConf::from_env | ( | cls | ) |
Construct :class:`DBConf` objects from environment : :envvar:`ENV_TSQL_URL` :envvar:`ENV_TSQL_USER` :envvar:`ENV_TSQL_PSWD`
Definition at line 225 of file dbconf.py.
00225 : %s configured in %s " % ( sect, secs, paths ) 00226 cascade[sect] = {} 00227 cascade[sect].update( cfp.items(sect) ) 00228 00229 user = ";".join([ cascade[sect]['user'] for sect in csect ]) 00230 pswd = ";".join([ cascade[sect]['password'] for sect in csect ]) 00231 host = ";".join([ cascade[sect]['host'] for sect in csect ]) 00232 db = ";".join([ cascade[sect]['database'] for sect in csect ]) 00233 url = ";".join([ DBConf.defaults['url'] % cascade[sect] for sect in csect ]) 00234 00235 self.update( cascade[zsect] ) ## zeroth slot into self 00236 self.cascade = cascade ## for debugging only 00237 return user, pswd, host, db, url 00238 00239 def dump_env(self, epfx='env_'):
def DybPython::dbconf::DBConf::export_ | ( | self, | ||
extras | ||||
) |
Exports the interpolated configuration into corresponding *DBI* envvars : :envvar:`ENV_TSQL_USER` :envvar:`ENV_TSQL_PSWD` :envvar:`ENV_TSQL_URL` And *DatabaseSvc* envvars for access to non-DBI tables via DatabaseSvc : :envvar:`DYB_DB_USER` :envvar:`DYB_DB_PWSD` :envvar:`DYB_DB_URL`
Definition at line 243 of file dbconf.py.
00246 :(self.url % self).split(";") ) 00247 users = property( lambda self:(self.user % self).split(";") ) 00248 pswds = property( lambda self:(self.pswd % self).split(";") ) 00249 fixs = property( lambda self:(self.fix % self).split(";") ) 00250 00251 def from_env(cls): 00252 """ 00253 Construct :class:`DBConf` objects from environment : 00254 00255 :envvar:`ENV_TSQL_URL` 00256 :envvar:`ENV_TSQL_USER` 00257 :envvar:`ENV_TSQL_PSWD` 00258 00259 """ 00260 url = os.environ.get( 'ENV_TSQL_URL', None ) 00261 user = os.environ.get( 'ENV_TSQL_USER', None ) 00262 pswd = os.environ.get( 'ENV_TSQL_PSWD', None ) 00263 assert url and user and pswd , "DBConf.from_env reconstruction requites the ENV_TSQL_* " 00264 cnf = DBConf(url=url, user=user, pswd=pswd,from_env=True) 00265 return cnf 00266 from_env = classmethod(from_env) 00267 00268 00269 def export_(self, **extras): 00270 """ 00271 Exports the interpolated configuration into corresponding *DBI* envvars : 00272 00273 :envvar:`ENV_TSQL_USER` 00274 :envvar:`ENV_TSQL_PSWD` 00275 :envvar:`ENV_TSQL_URL` 00276 00277 And *DatabaseSvc* envvars for access to non-DBI tables via DatabaseSvc :
def DybPython::dbconf::DBConf::read_cfg | ( | cls, | ||
path = None | ||||
) |
Classmethod to read config file(s) as specified by `path` argument or :envvar:`DBCONF_PATH` using :py:mod:`ConfigParser`
Definition at line 278 of file dbconf.py.
00279 :envvar:`DYB_DB_USER` 00280 :envvar:`DYB_DB_PWSD` 00281 :envvar:`DYB_DB_URL` 00282 00283 """ 00284 supplier = extras.pop('supplier', None ) 00285 if supplier: 00286 print "export_ supplier is %s " % supplier 00287 else: 00288 supplier = self
def DybPython::dbconf::DBConf::has_config | ( | cls, | ||
name_ = None | ||||
) |
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
Definition at line 292 of file dbconf.py.
00300 : 00301 self.export[k] = v % self 00302 00303 00304 def read_cfg( cls , path=None ): 00305 """ 00306 Classmethod to read config file(s) as specified by `path` argument or :envvar:`DBCONF_PATH` using 00307 :py:mod:`ConfigParser` 00308 """ 00309 path = path or os.environ.get('DBCONF_PATH', DBConf.defaults['path'] ) 00310 from ConfigParser import ConfigParser 00311 cfp = ConfigParser(DBConf.prime_parser()) 00312 cfp.optionxform = str ## avoid lowercasing keys, making the keys case sensitive 00313 paths = cfp.read( [os.path.expandvars(os.path.expanduser(p)) for p in path.split(":")] ) 00314 return cfp, paths 00315 read_cfg = classmethod( read_cfg ) 00316 00317 def has_config( cls , name_=None ):
def DybPython::dbconf::DBConf::prime_parser | ( | cls | ) |
def DybPython::dbconf::DBConf::export_to_env | ( | self, | ||
extras | ||||
) |
dictionary DybPython::dbconf::DBConf::defaults [static] |
tuple DybPython::dbconf::DBConf::Export = classmethod( Export ) [static] |
tuple DybPython::dbconf::DBConf::urls = property( lambda self:(self.url % self).split(";") ) [static] |
tuple DybPython::dbconf::DBConf::users = property( lambda self:(self.user % self).split(";") ) [static] |
tuple DybPython::dbconf::DBConf::pswds = property( lambda self:(self.pswd % self).split(";") ) [static] |
tuple DybPython::dbconf::DBConf::fixs = property( lambda self:(self.fix % self).split(";") ) [static] |
tuple DybPython::dbconf::DBConf::from_env = classmethod(from_env) [static] |
tuple DybPython::dbconf::DBConf::read_cfg = classmethod( read_cfg ) [static] |
tuple DybPython::dbconf::DBConf::has_config = classmethod( has_config ) [static] |
tuple DybPython::dbconf::DBConf::prime_parser = classmethod( prime_parser ) [static] |
string DybPython::dbconf::DBConf::__str__ = "\n" [static, private] |