Add FileIter to iwla, allowing to specify multiple files to analyse
This commit is contained in:
parent
62be78845a
commit
86fc5f2189
38
iwla.py
38
iwla.py
|
@ -720,6 +720,38 @@ class IWLA(object):
|
|||
else:
|
||||
self.logger.info('==> Analyse not started : nothing new')
|
||||
|
||||
|
||||
class FileIter(object):
|
||||
def __init__(self, filenames):
|
||||
self.filenames = [f for f in filenames.split(',') if f]
|
||||
for f in self.filenames:
|
||||
if not os.path.exists(f):
|
||||
print 'No such file \'%s\'' % (f)
|
||||
sys.exit(-1)
|
||||
self.cur_file = None
|
||||
self._openNextFile()
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
return self.next()
|
||||
|
||||
def _openNextFile(self):
|
||||
if self.cur_file:
|
||||
self.cur_file.close()
|
||||
self.cur_file = None
|
||||
if not self.filenames:
|
||||
raise StopIteration()
|
||||
self.cur_file = open(self.filenames.pop(0))
|
||||
|
||||
def next(self):
|
||||
l = self.cur_file.readline()
|
||||
if not l:
|
||||
self._openNextFile()
|
||||
l = self.cur_file.readline()
|
||||
return l[:-1]
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Intelligent Web Log Analyzer')
|
||||
|
||||
|
@ -775,8 +807,4 @@ if __name__ == '__main__':
|
|||
iwla.start(sys.stdin)
|
||||
else:
|
||||
filename = args.file or conf.analyzed_filename
|
||||
if not os.path.exists(filename):
|
||||
print 'No such file \'%s\'' % (filename)
|
||||
sys.exit(-1)
|
||||
with open(filename) as f:
|
||||
iwla.start(f)
|
||||
iwla.start(FileIter(filename))
|
||||
|
|
Loading…
Reference in New Issue
Block a user