Add FileIter to iwla, allowing to specify multiple files to analyse

This commit is contained in:
Gregory Soutade 2015-05-14 09:54:25 +02:00
parent 62be78845a
commit 86fc5f2189

38
iwla.py
View File

@ -720,6 +720,38 @@ class IWLA(object):
else: else:
self.logger.info('==> Analyse not started : nothing new') 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__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Intelligent Web Log Analyzer') parser = argparse.ArgumentParser(description='Intelligent Web Log Analyzer')
@ -775,8 +807,4 @@ if __name__ == '__main__':
iwla.start(sys.stdin) iwla.start(sys.stdin)
else: else:
filename = args.file or conf.analyzed_filename filename = args.file or conf.analyzed_filename
if not os.path.exists(filename): iwla.start(FileIter(filename))
print 'No such file \'%s\'' % (filename)
sys.exit(-1)
with open(filename) as f:
iwla.start(f)