From 40eaf8435414c6f0a3c7968942b90b5fa85f4d44 Mon Sep 17 00:00:00 2001 From: MichaƂ Klichowicz Date: Sat, 13 Apr 2024 13:17:56 +0200 Subject: In a console-less setup, e.g. GUI, revert to any sane log encoding Fixes #55 --- jfr_playoff/dto.py | 7 +++++-- jfr_playoff/logger.py | 6 ++++++ jfr_playoff/settings.py | 4 ++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/jfr_playoff/dto.py b/jfr_playoff/dto.py index 44fe8cb..605493a 100644 --- a/jfr_playoff/dto.py +++ b/jfr_playoff/dto.py @@ -1,6 +1,9 @@ from math import ceil, floor import sys +from jfr_playoff.logger import log_encoding + + def coalesce(*arg): for el in arg: if el is not None: @@ -39,7 +42,7 @@ class Team(object): return u'%s (%.1f)' % (coalesce(self.name, ''), self.score) def __repr__(self): - return unicode(self).encode(sys.stdin.encoding) + return unicode(self).encode(log_encoding()) class Match(object): @@ -61,7 +64,7 @@ class Match(object): self.id, coalesce(self.link, ''), [unicode(team) for team in self.teams], u'finished' if self.running < 0 else ( u'%d boards' % self.running)) - ).encode(sys.stdin.encoding) + ).encode(log_encoding()) class Phase(object): diff --git a/jfr_playoff/logger.py b/jfr_playoff/logger.py index 1218022..3f15c49 100644 --- a/jfr_playoff/logger.py +++ b/jfr_playoff/logger.py @@ -1,4 +1,10 @@ import logging as log +import sys + + +def log_encoding(): + # if there's no sys.stdin (e.g. console-less GUI), revert to default + return sys.stdin.encoding or 'utf8' class PlayoffLogger: diff --git a/jfr_playoff/settings.py b/jfr_playoff/settings.py index b12ffef..d997e42 100644 --- a/jfr_playoff/settings.py +++ b/jfr_playoff/settings.py @@ -4,7 +4,7 @@ import readline import requests import sys -from jfr_playoff.logger import PlayoffLogger +from jfr_playoff.logger import PlayoffLogger, log_encoding def complete_filename(text, state): @@ -47,7 +47,7 @@ class PlayoffSettings(object): readline.parse_and_bind("tab: complete") readline.set_completer(complete_filename) self.settings_file = raw_input( - 'JSON settings file: ').decode(sys.stdin.encoding) + 'JSON settings file: ').decode(log_encoding()) if self.settings is None: PlayoffLogger.get('settings').info( -- cgit v1.2.3