diff options
| -rw-r--r-- | main.py | 15 | ||||
| -rw-r--r-- | src/parser_factory.py | 13 |
2 files changed, 21 insertions, 7 deletions
| @@ -44,21 +44,30 @@ def clean_meta(filename:str): | |||
| 44 | if not __check_file(filename, os.R_OK|os.W_OK): | 44 | if not __check_file(filename, os.R_OK|os.W_OK): |
| 45 | return | 45 | return |
| 46 | 46 | ||
| 47 | p, mtype = parser_factory.get_parser(f) | 47 | p, mtype = parser_factory.get_parser(filename) |
| 48 | if p is None: | 48 | if p is None: |
| 49 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) | 49 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) |
| 50 | return | 50 | return |
| 51 | p.remove_all() | 51 | p.remove_all() |
| 52 | 52 | ||
| 53 | def main(): | 53 | def main(): |
| 54 | args = create_arg_parser().parse_args() | 54 | arg_parser = create_arg_parser() |
| 55 | args = arg_parser.parse_args() | ||
| 55 | 56 | ||
| 56 | if args.show: | 57 | if args.show: |
| 57 | for f in args.files: | 58 | for f in args.files: |
| 58 | show_meta(f) | 59 | show_meta(f) |
| 59 | else: | 60 | elif args.list: |
| 61 | print('[+] Supported formats:') | ||
| 62 | for parser in parser_factory._get_parsers(): | ||
| 63 | for mtype in parser.mimetypes: | ||
| 64 | extensions = ', '.join(mimetypes.guess_all_extensions(mtype)) | ||
| 65 | print(' - %s (%s)' % (mtype, extensions)) | ||
| 66 | elif args.files: | ||
| 60 | for f in args.files: | 67 | for f in args.files: |
| 61 | clean_meta(f) | 68 | clean_meta(f) |
| 69 | else: | ||
| 70 | arg_parser.print_help() | ||
| 62 | 71 | ||
| 63 | 72 | ||
| 64 | if __name__ == '__main__': | 73 | if __name__ == '__main__': |
diff --git a/src/parser_factory.py b/src/parser_factory.py index 770f17c..7ce4246 100644 --- a/src/parser_factory.py +++ b/src/parser_factory.py | |||
| @@ -16,12 +16,17 @@ for module_loader, name, ispkg in pkgutil.walk_packages('.src'): | |||
| 16 | continue | 16 | continue |
| 17 | importlib.import_module(name) | 17 | importlib.import_module(name) |
| 18 | 18 | ||
| 19 | def _get_parsers() -> list: | ||
| 20 | """ Get all our parsers!""" | ||
| 21 | def __get_parsers(cls): | ||
| 22 | return cls.__subclasses__() + \ | ||
| 23 | [g for s in cls.__subclasses__() for g in __get_parsers(s)] | ||
| 24 | return __get_parsers(abstract.AbstractParser) | ||
| 25 | |||
| 19 | def get_parser(filename: str) -> (T, str): | 26 | def get_parser(filename: str) -> (T, str): |
| 20 | mtype, _ = mimetypes.guess_type(filename) | 27 | mtype, _ = mimetypes.guess_type(filename) |
| 21 | def get_subclasses(cls): | 28 | |
| 22 | return cls.__subclasses__() + \ | 29 | for c in _get_parsers(): |
| 23 | [g for s in cls.__subclasses__() for g in get_subclasses(s)] | ||
| 24 | for c in get_subclasses(abstract.AbstractParser): | ||
| 25 | if mtype in c.mimetypes: | 30 | if mtype in c.mimetypes: |
| 26 | return c(filename), mtype | 31 | return c(filename), mtype |
| 27 | return None, mtype | 32 | return None, mtype |
