From bee5c022b1d72eab945f6da738fd2d2d67d9adbe Mon Sep 17 00:00:00 2001 From: Mageti Date: Thu, 27 Mar 2014 14:40:11 +0100 Subject: [PATCH] Updating bottle_sqlite.py to new bottle Updating bottle_sqlite.py to new bottle versions inspired by bottle_sqlalchemy adapted to python3 --- bottle_sqlite.py | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/bottle_sqlite.py b/bottle_sqlite.py index 4dae746..6f6ee93 100755 --- a/bottle_sqlite.py +++ b/bottle_sqlite.py @@ -34,7 +34,13 @@ __license__ = 'MIT' import sqlite3 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): @@ -61,20 +67,32 @@ class SQLitePlugin(object): raise PluginError("Found another sqlite plugin with "\ "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. - conf = context['config'].get('sqlite') or {} - dbfile = conf.get('dbfile', self.dbfile) - autocommit = conf.get('autocommit', self.autocommit) - dictrows = conf.get('dictrows', self.dictrows) - keyword = conf.get('keyword', self.keyword) + if "sqlite" in config: # support for configuration before `ConfigDict` namespaces + g = lambda key, default: config.get('sqlalchemy', {}).get(key, default) + else: + g = lambda key, default: config.get('sqlalchemy.' + key, default) + + 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. # Ignore it if it does not need a database handle. - args = inspect.getargspec(context['callback'])[0] - if keyword not in args: + argspec = inspect.getargspec(_callback) + if not ((keyword and argspec.keywords) or keyword in argspec.args): return callback - + def wrapper(*args, **kwargs): # Connect to the database db = sqlite3.connect(dbfile) @@ -86,12 +104,12 @@ class SQLitePlugin(object): try: rv = callback(*args, **kwargs) if autocommit: db.commit() - except sqlite3.IntegrityError, e: + except sqlite3.IntegrityError as e: db.rollback() raise HTTPError(500, "Database Error", e) - except HTTPError, e: + except bottle.HTTPError as e: raise - except HTTPResponse, e: + except bottle.HTTPResponse as e: if autocommit: db.commit() raise finally: @@ -101,4 +119,4 @@ class SQLitePlugin(object): # Replace the route callback with the wrapped one. return wrapper -Plugin = SQLitePlugin \ No newline at end of file +Plugin = SQLitePlugin