Updates for PEP8.

This commit is contained in:
Scott Wallace 2013-07-13 08:21:09 +01:00
parent d58959b834
commit 8a878cea62

View file

@ -1,4 +1,5 @@
#!/usr/bin/python #!/usr/bin/python
"""Program to fetch a blocklist via HTTP"""
import sys import sys
import logging import logging
@ -17,8 +18,8 @@ class BlockList:
try: try:
self.config = ConfigParser.ConfigParser() self.config = ConfigParser.ConfigParser()
self.config.readfp(open(config_file)) self.config.readfp(open(config_file))
except Exception, e: except (IOError, ConfigParser.MissingSectionHeaderError), error:
logging.error("Could not read configuration file %s: %s" % (config_file, e)) logging.error("Could not read configuration file %s: %s", config_file, error)
raise raise
def read(self, source): def read(self, source):
@ -32,15 +33,16 @@ class BlockList:
# Fetch the items from the input # Fetch the items from the input
(remote_ip, forwarded_ip, useragent, cookie) = line (remote_ip, forwarded_ip, useragent, cookie) = line
self.add(remote_ip, forwarded_ip, useragent, cookie) self.add(remote_ip, forwarded_ip, useragent, cookie)
except Exception, e: except csv.Error, error:
logging.error("There was an error retrieving the blocklist. %s" % e) logging.error("There was an error retrieving the blocklist. %s", error)
def add(self, remote_ip, forwarded_ip, useragent, cookie): def add(self, remote_ip, forwarded_ip, useragent, cookie):
"""Method to store the remote_ip, forwarded_ip, useragent and cookie to the in-memory dictionary."""
# Store the various items # Store the various items
if remote_ip not in self.data: if remote_ip not in self.data:
self.data[remote_ip] = { "remote_ip": remote_ip, "forwarded_ip": forwarded_ip, "useragent": useragent, "cookie": cookie } self.data[remote_ip] = { "remote_ip": remote_ip, "forwarded_ip": forwarded_ip, "useragent": useragent, "cookie": cookie }
else: else:
logging.debug("%s already exists in blacklist. Ignoring." % remote_ip) logging.debug("%s already exists in blacklist. Ignoring.", remote_ip)
def cache(self, source): def cache(self, source):
"""Attempt to read from the source URL and store results in a cache file, otherwise use the contents of the cache. If the cache isn't usable but the data is still available, return the transient data.""" """Attempt to read from the source URL and store results in a cache file, otherwise use the contents of the cache. If the cache isn't usable but the data is still available, return the transient data."""
@ -57,30 +59,30 @@ class BlockList:
if not os.path.exists(cache_dir): if not os.path.exists(cache_dir):
try: try:
os.makedirs(cache_dir) os.makedirs(cache_dir)
except: except OSError, error:
logging.warning("Could not create the caching directory. Will attempt to run without a cache.") logging.warning("Could not create the caching directory: %s Will attempt to run without a cache.", error)
# Attempt to fetch the data and store it in a cache file # Attempt to fetch the data and store it in a cache file
try: try:
list = urllib2.urlopen(source) input_list = urllib2.urlopen(source)
raw_data = list.read() raw_data = input_list.read()
cache_file = open(cache_path, "w+") cache_file = open(cache_path, "w+")
cache_file.write(raw_data) cache_file.write(raw_data)
# Rewind the file # Rewind the file
cache_file.seek(0) cache_file.seek(0)
except (urllib2.URLError, urllib2.HTTPError), e: except (urllib2.URLError, urllib2.HTTPError), error:
# Network error. Warn and use the cached content. # Network error. Warn and use the cached content.
logging.warning("Reverting to cache file. There was a problem contacting host %s: %s" % (hostname, e)) logging.warning("Reverting to cache file. There was a problem contacting host %s: %s", hostname, error)
try: try:
cache_file = open(cache_path, "r") cache_file = open(cache_path, "r")
except IOError, e: except IOError, error:
logging.error("No cache file was available for %s." % hostname) logging.error("No cache file was available for %s: %s", hostname, error)
raise raise
except Exception, e: except IOError, error:
# Cache error, but network succeeded. Use String IO to return the data. # Cache error, but network succeeded. Use String IO to return the data.
import StringIO import StringIO
logging.warning("Could not create cache file: %s. Returning transient data." % e) logging.warning("Could not create cache file: %s. Returning transient data.", error)
cache_file = StringIO.StringIO() cache_file = StringIO.StringIO()
cache_file.write(raw_data) cache_file.write(raw_data)
@ -92,11 +94,11 @@ class BlockList:
def dump(self): def dump(self):
"""Dump the local datastore out to stdout.""" """Dump the local datastore out to stdout."""
for list,val in self.data.iteritems(): for name, val in self.data.iteritems():
for address in val: for address in val:
print "%s: %s" % (list, address) print "%s: %s" % (name, address)
def export(self): def export(self):
"""Output a plaintext blocklist to stdout.""" """Output a plaintext blocklist to stdout."""
for key,item in self.data.iteritems(): for item in self.data.values():
print "%s,%s,%s,%s" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"]) print "%s,%s,%s,%s" % (item["remote_ip"], item["forwarded_ip"], item["useragent"], item["cookie"])