|
12 | 12 | __all__ = ['Unrar'] |
13 | 13 |
|
14 | 14 |
|
15 | | -def parse_hunk(hunk): |
16 | | - info = {} |
17 | | - for m in re.finditer( |
18 | | - r'^[ \t\f]*(.+?)[ \t\f]*:[ \t\f]*(.*?)[ \t\f]*$', |
19 | | - hunk, flags=re.M): |
20 | | - key = re.sub(r'\W', '_', m.group(1).lower()) |
21 | | - info[key] = m.group(2) |
22 | | - if info.get('service', '') == 'EOF': |
23 | | - raise StopIteration() |
24 | | - return info |
| 15 | +def iter_on_hunks(hunks): |
| 16 | + for hunk in hunks: |
| 17 | + info = {} |
| 18 | + for m in re.finditer( |
| 19 | + r'^[ \t\f]*(.+?)[ \t\f]*:[ \t\f]*(.*?)[ \t\f]*$', |
| 20 | + hunk, flags=re.M): |
| 21 | + key = re.sub(r'\W', '_', m.group(1).lower()) |
| 22 | + info[key] = m.group(2) |
| 23 | + if info.get('service', '') == 'EOF': |
| 24 | + break |
| 25 | + yield info |
25 | 26 |
|
26 | 27 |
|
27 | 28 | class Header(object): |
28 | | - def __init__(self, hunk): |
29 | | - self.__dict__.update(parse_hunk(hunk)) |
| 29 | + def __init__(self, info): |
| 30 | + self.__dict__.update(info) |
30 | 31 | assert 'RAR' in self.details, \ |
31 | 32 | "Maybe not a RAR file:%s\n" % self.details |
32 | 33 |
|
33 | 34 |
|
34 | 35 | class Member(object): |
35 | | - def __init__(self, hunk): |
36 | | - info = parse_hunk(hunk) |
| 36 | + def __init__(self, info): |
37 | 37 | info['filename'] = info.pop('name') |
38 | 38 | info['size'] = int(info.get('size', 0)) |
39 | 39 | info['packed_size'] = int(info.get('packed_size', 0)) |
@@ -76,8 +76,8 @@ def __init__(self, name, fileobj): |
76 | 76 | self.fileobj = ArchiveTemp(fileobj) |
77 | 77 | output = check_output(self._command + |
78 | 78 | ['vta', self.fileobj.name]).decode() |
79 | | - hunks = iter(output.split("\n\n")) |
80 | | - self.information = next(hunks).strip() |
| 79 | + hunks = iter_on_hunks(output.split("\n\n")) |
| 80 | + self.information = next(hunks) |
81 | 81 | self.header = Header(next(hunks)) |
82 | 82 | self._members = [m for m in (Member(h) for h in hunks)] |
83 | 83 | self._stream = (len(self._members) == 1) |
|
0 commit comments