Added is_valid method to readers.

detect_format now takes in optional compression type.
This commit is contained in:
John Garland 2009-09-12 14:25:38 +00:00
parent 5573d7ef7d
commit d6597bdc22
3 changed files with 17 additions and 2 deletions

View File

@ -344,7 +344,7 @@ class Core(CorePluginBase):
:raises UnknownFormatError: if the format cannot be detected
"""
self.config["list_compression"] = detect_compression(blocklist)
self.config["list_type"] = detect_format(blocklist)
self.config["list_type"] = detect_format(blocklist, self.config["list_compression"])
if not self.config["list_type"]:
self.config["list_compression"] = ""
raise UnknownFormatError

View File

@ -48,6 +48,6 @@ def detect_compression(filename):
f.close()
return COMPRESSION_TYPES.get(magic_number, "")
def detect_format(filename):
def detect_format(filename, compression=""):
# TODO: implement this function
return ""

View File

@ -60,6 +60,21 @@ class BaseReader(object):
"""Ignore commented lines and blank lines"""
return line.startswith('#') or not line.strip()
def is_valid(self, file):
"""Determines whether file is valid for this reader"""
blocklist = self.open()
valid = True
for line in blocklist:
if not self.is_ignored(line):
try:
(start, end) = self.parse(line)
except:
valid = False
finally:
break
blocklist.close()
return valid
def readranges(self):
"""Yields each ip range from the file"""
blocklist = self.open()