[Modeling-cvs] ProjectModeling/Modeling CHANGES,1.100,1.101 ModelSet.py,1.5,1.6
Status: Abandoned
Brought to you by:
sbigaret
From: <sbi...@us...> - 2003-05-07 11:22:10
|
Update of /cvsroot/modeling/ProjectModeling/Modeling In directory sc8-pr-cvs1:/tmp/cvs-serv22018 Modified Files: CHANGES ModelSet.py Log Message: RFE #726839: Added environment variable MDL_DB_CONNECTIONS_CFG; it points to a single ini-file which centralizes the sensitive informations contained in each model's database connection dictionary (username, password). See tests/Postgresql.cfg for an example. Index: CHANGES =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/CHANGES,v retrieving revision 1.100 retrieving revision 1.101 diff -C2 -d -r1.100 -r1.101 *** CHANGES 6 May 2003 13:52:00 -0000 1.100 --- CHANGES 7 May 2003 11:22:06 -0000 1.101 *************** *** 8,11 **** --- 8,16 ---- -------------------------------------------------------- + * RFE #726839: Added environment variable MDL_DB_CONNECTIONS_CFG; it points + to a single ini-file which centralizes the sensitive informations + contained in each model's database connection dictionary (username, + password). See tests/Postgresql.cfg for an example. + 0.9-pre-7 (2003/05/06) --------- Index: ModelSet.py =================================================================== RCS file: /cvsroot/modeling/ProjectModeling/Modeling/ModelSet.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** ModelSet.py 28 Feb 2003 23:21:26 -0000 1.5 --- ModelSet.py 7 May 2003 11:22:07 -0000 1.6 *************** *** 56,60 **** import types ! from logging import error try: --- 56,60 ---- import types ! from logging import error, warn try: *************** *** 98,101 **** --- 98,161 ---- setDefaultModelGroup=setDefaultModelSet + def updateModelWithCFG(model, cfg_path): + """ + Updates the model's connection dictionary and adaptorName with the values + in file 'cfg_path'. + + A sample configuration file is like:: + + [DEFAULTS] + host: localhost + + [ModelName_1] + user: user_1 + password: pwd_1 + + [ModelName_2] + adaptor: MySQL + user: user_2 + password: pwd_2 + + The special field 'adaptor', if present, changes the adaptorName of the + model. + + Raises IOError if file 'cfg_path' cannot be found. + + See also: ModelSet.addModel() + + Parameters: + + model -- the model whose conn.dict. should be updated + + cfg_path -- the full path to the configuration file + + """ + from ConfigParser import ConfigParser + import os + defaults=model.connectionDictionary() + cp=ConfigParser() + try: + cp.readfp(open(cfg_path)) + except IOError: + import traceback, cStringIO, sys + exc_raised=sys.exc_info()[:2] + err_msg="Unable to open file '%s' pointed by env. variable MDL_DB_CONNECTIONS_CFG"%cfg_path + exc=cStringIO.StringIO() + traceback.print_exception(exc_raised[0], exc_raised[1], None, file=exc) + err_msg+="\nOriginal exception was: %s"%exc.getvalue() + raise IOError, err_msg + + try: options=cp.options(model.name()) + except: return + try: options.remove('adaptor') + except ValueError: pass + for key in options: ##### TBD ++ section [Default] + defaults[key]=cp.get(model.name(), key) + model.setConnectionDictionary(defaults) + try: + model.setAdaptorName(cp.get(model.name(), 'adaptor')) + except: + pass + class ModelSet(Persistent): """Holds a set of Modeling.Models that can co-exist at runtime *************** *** 114,118 **** def addModel(self, aModel): ! "..." #assert # Check model name --- 174,189 ---- def addModel(self, aModel): ! """ ! Add a model to the model set. In order to be successfully added the model ! should not have the same name than one of the models already added to the ! ModelSet, nor should it hold an entity whose name is already used within ! the ModelSet. ! ! Raises ModelError if the insertion of aModel fails. ! ! If the environment variable 'MDL_DB_CONNECTIONS_CFG' is set, the file ! it points to is used to update aModel's connection dictionary (and ! possibly its adaptorName as well). See updateModelWithCFG() for details. ! """ #assert # Check model name *************** *** 129,132 **** --- 200,209 ---- aModel._setModelSet(self) + # MDL_DB_CONNECTIONS_CFG + import os + cfg_path=os.environ.get('MDL_DB_CONNECTIONS_CFG') + if cfg_path: + updateModelWithCFG(aModel, cfg_path) + # XML Import/Export facilities def addModelFromXML(self, xmlSource): |