diff options
| author | jvoisin | 2018-04-02 17:36:26 +0200 |
|---|---|---|
| committer | jvoisin | 2018-04-02 17:36:26 +0200 |
| commit | 6868f20065ef2e46f295c34f27c0736d54283535 (patch) | |
| tree | a76ef4cf903d763fe4435e282c402f2ebd0d0a82 | |
| parent | 6c29e0eae256ddb3d32fd78e9949d0aa81be033e (diff) | |
`parser_factory` now returns the mtype too
| -rw-r--r-- | main.py | 7 | ||||
| -rw-r--r-- | src/office.py | 4 | ||||
| -rw-r--r-- | src/parser_factory.py | 4 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 2 |
4 files changed, 8 insertions, 9 deletions
| @@ -20,9 +20,8 @@ def create_arg_parser(): | |||
| 20 | return parser | 20 | return parser |
| 21 | 21 | ||
| 22 | def show_meta(filename:str): | 22 | def show_meta(filename:str): |
| 23 | p = parser_factory.get_parser(filename) | 23 | p, mtype = parser_factory.get_parser(filename) |
| 24 | if p is None: | 24 | if p is None: |
| 25 | mtype, _ = mimetypes.guess_type(filename) | ||
| 26 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) | 25 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) |
| 27 | return | 26 | return |
| 28 | for k,v in p.get_meta().items(): | 27 | for k,v in p.get_meta().items(): |
| @@ -38,9 +37,9 @@ def main(): | |||
| 38 | return 0 | 37 | return 0 |
| 39 | 38 | ||
| 40 | for f in args.files: | 39 | for f in args.files: |
| 41 | p = parser_factory.get_parser(f) | 40 | p, mtype = parser_factory.get_parser(f) |
| 42 | if p is None: | 41 | if p is None: |
| 43 | print("[-] %s's format (%s) is not supported" % (f, "meh")) | 42 | print("[-] %s's format (%s) is not supported" % (f, mtype)) |
| 44 | continue | 43 | continue |
| 45 | p.remove_all() | 44 | p.remove_all() |
| 46 | 45 | ||
diff --git a/src/office.py b/src/office.py index 0a34185..8d478c1 100644 --- a/src/office.py +++ b/src/office.py | |||
| @@ -38,9 +38,9 @@ class ArchiveBasedAbstractParser(abstract.AbstractParser): | |||
| 38 | 38 | ||
| 39 | def _clean_internal_file(self, item:zipfile.ZipInfo, temp_folder:str, zin:zipfile.ZipFile, zout:zipfile.ZipFile): | 39 | def _clean_internal_file(self, item:zipfile.ZipInfo, temp_folder:str, zin:zipfile.ZipFile, zout:zipfile.ZipFile): |
| 40 | zin.extract(member=item, path=temp_folder) | 40 | zin.extract(member=item, path=temp_folder) |
| 41 | tmp_parser = parser_factory.get_parser(os.path.join(temp_folder, item.filename)) | 41 | tmp_parser, mtype = parser_factory.get_parser(os.path.join(temp_folder, item.filename)) |
| 42 | if tmp_parser is None: | 42 | if tmp_parser is None: |
| 43 | print("%s isn't supported" % item.filename) | 43 | print("%s's format (%s) isn't supported" % (item.filename, mtype)) |
| 44 | return | 44 | return |
| 45 | tmp_parser.remove_all() | 45 | tmp_parser.remove_all() |
| 46 | zinfo = zipfile.ZipInfo(item.filename) | 46 | zinfo = zipfile.ZipInfo(item.filename) |
diff --git a/src/parser_factory.py b/src/parser_factory.py index 176ff2b..226234b 100644 --- a/src/parser_factory.py +++ b/src/parser_factory.py | |||
| @@ -15,6 +15,6 @@ def get_parser(filename: str): | |||
| 15 | mtype, _ = mimetypes.guess_type(filename) | 15 | mtype, _ = mimetypes.guess_type(filename) |
| 16 | for c in abstract.AbstractParser.__subclasses__(): | 16 | for c in abstract.AbstractParser.__subclasses__(): |
| 17 | if mtype in c.mimetypes: | 17 | if mtype in c.mimetypes: |
| 18 | return c(filename) | 18 | return c(filename), mtype |
| 19 | print('factory: %s is not supported' % mtype) | 19 | print('factory: %s is not supported' % mtype) |
| 20 | return None | 20 | return None, mtype |
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 8aa91f0..b52e8ce 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -72,7 +72,7 @@ class TestDeepCleaning(unittest.TestCase): | |||
| 72 | for subdir, dirs, files in os.walk(tempdir): | 72 | for subdir, dirs, files in os.walk(tempdir): |
| 73 | for f in files: | 73 | for f in files: |
| 74 | complete_path = os.path.join(subdir, f) | 74 | complete_path = os.path.join(subdir, f) |
| 75 | inside_p = parser_factory.get_parser(complete_path) | 75 | inside_p, _ = parser_factory.get_parser(complete_path) |
| 76 | if inside_p is None: | 76 | if inside_p is None: |
| 77 | continue | 77 | continue |
| 78 | print('[+] %s is clean inside %s' %(complete_path, p.filename)) | 78 | print('[+] %s is clean inside %s' %(complete_path, p.filename)) |
