Escape more control chars in the cli
This commit is contained in:
parent
05b8e97b68
commit
1c3e2afa1e
46
mat2
46
mat2
@ -26,13 +26,19 @@ assert Union
|
|||||||
|
|
||||||
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING)
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.WARNING)
|
||||||
|
|
||||||
|
def __print_without_chars(s: str):
|
||||||
|
""" Remove control characters
|
||||||
|
We might use 'Cc' instead of 'C', but better safe than sorry
|
||||||
|
https://www.unicode.org/reports/tr44/#GC_Values_Table
|
||||||
|
"""
|
||||||
|
print(''.join(ch for ch in s if not unicodedata.category(ch).startswith('C')))
|
||||||
|
|
||||||
def __check_file(filename: str, mode: int = os.R_OK) -> bool:
|
def __check_file(filename: str, mode: int = os.R_OK) -> bool:
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
print("[-] %s doesn't exist." % filename)
|
__print_without_chars("[-] %s doesn't exist." % filename)
|
||||||
return False
|
return False
|
||||||
elif not os.path.isfile(filename):
|
elif not os.path.isfile(filename):
|
||||||
print("[-] %s is not a regular file." % filename)
|
__print_without_chars("[-] %s is not a regular file." % filename)
|
||||||
return False
|
return False
|
||||||
elif not os.access(filename, mode):
|
elif not os.access(filename, mode):
|
||||||
mode_str = [] # type: List[str]
|
mode_str = [] # type: List[str]
|
||||||
@ -40,7 +46,7 @@ def __check_file(filename: str, mode: int = os.R_OK) -> bool:
|
|||||||
mode_str += 'readable'
|
mode_str += 'readable'
|
||||||
if mode & os.W_OK:
|
if mode & os.W_OK:
|
||||||
mode_str += 'writeable'
|
mode_str += 'writeable'
|
||||||
print("[-] %s is not %s." % (filename, 'nor '.join(mode_str)))
|
__print_without_chars("[-] %s is not %s." % (filename, 'nor '.join(mode_str)))
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -88,10 +94,10 @@ def show_meta(filename: str, sandbox: bool):
|
|||||||
try:
|
try:
|
||||||
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print("[-] something went wrong when processing %s: %s" % (filename, e))
|
__print_without_chars("[-] something went wrong when processing %s: %s" % (filename, e))
|
||||||
return
|
return
|
||||||
if p is None:
|
if p is None:
|
||||||
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
__print_without_chars("[-] %s's format (%s) is not supported" % (filename, mtype))
|
||||||
return
|
return
|
||||||
p.sandbox = sandbox
|
p.sandbox = sandbox
|
||||||
__print_meta(filename, p.get_meta())
|
__print_meta(filename, p.get_meta())
|
||||||
@ -100,29 +106,23 @@ def show_meta(filename: str, sandbox: bool):
|
|||||||
def __print_meta(filename: str, metadata: dict, depth: int = 1):
|
def __print_meta(filename: str, metadata: dict, depth: int = 1):
|
||||||
padding = " " * depth*2
|
padding = " " * depth*2
|
||||||
if not metadata:
|
if not metadata:
|
||||||
print(padding + "No metadata found in %s." % filename)
|
__print_without_chars(padding + "No metadata found in %s." % filename)
|
||||||
return
|
return
|
||||||
|
|
||||||
print("[%s] Metadata for %s:" % ('+'*depth, filename))
|
__print_without_chars("[%s] Metadata for %s:" % ('+'*depth, filename))
|
||||||
|
|
||||||
for (k, v) in sorted(metadata.items()):
|
for (k, v) in sorted(metadata.items()):
|
||||||
if isinstance(v, dict):
|
if isinstance(v, dict):
|
||||||
__print_meta(k, v, depth+1)
|
__print_meta(k, v, depth+1)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Remove control characters
|
try: # FIXME this is ugly.
|
||||||
# We might use 'Cc' instead of 'C', but better safe than sorry
|
__print_without_chars(padding + " %s: %s" % (k, v))
|
||||||
# https://www.unicode.org/reports/tr44/#GC_Values_Table
|
except UnicodeEncodeError:
|
||||||
try:
|
__print_without_chars(padding + " %s: harmful content" % k)
|
||||||
v = ''.join(ch for ch in v if not unicodedata.category(ch).startswith('C'))
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass # for things that aren't iterable
|
pass # for things that aren't iterable
|
||||||
|
|
||||||
try: # FIXME this is ugly.
|
|
||||||
print(padding + " %s: %s" % (k, v))
|
|
||||||
except UnicodeEncodeError:
|
|
||||||
print(padding + " %s: harmful content" % k)
|
|
||||||
|
|
||||||
|
|
||||||
def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool,
|
def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool,
|
||||||
policy: UnknownMemberPolicy) -> bool:
|
policy: UnknownMemberPolicy) -> bool:
|
||||||
@ -133,10 +133,10 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool
|
|||||||
try:
|
try:
|
||||||
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
p, mtype = parser_factory.get_parser(filename) # type: ignore
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
print("[-] something went wrong when cleaning %s: %s" % (filename, e))
|
__print_without_chars("[-] something went wrong when cleaning %s: %s" % (filename, e))
|
||||||
return False
|
return False
|
||||||
if p is None:
|
if p is None:
|
||||||
print("[-] %s's format (%s) is not supported" % (filename, mtype))
|
__print_without_chars("[-] %s's format (%s) is not supported" % (filename, mtype))
|
||||||
return False
|
return False
|
||||||
p.unknown_member_policy = policy
|
p.unknown_member_policy = policy
|
||||||
p.lightweight_cleaning = is_lightweight
|
p.lightweight_cleaning = is_lightweight
|
||||||
@ -151,7 +151,7 @@ def clean_meta(filename: str, is_lightweight: bool, inplace: bool, sandbox: bool
|
|||||||
os.rename(p.output_filename, filename)
|
os.rename(p.output_filename, filename)
|
||||||
return ret
|
return ret
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
print("[-] %s can't be cleaned: %s" % (filename, e))
|
__print_without_chars("[-] %s can't be cleaned: %s" % (filename, e))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@ -169,7 +169,7 @@ def show_parsers():
|
|||||||
# mimetype, so there is not point in showing the mimetype at all
|
# mimetype, so there is not point in showing the mimetype at all
|
||||||
continue
|
continue
|
||||||
formats.add(' - %s (%s)' % (mtype, ', '.join(extensions)))
|
formats.add(' - %s (%s)' % (mtype, ', '.join(extensions)))
|
||||||
print('\n'.join(sorted(formats)))
|
__print_without_chars('\n'.join(sorted(formats)))
|
||||||
|
|
||||||
|
|
||||||
def __get_files_recursively(files: List[str]) -> List[str]:
|
def __get_files_recursively(files: List[str]) -> List[str]:
|
||||||
@ -198,9 +198,9 @@ def main() -> int:
|
|||||||
show_parsers()
|
show_parsers()
|
||||||
return 0
|
return 0
|
||||||
elif args.check_dependencies:
|
elif args.check_dependencies:
|
||||||
print("Dependencies for mat2 %s:" % __version__)
|
__print_without_chars("Dependencies for mat2 %s:" % __version__)
|
||||||
for key, value in sorted(check_dependencies().items()):
|
for key, value in sorted(check_dependencies().items()):
|
||||||
print('- %s: %s %s' % (key, 'yes' if value['found'] else 'no',
|
__print_without_chars('- %s: %s %s' % (key, 'yes' if value['found'] else 'no',
|
||||||
'(optional)' if not value['required'] else ''))
|
'(optional)' if not value['required'] else ''))
|
||||||
else:
|
else:
|
||||||
arg_parser.print_help()
|
arg_parser.print_help()
|
||||||
|
Loading…
Reference in New Issue
Block a user