From 8d257510d0ab80b868260210678c8ebe310a94c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 2 Dec 2015 16:21:15 +0100 Subject: [PATCH] rework output module --- gallery_dl/output.py | 110 ++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 64 deletions(-) diff --git a/gallery_dl/output.py b/gallery_dl/output.py index 3f6b24ad..6e37df23 100644 --- a/gallery_dl/output.py +++ b/gallery_dl/output.py @@ -14,107 +14,89 @@ import platform def select(): """Automatically select a suitable printer class""" if hasattr(sys.stdout, "isatty") and sys.stdout.isatty(): - return ColorPrinter if ANSI else TerminalPrinter + return ColorPrinter() if ANSI else TerminalPrinter() else: - return Printer + return Printer() + +def safeprint(txt, **kwargs): + """Handle unicode errors and replace invalid characters""" + try: + print(txt, **kwargs) + except UnicodeEncodeError: + enc = sys.stdout.encoding + txt = txt.encode(enc, errors="replace").decode(enc) + print(txt, **kwargs) class Printer(): - @staticmethod - def start(path): + def start(self, path): """Print a message indicating the start of a download""" pass - @staticmethod - def skip(path): + def skip(self, path): """Print a message indicating that a download has been skipped""" print(CHAR_SKIP, path, sep="", flush=True) - @staticmethod - def success(path, tries): + def success(self, path, tries): """Print a message indicating the completion of a download""" print(path, flush=True) - @staticmethod - def error(file, error, tries, max_tries): + def error(self, file, error, tries, max_tries): """Print a message indicating an error during download""" pass -class TerminalPrinter(): +class TerminalPrinter(Printer): - @staticmethod - def start(path): - """Print a message indicating the start of a download""" - _safeprint(_shorten(" " + path), end="", flush=True) + def __init__(self): + self.width = shutil.get_terminal_size().columns - OFFSET - @staticmethod - def skip(path): - """Print a message indicating that a download has been skipped""" - _safeprint(_shorten(CHAR_SKIP + path)) + def start(self, path): + safeprint(self.shorten(" " + path), end="", flush=True) - @staticmethod - def success(path, tries): - """Print a message indicating the completion of a download""" + def skip(self, path): + safeprint(self.shorten(CHAR_SKIP + path)) + + def success(self, path, tries): print("\r", end="") - _safeprint(_shorten(CHAR_SUCCESS + path)) + safeprint(self.shorten(CHAR_SUCCESS + path)) - @staticmethod - def error(file, error, tries, max_tries): - """Print a message indicating an error during download""" + def error(self, file, error, tries, max_tries): if tries <= 1 and hasattr(file, "name"): print("\r", end="") - _safeprint(_shorten(CHAR_ERROR + file.name)) + safeprint(self.shorten(CHAR_ERROR + file.name)) print("[Error] ", end="") - _safeprint(error, end="") + safeprint(error, end="") print(" (", tries, "/", max_tries, ")", sep="") + def shorten(self, txt): + """Reduce the length of 'txt' to the width of the terminal""" + if len(txt) > self.width: + hwidth = self.width // 2 - OFFSET + return "".join((txt[:hwidth-1], CHAR_ELLIPSIES, txt[-hwidth-(self.width%2):])) + return txt -class ColorPrinter(): - @staticmethod - def start(path): - """Print a message indicating the start of a download""" - print(_shorten(path), end="", flush=True) +class ColorPrinter(TerminalPrinter): - @staticmethod - def skip(path): - """Print a message indicating that a download has been skipped""" - print("\033[2m", _shorten(path), "\033[0m", sep="") + def start(self, path): + print(self.shorten(path), end="", flush=True) - @staticmethod - def success(path, tries): - """Print a message indicating the completion of a download""" - if tries == 0: - print("\r", end="") - print("\r\033[1;32m", _shorten(path), "\033[0m", sep="") + def skip(self, path): + print("\033[2m", self.shorten(path), "\033[0m", sep="") - @staticmethod - def error(file, error, tries, max_tries): - """Print a message indicating an error during download""" + def success(self, path, tries): + # if tries == 0: + # print("\r", end="") + print("\r\033[1;32m", self.shorten(path), "\033[0m", sep="") + + def error(self, file, error, tries, max_tries): if tries <= 1 and hasattr(file, "name"): - print("\r\033[1;31m", _shorten(file.name), sep="") + print("\r\033[1;31m", self.shorten(file.name), sep="") print("\033[0;31m[Error]\033[0m ", error, " (", tries, "/", max_tries, ")", sep="") -def _shorten(txt): - """Reduce the length of 'txt' to the width of the terminal""" - width = shutil.get_terminal_size().columns - OFFSET - if len(txt) > width: - hwidth = width // 2 - OFFSET - return "".join((txt[:hwidth-1], CHAR_ELLIPSIES, txt[-hwidth-(width%2):])) - return txt - -def _safeprint(txt, **kwargs): - """Handle unicode errors and replace invalid characters""" - try: - print(txt, **kwargs) - except UnicodeEncodeError: - enc = sys.stdout.encoding - txt = txt.encode(enc, errors="replace").decode(enc) - print(txt, **kwargs) - if platform.system() == "Windows": ANSI = os.environ.get("TERM") == "ANSI" OFFSET = 1