Guest User

Untitled

a guest
Oct 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import zipfile
  2.  
  3.  
  4. class ZipWalker:
  5.     """
  6.        Provide an interator that walks the internal syslog data in all the
  7.        zip archives identified at the specified path.
  8.    """
  9.  
  10.     def __init__(self, path):
  11.         self._path = path
  12.         self._archives = self._populate()
  13.  
  14.     def __iter__(self):
  15.         return self.nextline()
  16.  
  17.     def nextline(self):
  18.         for archive in self._archives.keys():
  19.             archive = zipfile.ZipFile(self._path + archive)
  20.             for member in archive.namelist():
  21.                 member = archive.open(member)
  22.                 for line in member.readlines():
  23.                     yield line
  24.  
  25.     def archives(self):
  26.         return self._archives.keys()
  27.  
  28.     def files(self):
  29.         files = []
  30.         for archive in self._archives.keys():
  31.             for file in self._archives[archive]:
  32.                 files.append(file)
  33.         return files
  34.  
  35.     def _populate(self):
  36.         archives = {}
  37.         for file in os.listdir(self._path):
  38.             if zipfile.is_zipfile(self._path + file):
  39.                 archives[file] = []
  40.                 for logfile in zipfile.ZipFile(self._path + file).namelist():
  41.                     archives[file].append(logfile)
  42.         return archives
  43.  
  44. if __name__ == '__main__':
  45.  
  46.     logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
  47.     path = "/home/oxseyn/Documents/hp_data/2011.07.03-2011.07.11/archives/"
  48.     walker = ZipWalker(path)
  49.  
  50.     logging.debug('starting run')
  51.     x = 0
  52.     for line in walker:
  53.         #    if x % 25000 == 0:
  54.         #   print x
  55.         x = x + 1
  56.     print x
  57.     logging.debug('ended run')
Add Comment
Please, Sign In to add comment