def getdbconnection(configsection, as_dict=False, defaultargs={}):
""" Helper function that gets a database connection object from a config dictionary """
dbengine = configsection['dbengine'] if 'dbengine' in configsection else 'MySQLDB'
if dbengine == 'MySQLDB':
dbargs = defaultargs.copy()
# Convert the external configuration names to python PEP-249 config names
translate = {"host": "host",
"defaultsfile": "read_default_file",
"user": "user",
"pass": "passwd",
"port": "port"}
for confval, myval in translate.iteritems():
if confval in configsection:
dbargs[myval] = configsection[confval]
if as_dict:
dbargs['cursorclass'] = MySQLdb.cursors.DictCursor
return MySQLdb.connect(**dbargs)
else:
raise Exception("Unsupported database engine %s" % (dbengine))
评论列表
文章目录