Package wtf :: Package ext :: Package db :: Module _config
[hide private]
[frames] | no frames]

Source Code for Module wtf.ext.db._config

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2010-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 17  """ 
 18  ================== 
 19   DB access module 
 20  ================== 
 21   
 22  DB access module. 
 23  """ 
 24  __author__ = u"Andr\xe9 Malo" 
 25  __docformat__ = "restructuredtext en" 
 26   
 27  import ConfigParser as _config_parser 
 28  import os as _os 
 29   
 30  from wtf.ext.db._exceptions import DBConfigurationError 
 31  from wtf import util as _wtf_util 
 32   
 33   
 34  #: Default conf file location 
 35  #: 
 36  #: :Type: ``str`` 
 37  DEFAULT_CONF = '/etc/wtf/ext/db.conf' 
 38   
 39   
40 -def load_from_file(dbconf):
41 """ 42 Load config 43 44 :Parameters: 45 `dbconf` : ``str`` 46 DB config filename 47 48 :Return: Config dict 49 :Rtype: ``dict`` 50 """ 51 parser = _config_parser.RawConfigParser() 52 try: 53 fp = open(dbconf, 'rb') 54 try: 55 parser.readfp(fp) 56 finally: 57 fp.close() 58 except IOError, e: 59 raise DBConfigurationError(str(e)) 60 61 config = dict((section, dict((opt, parser.get(section, opt)) 62 for opt in parser.options(section) 63 )) for section in parser.sections()) 64 65 for _, opts in config.items(): 66 if 'alias' in opts: 67 alias = opts['alias'] 68 opts.clear() 69 opts.update(config[alias]) 70 71 return config
72 73
74 -def configure(dbconf=None, unpack_password=None):
75 """ 76 Configure the databases 77 78 This function is called automatically at import time. But configuration 79 errors are ignored this first time. 80 81 :Parameters: 82 `dbconf` : ``str`` 83 Config file name. If omitted or ``None``, the environment variable 84 ``WTF_EXT_DB_CONF`` is queried. If that's unset, too, it defaults to 85 `DEFAULT_CONF`. 86 87 `unpack_password` : callable 88 Password unpacker. If omitted or ``None``, no password unpacker is 89 applied. 90 91 :Exceptions: 92 - `DBConfigurationError` : Config error 93 """ 94 def resolve(arg, env, default): 95 """ Resolve path """ 96 if arg is not None: 97 return arg 98 return _os.path.expanduser(_os.environ.get(env, default))
99 100 dbconf = resolve(dbconf, 'WTF_EXT_DB_CONF', DEFAULT_CONF) 101 config = load_from_file(dbconf) 102 103 def driver(dbname): 104 """ 105 Determine driver module 106 107 :Parameters: 108 `dbname` : ``str`` 109 DB name (section token in db.conf) 110 111 :Return: Driver module 112 :Rtype: ``module`` 113 114 :Exceptions: 115 - `DBConfigurationError` : DB not configured 116 - `KeyError` : DB name not found 117 - `ImportError` : Driver not found 118 """ 119 return _wtf_util.load_dotted( 120 'wtf.ext.db._driver_%s' % config[dbname]['driver'] 121 ) 122 123 def connect(dbname, **kwargs): 124 """ 125 Connect to database 126 127 :Parameters: 128 `dbname` : ``str`` 129 DB name (section token in db.conf) 130 131 `kwargs` : ``dict`` 132 Additional parameters for adapter connect() call 133 134 :Return: new connection 135 :Rtype: connection object (DBAPI 2) 136 137 :Exceptions: 138 - `DBConfigurationError` : DB not configured 139 - `KeyError` : DB name not found 140 - `ImportError` : Driver not found 141 """ 142 conf = config[dbname].copy() 143 db_driver = driver(dbname) 144 conf.pop('driver', None) 145 if 'passwd' in conf and unpack_password is not None: 146 passwd = conf['passwd'] 147 conf['passwd'] = lambda: unpack_password(passwd) 148 return db_driver.connect(conf, kwargs) 149 connect.config = config 150 151 return driver, connect 152