def connect_db(*a, **k): if 'config' in k: if not os.path.exists(k['config']): raise Exception('Config file missing.') config = Config(k['config']) plist = [ ('engine','engine','engine'), ('host','host','dbhost'), ('name','db','dbname'), ('user','user','dbuser'), ('passwd','password','dbpass') ] db_params = {} for p,s,t in plist: v = k.get(p, config.get('database', t, db_defaults.get(p))) if v: if p == 'name': s = 'db' else: s = p db_params[s] = v try: db = DB(**db_params) except: logger.error('Error connecting to database with config %s' % repr((a,k))) raise logger.debug('Connected to db with config %s' % repr((a,k))) else: try: db = DB(*a, **k) except: logger.error('Error connecting to database with config %s' % repr((a,k))) raise logger.debug('Connected to db with %s' % repr((a,k))) return db
def __init__(self, name, path='.'): self.name = name self.config = Config(locate_config('dz.conf', path))