Updating bottle_sqlite.py to new bottle

Updating bottle_sqlite.py to new bottle versions
inspired by bottle_sqlalchemy
adapted to python3
This commit is contained in:
Mageti 2014-03-27 14:40:11 +01:00
parent 87d94efcdc
commit bee5c022b1

View File

@ -34,7 +34,13 @@ __license__ = 'MIT'
import sqlite3 import sqlite3
import inspect import inspect
from bottle import HTTPResponse, HTTPError import bottle
# PluginError is defined to bottle >= 0.10
if not hasattr(bottle, 'PluginError'):
class PluginError(bottle.BottleException):
pass
bottle.PluginError = PluginError
class SQLitePlugin(object): class SQLitePlugin(object):
@ -61,20 +67,32 @@ class SQLitePlugin(object):
raise PluginError("Found another sqlite plugin with "\ raise PluginError("Found another sqlite plugin with "\
"conflicting settings (non-unique keyword).") "conflicting settings (non-unique keyword).")
def apply(self, callback, context): def apply(self, callback, route):
# hack to support bottle v0.9.x
if bottle.__version__.startswith('0.9'):
config = route['config']
_callback = route['callback']
else:
config = route.config
_callback = route.callback
# Override global configuration with route-specific values. # Override global configuration with route-specific values.
conf = context['config'].get('sqlite') or {} if "sqlite" in config: # support for configuration before `ConfigDict` namespaces
dbfile = conf.get('dbfile', self.dbfile) g = lambda key, default: config.get('sqlalchemy', {}).get(key, default)
autocommit = conf.get('autocommit', self.autocommit) else:
dictrows = conf.get('dictrows', self.dictrows) g = lambda key, default: config.get('sqlalchemy.' + key, default)
keyword = conf.get('keyword', self.keyword)
dbfile = g('dbfile', self.dbfile)
autocommit = g('autocommit', self.autocommit)
dictrows = g('dictrows', self.dictrows)
keyword = g('keyword', self.keyword)
# Test if the original callback accepts a 'db' keyword. # Test if the original callback accepts a 'db' keyword.
# Ignore it if it does not need a database handle. # Ignore it if it does not need a database handle.
args = inspect.getargspec(context['callback'])[0] argspec = inspect.getargspec(_callback)
if keyword not in args: if not ((keyword and argspec.keywords) or keyword in argspec.args):
return callback return callback
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
# Connect to the database # Connect to the database
db = sqlite3.connect(dbfile) db = sqlite3.connect(dbfile)
@ -86,12 +104,12 @@ class SQLitePlugin(object):
try: try:
rv = callback(*args, **kwargs) rv = callback(*args, **kwargs)
if autocommit: db.commit() if autocommit: db.commit()
except sqlite3.IntegrityError, e: except sqlite3.IntegrityError as e:
db.rollback() db.rollback()
raise HTTPError(500, "Database Error", e) raise HTTPError(500, "Database Error", e)
except HTTPError, e: except bottle.HTTPError as e:
raise raise
except HTTPResponse, e: except bottle.HTTPResponse as e:
if autocommit: db.commit() if autocommit: db.commit()
raise raise
finally: finally:
@ -101,4 +119,4 @@ class SQLitePlugin(object):
# Replace the route callback with the wrapped one. # Replace the route callback with the wrapped one.
return wrapper return wrapper
Plugin = SQLitePlugin Plugin = SQLitePlugin