From a746772bf9f42a49000fbded45f29e3df9f5c560 Mon Sep 17 00:00:00 2001 From: Iuri de Silvio Date: Sat, 29 Mar 2014 11:04:08 -0300 Subject: [PATCH] Fix all PEP8 warnings, including a missing import (added the `test_raise_sqlite_integrity_error`). --- bottle_sqlite.py | 35 ++++++++++++++++++++--------------- test.py | 27 +++++++++++++++++++-------- 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/bottle_sqlite.py b/bottle_sqlite.py index f9de278..7f4c3ae 100755 --- a/bottle_sqlite.py +++ b/bottle_sqlite.py @@ -54,19 +54,20 @@ class SQLitePlugin(object): def __init__(self, dbfile=':memory:', autocommit=True, dictrows=True, keyword='db'): - self.dbfile = dbfile - self.autocommit = autocommit - self.dictrows = dictrows - self.keyword = keyword + self.dbfile = dbfile + self.autocommit = autocommit + self.dictrows = dictrows + self.keyword = keyword def setup(self, app): ''' Make sure that other installed plugins don't affect the same keyword argument.''' for other in app.plugins: - if not isinstance(other, SQLitePlugin): continue + if not isinstance(other, SQLitePlugin): + continue if other.keyword == self.keyword: - raise PluginError("Found another sqlite plugin with "\ - "conflicting settings (non-unique keyword).") + raise PluginError("Found another sqlite plugin with " + "conflicting settings (non-unique keyword).") elif other.name == self.name: self.name += '_%s' % self.keyword @@ -78,13 +79,14 @@ class SQLitePlugin(object): else: config = route.config _callback = route.callback - + # Override global configuration with route-specific values. - if "sqlite" in config: # support for configuration before `ConfigDict` namespaces + if "sqlite" in config: + # support for configuration before `ConfigDict` namespaces g = lambda key, default: config.get('sqlite', {}).get(key, default) else: g = lambda key, default: config.get('sqlite.' + key, default) - + dbfile = g('dbfile', self.dbfile) autocommit = g('autocommit', self.autocommit) dictrows = g('dictrows', self.dictrows) @@ -95,25 +97,28 @@ class SQLitePlugin(object): argspec = inspect.getargspec(_callback) if keyword not in argspec.args: return callback - + def wrapper(*args, **kwargs): # Connect to the database db = sqlite3.connect(dbfile) # This enables column access by name: row['column_name'] - if dictrows: db.row_factory = sqlite3.Row + if dictrows: + db.row_factory = sqlite3.Row # Add the connection handle as a keyword argument. kwargs[keyword] = db try: rv = callback(*args, **kwargs) - if autocommit: db.commit() + if autocommit: + db.commit() except sqlite3.IntegrityError as e: db.rollback() - raise HTTPError(500, "Database Error", e) + raise bottle.HTTPError(500, "Database Error", e) except bottle.HTTPError as e: raise except bottle.HTTPResponse as e: - if autocommit: db.commit() + if autocommit: + db.commit() raise finally: db.close() diff --git a/test.py b/test.py index fb50d9e..70d34d8 100644 --- a/test.py +++ b/test.py @@ -1,8 +1,8 @@ import unittest -import os +import sqlite3 import bottle from bottle.ext import sqlite -import sqlite3 + class SQLiteTest(unittest.TestCase): def setUp(self): @@ -14,20 +14,20 @@ class SQLiteTest(unittest.TestCase): @self.app.get('/') def test(db): self.assertEqual(type(db), type(sqlite3.connect(':memory:'))) - self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None) + self._request('/') def test_without_keyword(self): self.plugin = self.app.install(sqlite.Plugin()) @self.app.get('/') - def test(): + def test_1(): pass - self.app({'PATH_INFO':'/', 'REQUEST_METHOD':'GET'}, lambda x, y: None) + self._request('/') @self.app.get('/2') - def test(**kw): + def test_2(**kw): self.assertFalse('db' in kw) - self.app({'PATH_INFO':'/2', 'REQUEST_METHOD':'GET'}, lambda x, y: None) + self._request('/2') def test_install_conflicts(self): self.app.install(sqlite.Plugin()) @@ -38,8 +38,19 @@ class SQLiteTest(unittest.TestCase): pass # I have two plugins working with different names - self.app({'PATH_INFO': '/', 'REQUEST_METHOD': 'GET'}, lambda x, y: None) + self._request('/') + def test_raise_sqlite_integrity_error(self): + self.plugin = self.app.install(sqlite.Plugin()) + + @self.app.get('/') + def test(db): + raise sqlite3.IntegrityError() + self._request('/') + + def _request(self, path, method='GET'): + self.app({'PATH_INFO': path, 'REQUEST_METHOD': method}, + lambda x, y: None) if __name__ == '__main__': unittest.main()