Iterator for looping over a large dictionaries >>> from lib.core.option import paths >>> isinstance(next(Wordlist(paths.SMALL_DICT)), six.binary_type) True >>> isinstance(next(Wordlist(paths.WORDLIST)), six.binary_type) True
| 14 | from thirdparty import six |
| 15 | |
| 16 | class Wordlist(six.Iterator): |
| 17 | """ |
| 18 | Iterator for looping over a large dictionaries |
| 19 | |
| 20 | >>> from lib.core.option import paths |
| 21 | >>> isinstance(next(Wordlist(paths.SMALL_DICT)), six.binary_type) |
| 22 | True |
| 23 | >>> isinstance(next(Wordlist(paths.WORDLIST)), six.binary_type) |
| 24 | True |
| 25 | """ |
| 26 | |
| 27 | def __init__(self, filenames, proc_id=None, proc_count=None, custom=None): |
| 28 | self.filenames = [filenames] if isinstance(filenames, six.string_types) else filenames |
| 29 | self.fp = None |
| 30 | self.index = 0 |
| 31 | self.counter = -1 |
| 32 | self.current = None |
| 33 | self.iter = None |
| 34 | self.custom = custom or [] |
| 35 | self.proc_id = proc_id |
| 36 | self.proc_count = proc_count |
| 37 | self.adjust() |
| 38 | |
| 39 | def __iter__(self): |
| 40 | return self |
| 41 | |
| 42 | def adjust(self): |
| 43 | self.closeFP() |
| 44 | if self.index > len(self.filenames): |
| 45 | return # Note: https://stackoverflow.com/a/30217723 (PEP 479) |
| 46 | elif self.index == len(self.filenames): |
| 47 | self.iter = iter(self.custom) |
| 48 | else: |
| 49 | self.current = self.filenames[self.index] |
| 50 | if isZipFile(self.current): |
| 51 | try: |
| 52 | _ = zipfile.ZipFile(self.current, 'r') |
| 53 | except zipfile.error as ex: |
| 54 | errMsg = "something appears to be wrong with " |
| 55 | errMsg += "the file '%s' ('%s'). Please make " % (self.current, getSafeExString(ex)) |
| 56 | errMsg += "sure that you haven't made any changes to it" |
| 57 | raise SqlmapInstallationException(errMsg) |
| 58 | if len(_.namelist()) == 0: |
| 59 | errMsg = "no file(s) inside '%s'" % self.current |
| 60 | raise SqlmapDataException(errMsg) |
| 61 | self.fp = _.open(_.namelist()[0]) |
| 62 | else: |
| 63 | self.fp = open(self.current, "rb") |
| 64 | self.iter = iter(self.fp) |
| 65 | |
| 66 | self.index += 1 |
| 67 | |
| 68 | def closeFP(self): |
| 69 | if self.fp: |
| 70 | self.fp.close() |
| 71 | self.fp = None |
| 72 | |
| 73 | def __next__(self): |
no outgoing calls
no test coverage detected
searching dependent graphs…