From 2244ac497d02b6a644ac0717d9d265187fd5c597 Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Sun, 7 Sep 2014 20:07:26 +0100 Subject: [PATCH] Code tidy to ensure it complies with the Python coding standard (10/10 from PyLint). --- sqlite.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/sqlite.py b/sqlite.py index 8e7095e..add3ef4 100755 --- a/sqlite.py +++ b/sqlite.py @@ -1,17 +1,18 @@ -#!/usr/bin/python +#!/usr/bin/python """Script to create a SQLite DB from the blocklist data.""" import blocklist import sys import logging -if sys.version_info >= (2, 5): +try: import sqlite3 -else: +except ImportError: from pysqlite2 import dbapi2 as sqlite3 class SQLiteBlockList(blocklist.BlockList): - """New class to extend the main BlockList class for implementation with Varnish.""" + """New class to extend the main BlockList class for + implementation with Varnish.""" def export(self): """Exports blocklist criteria to a SQLite file.""" @@ -19,18 +20,36 @@ class SQLiteBlockList(blocklist.BlockList): database = sqlite3.connect(self.config.get("sqlite", "database")) cur = database.cursor() - cur.execute("CREATE TABLE IF NOT EXISTS blocklist(remote_ip VARCHAR(15), forwarded_ip VARCHAR(15), useragent VARCHAR(256), cookie VARCHAR(1024), PRIMARY KEY(remote_ip))") + cur.execute( + "CREATE TABLE IF NOT EXISTS blocklist (" + + "remote_ip VARCHAR(15)," + + "forwarded_ip VARCHAR(15)," + + "useragent VARCHAR(256)," + + "cookie VARCHAR(1024)," + + "PRIMARY KEY(remote_ip))" + ) cur.execute("DELETE FROM blocklist") for item in self.data.values(): if item["useragent"] == 'NULL': item["useragent"] = '(null)' - cur.execute("INSERT INTO blocklist VALUES ('%s', '%s', '%s', '%s')" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"])) + + cur.execute( + "INSERT INTO blocklist VALUES ('%s', '%s', '%s', '%s')" % ( + item["remote_ip"], + item["forwarded_ip"], + item["useragent"], + item["cookie"] + ) + ) database.commit() cur.close() except sqlite3.Error, error: - logging.error("There was a problem exporting the data to SQLite. %s", error) + logging.error( + "There was a problem exporting the data to SQLite: %s", + error + ) def main(): """Main program loop."""