Handle UnicodeEncodeError's in a cleaner way

This commit is contained in:
Lonami Exo 2017-05-29 17:07:09 +02:00
parent 452532cce7
commit ae1dbc63da

View File

@ -10,16 +10,21 @@ from .utils import get_display_name
cols, rows = shutil.get_terminal_size() cols, rows = shutil.get_terminal_size()
def sprint(string, *args, **kwargs):
"""Safe Print (handle UnicodeEncodeErrors on some terminals)"""
try:
print(string, *args, **kwargs)
except UnicodeEncodeError:
string = string.encode('utf-8', errors='ignore')\
.decode('ascii', errors='ignore')
print(string, *args, **kwargs)
def print_title(title): def print_title(title):
# Clear previous window # Clear previous window
print('\n') print('\n')
print('=={}=='.format('=' * len(title))) print('=={}=='.format('=' * len(title)))
try: sprint('= {} ='.format(title))
print('= {} ='.format(title))
except UnicodeEncodeError:
# Issue #70, some consoles lack support for certain characters
title_ascii = title.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('= {} ='.format(title_ascii))
print('=={}=='.format('=' * len(title))) print('=={}=='.format('=' * len(title)))
@ -88,13 +93,7 @@ class InteractiveTelegramClient(TelegramClient):
# Display them so the user can choose # Display them so the user can choose
for i, entity in enumerate(entities): for i, entity in enumerate(entities):
i += 1 # 1-based index i += 1 # 1-based index
try: sprint('{}. {}'.format(i, get_display_name(entity)))
print('{}. {}'.format(i, get_display_name(entity)))
except UnicodeEncodeError:
# Issue #70, some consoles lack support for certain characters
name = get_display_name(entity)
name_ascii = name.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('{}. {}'.format(i, name_ascii))
# Let the user decide who they want to talk to # Let the user decide who they want to talk to
print() print()
@ -173,9 +172,9 @@ class InteractiveTelegramClient(TelegramClient):
content = msg.__class__.__name__ content = msg.__class__.__name__
# And print it to the user # And print it to the user
print('[{}:{}] (ID={}) {}: {}'.format( sprint('[{}:{}] (ID={}) {}: {}'.format(
msg.date.hour, msg.date.minute, msg.id, name, msg.date.hour, msg.date.minute, msg.id, name,
content)) content))
# Send photo # Send photo
elif msg.startswith('!up '): elif msg.startswith('!up '):
@ -266,38 +265,17 @@ class InteractiveTelegramClient(TelegramClient):
def update_handler(update_object): def update_handler(update_object):
if type(update_object) is UpdateShortMessage: if type(update_object) is UpdateShortMessage:
if update_object.out: if update_object.out:
try: sprint('You sent {} to user #{}'.format(
print('You sent {} to user #{}'.format(update_object.message, update_object.message, update_object.user_id))
update_object.user_id))
except UnicodeEncodeError:
message_ascii = update_object.message.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('You sent {} to user #{}'.format(message_ascii,
update_object.user_id))
else: else:
try: sprint('[User #{} sent {}]'.format(
print('[User #{} sent {}]'.format(update_object.user_id, update_object.user_id, update_object.message))
update_object.message))
except UnicodeEncodeError:
message_ascii = update_object.message.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('[User #{} sent {}]'.format(update_object.user_id,
message_ascii))
elif type(update_object) is UpdateShortChatMessage: elif type(update_object) is UpdateShortChatMessage:
if update_object.out: if update_object.out:
try: sprint('You sent {} to chat #{}'.format(
print('You sent {} to chat #{}'.format(update_object.message, update_object.message, update_object.chat_id))
update_object.chat_id))
except UnicodeEncodeError:
message_ascii = update_object.message.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('You sent {} to chat #{}'.format(message_ascii,
update_object.chat_id))
else: else:
try: sprint('[Chat #{}, user #{} sent {}]'.format(
print('[Chat #{}, user #{} sent {}]'.format( update_object.chat_id, update_object.from_id,
update_object.chat_id, update_object.from_id, update_object.message))
update_object.message))
except UnicodeEncodeError:
message_ascii = update_object.message.encode('utf-8',errors='ignore').decode('ascii',errors='ignore')
print('[Chat #{}, user #{} sent {}]'.format(
update_object.chat_id, update_object.from_id,
message_ascii))