Code tidy to ensure it complies with the Python coding standard (10/10 from PyLint).
This commit is contained in:
parent
97cfd2ca5d
commit
2244ac497d
33
sqlite.py
33
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."""
|
||||
|
|
Loading…
Reference in a new issue