From bee5c022b1d72eab945f6da738fd2d2d67d9adbe Mon Sep 17 00:00:00 2001
From: Mageti <etienne@mageti.fr>
Date: Thu, 27 Mar 2014 14:40:11 +0100
Subject: [PATCH 1/3] 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

From 7d5b72cde1fd3f501199108581a0e80f1e952a1f Mon Sep 17 00:00:00 2001
From: Mageti <etienne@mageti.fr>
Date: Thu, 27 Mar 2014 15:07:01 +0100
Subject: [PATCH 2/3] sqlalchemy->sqlite

Correct a bad copy-paste
change sqlalchemy keyword to sqlite
---
 bottle_sqlite.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/bottle_sqlite.py b/bottle_sqlite.py
index 6f6ee93..943888e 100755
--- a/bottle_sqlite.py
+++ b/bottle_sqlite.py
@@ -78,9 +78,9 @@ class SQLitePlugin(object):
     
         # Override global configuration with route-specific values.
         if "sqlite" in config: # support for configuration before `ConfigDict` namespaces
-            g = lambda key, default: config.get('sqlalchemy', {}).get(key, default)
+            g = lambda key, default: config.get('sqlite', {}).get(key, default)
         else:
-            g = lambda key, default: config.get('sqlalchemy.' + key, default)
+            g = lambda key, default: config.get('sqlite.' + key, default)
     
         dbfile = g('dbfile', self.dbfile)
         autocommit = g('autocommit', self.autocommit)

From d666670c9482f5c35ed857a47658de3983eb6f4c Mon Sep 17 00:00:00 2001
From: Mageti <etienne@mageti.fr>
Date: Thu, 27 Mar 2014 15:10:24 +0100
Subject: [PATCH 3/3] correct keyword in args

correct bad copy-paste to not change this plugin behavior
---
 bottle_sqlite.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bottle_sqlite.py b/bottle_sqlite.py
index 943888e..d37ae28 100755
--- a/bottle_sqlite.py
+++ b/bottle_sqlite.py
@@ -90,7 +90,7 @@ class SQLitePlugin(object):
         # Test if the original callback accepts a 'db' keyword.
         # Ignore it if it does not need a database handle.
         argspec = inspect.getargspec(_callback)
-        if not ((keyword and argspec.keywords) or keyword in argspec.args):
+        if keyword not in argspec.args:
             return callback
         
         def wrapper(*args, **kwargs):