1
0
mirror of https://github.com/kellyjonbrazil/jc.git synced 2025-06-17 00:07:37 +02:00

refactor try/except blocks to safe_print function

This commit is contained in:
Kelly Brazil
2022-04-26 14:25:34 -07:00
parent 0b407123c2
commit a4ef52b533
2 changed files with 27 additions and 26 deletions

View File

@ -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: