raisoblast / bottle-pgsql

PostgreSQL Plugin for Bottle.py
17 stars 7 forks source link

DeprecationWarning: Switch to Plugin API v2 #6

Open em- opened 8 years ago

em- commented 8 years ago

Similar to bottlepy/bottle-sqlite#10, bottle now complains when loading the plugin:

/usr/lib/python2.7/dist-packages/bottle.py:527: DeprecationWarning: Switch to Plugin API v2 and access the Route object directly.
  context = self if api > 1 else self._context

See bottlepy/bottle-redis#3 for the way bottle-redis fixed the issue.

sporian-smckown commented 1 year ago

This change:

diff --git 1/venv/lib/python3.8/site-packages/bottle_pgsql.py.orig 2/venv/lib/python3.8/site-packages/bottle_pgsql.py
index f65f808..b8d5219 100644
--- 1/venv/lib/python3.8/site-packages/bottle_pgsql.py.orig
+++ 2/venv/lib/python3.8/site-packages/bottle_pgsql.py
@@ -51,6 +51,7 @@ class PgSQLPlugin(object):
     '''

     name = 'pgsql'
+    api = 2

     def __init__(self, dsn=None, autocommit=True, dictrows=True, keyword='db'):
         self.dsn = dsn
@@ -68,9 +69,11 @@ class PgSQLPlugin(object):
             if other.keyword == self.keyword:
                 raise PluginError("Found another pgsql plugin with conflicting settings (non-unique keyword).")

-    def apply(self, callback, context):
+    def apply(self, callback, route):
         # Override global configuration with route-specific values.
-        conf = context['config'].get('pgsql') or {}
+        config = route.config
+        _callback = route.callback
+        conf = config.get('pgsql') or {}
         dsn = conf.get('dsn', self.dsn)
         autocommit = conf.get('autocommit', self.autocommit)
         dictrows = conf.get('dictrows', self.dictrows)
@@ -78,7 +81,7 @@ class PgSQLPlugin(object):

         # 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]
+        args = inspect.signature(_callback).parameters
         if keyword not in args:
             return callback

Solves both of these warnings:

venv/lib/python3.8/site-packages/bottle.py:539: DeprecationWarning: Switch to Plugin API v2 and access the Route object directly.
  context = self if api > 1 else self._context
venv/lib/python3.8/site-packages/bottle_pgsql.py:81: DeprecationWarning: inspect.getargspec() is deprecated since Python 3.0, use inspect.signature() or inspect.getfullargspec()
  args = inspect.getargspec(context['callback'])[0]

Tested on Python 3.8.10 using bottle 0.12.25.