From a4ef52b5334c5e88cba8fcbff8a23312aa62fe33 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Tue, 26 Apr 2022 14:25:34 -0700 Subject: [PATCH] refactor try/except blocks to safe_print function --- jc/cli.py | 26 ++++---------------------- jc/utils.py | 27 +++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/jc/cli.py b/jc/cli.py index 75d7f41e..b8c6e607 100644 --- a/jc/cli.py +++ b/jc/cli.py @@ -84,16 +84,7 @@ if PYGMENTS_INSTALLED: } -def asciify(string): - """ - Return a string downgraded from Unicode to ASCII with some simple - conversions. - """ - string = string.replace('©', '(c)') - # the ascii() function adds single quotes around the string - string = ascii(string)[1:-1] - string = string.replace(r'\n', '\n') - return string + def set_env_colors(env_colors=None): @@ -449,17 +440,11 @@ def main(): sys.exit(0) if help_me: - try: - print(help_doc(sys.argv)) - except UnicodeEncodeError: - print(asciify(help_doc(sys.argv))) + utils.safe_print(help_doc(sys.argv)) sys.exit(0) if version_info: - try: - print(versiontext()) - except UnicodeEncodeError: - print(asciify(versiontext())) + utils.safe_print(versiontext()) sys.exit(0) # if magic syntax used, try to run the command and error if it's not found, etc. @@ -474,10 +459,7 @@ def main(): try: magic_stdout, magic_stderr, magic_exit_code = run_user_command(run_command) if magic_stderr: - try: - print(magic_stderr[:-1], file=sys.stderr) - except UnicodeEncodeError: - print(asciify(magic_stderr[:-1], file=sys.stderr)) + utils.safe_print(magic_stderr[:-1], file=sys.stderr) except OSError as e: if debug: diff --git a/jc/utils.py b/jc/utils.py index bb03bff1..493c6207 100644 --- a/jc/utils.py +++ b/jc/utils.py @@ -9,6 +9,25 @@ from functools import lru_cache from typing import List, Iterable, Union, Optional +def asciify(string: str) -> str: + """ + Return a string downgraded from Unicode to ASCII with some simple + conversions. + """ + string = string.replace('©', '(c)') + # the ascii() function adds single quotes around the string + string = ascii(string)[1:-1] + string = string.replace(r'\n', '\n') + return string + + +def safe_print(string: str, sep=' ', end='\n', file=sys.stdout, flush=False) -> None: + try: + print(string, sep=sep, end=end, file=file, flush=flush) + except UnicodeEncodeError: + print(asciify(string), sep=sep, end=end, file=file, flush=flush) + + def warning_message(message_lines: List[str]) -> None: """ Prints warning message for non-fatal issues. The first line is @@ -36,13 +55,13 @@ def warning_message(message_lines: List[str]) -> None: first_line = message_lines.pop(0) first_str = f'jc: Warning - {first_line}' first_str = first_wrapper.fill(first_str) - print(first_str, file=sys.stderr) + safe_print(first_str, file=sys.stderr) for line in message_lines: if line == '': continue message = next_wrapper.fill(line) - print(message, file=sys.stderr) + safe_print(message, file=sys.stderr) def error_message(message_lines: List[str]) -> None: @@ -68,13 +87,13 @@ def error_message(message_lines: List[str]) -> None: first_line = message_lines.pop(0) first_str = f'jc: Error - {first_line}' first_str = first_wrapper.fill(first_str) - print(first_str, file=sys.stderr) + safe_print(first_str, file=sys.stderr) for line in message_lines: if line == '': continue message = next_wrapper.fill(line) - print(message, file=sys.stderr) + safe_print(message, file=sys.stderr) def compatibility(mod_name: str, compatible: List, quiet: bool = False) -> None: