Code tidy to ensure it complies with the Python coding standard (10/10 from PyLint).

This commit is contained in:
Scott Wallace 2014-09-07 20:07:26 +01:00
parent 97cfd2ca5d
commit 2244ac497d

View file

@ -1,17 +1,18 @@
#!/usr/bin/python #!/usr/bin/python
"""Script to create a SQLite DB from the blocklist data.""" """Script to create a SQLite DB from the blocklist data."""
import blocklist import blocklist
import sys import sys
import logging import logging
if sys.version_info >= (2, 5): try:
import sqlite3 import sqlite3
else: except ImportError:
from pysqlite2 import dbapi2 as sqlite3 from pysqlite2 import dbapi2 as sqlite3
class SQLiteBlockList(blocklist.BlockList): 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): def export(self):
"""Exports blocklist criteria to a SQLite file.""" """Exports blocklist criteria to a SQLite file."""
@ -19,18 +20,36 @@ class SQLiteBlockList(blocklist.BlockList):
database = sqlite3.connect(self.config.get("sqlite", "database")) database = sqlite3.connect(self.config.get("sqlite", "database"))
cur = database.cursor() 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") cur.execute("DELETE FROM blocklist")
for item in self.data.values(): for item in self.data.values():
if item["useragent"] == 'NULL': if item["useragent"] == 'NULL':
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() database.commit()
cur.close() cur.close()
except sqlite3.Error, error: 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(): def main():
"""Main program loop.""" """Main program loop."""