diff options
| -rwxr-xr-x | mat2 | 46 |
1 files changed, 23 insertions, 23 deletions
| @@ -26,13 +26,19 @@ assert Union | |||
| 26 | 26 | ||
| 27 | logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING) | 27 | logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING) |
| 28 | 28 | ||
| 29 | def __print_without_chars(s: str): | ||
| 30 | """ Remove control characters | ||
| 31 | We might use 'Cc' instead of 'C', but better safe than sorry | ||
| 32 | https://www.unicode.org/reports/tr44/#GC_Values_Table | ||
| 33 | """ | ||
| 34 | print(''.join(ch for ch in s if not unicodedata.category(ch).startswith('C'))) | ||
| 29 | 35 | ||
| 30 | def __check_file(filename: str, mode: int = os.R_OK) -> bool: | 36 | def __check_file(filename: str, mode: int = os.R_OK) -> bool: |
| 31 | if not os.path.exists(filename): | 37 | if not os.path.exists(filename): |
| 32 | print("[-] %s doesn't exist." % filename) | 38 | __print_without_chars("[-] %s doesn't exist." % filename) |
| 33 | return False | 39 | return False |
| 34 | elif not os.path.isfile(filename): | 40 | elif not os.path.isfile(filename): |
| 35 | print("[-] %s is not a regular file." % filename) | 41 | __print_without_chars("[-] %s is not a regular file." % filename) |
| 36 | return False | 42 | return False |
| 37 | elif not os.access(filename, mode): | 43 | elif not os.access(filename, mode): |
| 38 | mode_str = [] # type: List[str] | 44 | mode_str = [] # type: List[str] |
| @@ -40,7 +46,7 @@ def __check_file(filename: str, mode: int = os.R_OK) -> bool: | |||
| 40 | mode_str += 'readable' | 46 | mode_str += 'readable' |
| 41 | if mode & os.W_OK: | 47 | if mode & os.W_OK: |
| 42 | mode_str += 'writeable' | 48 | mode_str += 'writeable' |
| 43 | print("[-] %s is not %s." % (filename, 'nor '.join(mode_str))) | 49 | __print_without_chars("[-] %s is not %s." % (filename, 'nor '.join(mode_str))) |
| 44 | return False | 50 | return False |
| 45 | return True | 51 | return True |
| 46 | 52 | ||
| @@ -88,10 +94,10 @@ def show_meta(filename: str, sandbox: bool): | |||
| 88 | try: | 94 | try: |
| 89 | p, mtype = parser_factory.get_parser(filename) # type: ignore | 95 | p, mtype = parser_factory.get_parser(filename) # type: ignore |
| 90 | except ValueError as e: | 96 | except ValueError as e: |
| 91 | print("[-] something went wrong when processing %s: %s" % (filename, e)) | 97 | __print_without_chars("[-] something went wrong when processing %s: %s" % (filename, e)) |
| 92 | return | 98 | return |
| 93 | if p is None: | 99 | if p is None: |
| 94 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) | 100 | __print_without_chars("[-] %s's format (%s) is not supported" % (filename, mtype)) |
| 95 | return | 101 | return |
| 96 | p.sandbox = sandbox | 102 | p.sandbox = sandbox |
| 97 | __print_meta(filename, p.get_meta()) | 103 | __print_meta(filename, p.get_meta()) |
| @@ -100,28 +106,22 @@ def show_meta(filename: str, sandbox: bool): | |||
| 100 | def __print_meta(filename: str, metadata: dict, depth: int = 1): | 106 | def __print_meta(filename: str, metadata: dict, depth: int = 1): |
| 101 | padding = " " * depth*2 | 107 | padding = " " * depth*2 |
| 102 | if not metadata: | 108 | if not metadata: |
| 103 | print(padding + "No metadata found in %s." % filename) | 109 | __print_without_chars(padding + "No metadata found in %s." % filename) |
| 104 | return | 110 | return |
| 105 | 111 | ||
| 106 | print("[%s] Metadata for %s:" % ('+'*depth, filename)) | 112 | __print_without_chars("[%s] Metadata for %s:" % ('+'*depth, filename)) |
| 107 | 113 | ||
| 108 | for (k, v) in sorted(metadata.items()): | 114 | for (k, v) in sorted(metadata.items()): |
| 109 | if isinstance(v, dict): | 115 | if isinstance(v, dict): |
| 110 | __print_meta(k, v, depth+1) | 116 | __print_meta(k, v, depth+1) |
| 111 | continue | 117 | continue |
| 112 | 118 | ||
| 113 | # Remove control characters | ||
| 114 | # We might use 'Cc' instead of 'C', but better safe than sorry | ||
| 115 | # https://www.unicode.org/reports/tr44/#GC_Values_Table | ||
| 116 | try: | ||
| 117 | v = ''.join(ch for ch in v if not unicodedata.category(ch).startswith('C')) | ||
| 118 | except TypeError: | ||
| 119 | pass # for things that aren't iterable | ||
| 120 | |||
| 121 | try: # FIXME this is ugly. | 119 | try: # FIXME this is ugly. |
| 122 | print(padding + " %s: %s" % (k, v)) | 120 | __print_without_chars(padding + " %s: %s" % (k, v)) |
| 123 | except UnicodeEncodeError: | 121 | except UnicodeEncodeError: |
| 124 | print(padding + " %s: harmful content" % k) | 122 | __print_without_chars(padding + " %s: harmful content" % k) |
| 123 | except TypeError: | ||
| 124 | pass # for things that aren't iterable | ||
| 125 | 125 | ||
| 126 | 126 | ||
| 127 | def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool, | 127 | def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool, |
| @@ -133,10 +133,10 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool | |||
| 133 | try: | 133 | try: |
| 134 | p, mtype = parser_factory.get_parser(filename) # type: ignore | 134 | p, mtype = parser_factory.get_parser(filename) # type: ignore |
| 135 | except ValueError as e: | 135 | except ValueError as e: |
| 136 | print("[-] something went wrong when cleaning %s: %s" % (filename, e)) | 136 | __print_without_chars("[-] something went wrong when cleaning %s: %s" % (filename, e)) |
| 137 | return False | 137 | return False |
| 138 | if p is None: | 138 | if p is None: |
| 139 | print("[-] %s's format (%s) is not supported" % (filename, mtype)) | 139 | __print_without_chars("[-] %s's format (%s) is not supported" % (filename, mtype)) |
| 140 | return False | 140 | return False |
| 141 | p.unknown_member_policy = policy | 141 | p.unknown_member_policy = policy |
| 142 | p.lightweight_cleaning = is_lightweight | 142 | p.lightweight_cleaning = is_lightweight |
| @@ -151,7 +151,7 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool | |||
| 151 | os.rename(p.output_filename, filename) | 151 | os.rename(p.output_filename, filename) |
| 152 | return ret | 152 | return ret |
| 153 | except RuntimeError as e: | 153 | except RuntimeError as e: |
| 154 | print("[-] %s can't be cleaned: %s" % (filename, e)) | 154 | __print_without_chars("[-] %s can't be cleaned: %s" % (filename, e)) |
| 155 | return False | 155 | return False |
| 156 | 156 | ||
| 157 | 157 | ||
| @@ -169,7 +169,7 @@ def show_parsers(): | |||
| 169 | # mimetype, so there is not point in showing the mimetype at all | 169 | # mimetype, so there is not point in showing the mimetype at all |
| 170 | continue | 170 | continue |
| 171 | formats.add(' - %s (%s)' % (mtype, ', '.join(extensions))) | 171 | formats.add(' - %s (%s)' % (mtype, ', '.join(extensions))) |
| 172 | print('\n'.join(sorted(formats))) | 172 | __print_without_chars('\n'.join(sorted(formats))) |
| 173 | 173 | ||
| 174 | 174 | ||
| 175 | def __get_files_recursively(files: List[str]) -> List[str]: | 175 | def __get_files_recursively(files: List[str]) -> List[str]: |
| @@ -198,9 +198,9 @@ def main() -> int: | |||
| 198 | show_parsers() | 198 | show_parsers() |
| 199 | return 0 | 199 | return 0 |
| 200 | elif args.check_dependencies: | 200 | elif args.check_dependencies: |
| 201 | print("Dependencies for mat2 %s:" % __version__) | 201 | __print_without_chars("Dependencies for mat2 %s:" % __version__) |
| 202 | for key, value in sorted(check_dependencies().items()): | 202 | for key, value in sorted(check_dependencies().items()): |
| 203 | print('- %s: %s %s' % (key, 'yes' if value['found'] else 'no', | 203 | __print_without_chars('- %s: %s %s' % (key, 'yes' if value['found'] else 'no', |
| 204 | '(optional)' if not value['required'] else '')) | 204 | '(optional)' if not value['required'] else '')) |
| 205 | else: | 205 | else: |
| 206 | arg_parser.print_help() | 206 | arg_parser.print_help() |
