2015-12-01 21:21:39 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2017-04-06 16:45:16 +02:00
|
|
|
# Copyright 2015-2017 Mike Fährmann
|
2015-12-01 21:21:39 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License version 2 as
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import shutil
|
2015-12-02 18:47:42 +01:00
|
|
|
from . import config
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2015-12-01 21:21:39 +01:00
|
|
|
def select():
|
|
|
|
"""Automatically select a suitable printer class"""
|
2015-12-02 18:47:42 +01:00
|
|
|
pdict = {
|
|
|
|
"default": Printer,
|
|
|
|
"pipe": Printer,
|
|
|
|
"term": TerminalPrinter,
|
|
|
|
"terminal": TerminalPrinter,
|
|
|
|
"color": ColorPrinter,
|
|
|
|
}
|
|
|
|
omode = config.get(("output", "mode"), "auto").lower()
|
|
|
|
if omode in pdict:
|
|
|
|
return pdict[omode]()
|
|
|
|
elif omode == "auto":
|
|
|
|
if hasattr(sys.stdout, "isatty") and sys.stdout.isatty():
|
|
|
|
return ColorPrinter() if ANSI else TerminalPrinter()
|
|
|
|
else:
|
|
|
|
return Printer()
|
2015-12-01 21:21:39 +01:00
|
|
|
else:
|
2015-12-02 18:47:42 +01:00
|
|
|
raise Exception("invalid output mode: " + omode)
|
2015-12-02 16:21:15 +01:00
|
|
|
|
2017-01-30 19:40:15 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
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)
|
2015-12-01 21:21:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Printer():
|
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def start(self, path):
|
2015-12-01 21:21:39 +01:00
|
|
|
"""Print a message indicating the start of a download"""
|
|
|
|
pass
|
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def skip(self, path):
|
2015-12-01 21:21:39 +01:00
|
|
|
"""Print a message indicating that a download has been skipped"""
|
2015-12-01 23:54:57 +01:00
|
|
|
print(CHAR_SKIP, path, sep="", flush=True)
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def success(self, path, tries):
|
2015-12-01 21:21:39 +01:00
|
|
|
"""Print a message indicating the completion of a download"""
|
|
|
|
print(path, flush=True)
|
|
|
|
|
2017-04-06 16:45:16 +02:00
|
|
|
def error(self, path, error, tries, max_tries):
|
2015-12-01 21:21:39 +01:00
|
|
|
"""Print a message indicating an error during download"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
class TerminalPrinter(Printer):
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def __init__(self):
|
2015-12-02 18:47:42 +01:00
|
|
|
self.short = config.get(("output", "shorten"), True)
|
|
|
|
if self.short:
|
|
|
|
self.width = shutil.get_terminal_size().columns - OFFSET
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def start(self, path):
|
2015-12-02 18:49:49 +01:00
|
|
|
safeprint(self.shorten(" " + path), end="", flush=True)
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def skip(self, path):
|
|
|
|
safeprint(self.shorten(CHAR_SKIP + path))
|
|
|
|
|
|
|
|
def success(self, path, tries):
|
2015-12-01 23:54:57 +01:00
|
|
|
print("\r", end="")
|
2015-12-02 16:21:15 +01:00
|
|
|
safeprint(self.shorten(CHAR_SUCCESS + path))
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2017-04-06 16:45:16 +02:00
|
|
|
def error(self, path, error, tries, max_tries):
|
|
|
|
if tries <= 1 and path:
|
2015-12-01 23:54:57 +01:00
|
|
|
print("\r", end="")
|
2017-04-06 16:45:16 +02:00
|
|
|
safeprint(self.shorten(CHAR_ERROR + path))
|
|
|
|
print("\r[Error] ", end="")
|
2015-12-02 16:21:15 +01:00
|
|
|
safeprint(error, end="")
|
2015-12-01 23:54:57 +01:00
|
|
|
print(" (", tries, "/", max_tries, ")", sep="")
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def shorten(self, txt):
|
|
|
|
"""Reduce the length of 'txt' to the width of the terminal"""
|
2015-12-02 18:47:42 +01:00
|
|
|
if self.short and len(txt) > self.width:
|
2015-12-02 16:21:15 +01:00
|
|
|
hwidth = self.width // 2 - OFFSET
|
2017-01-30 19:40:15 +01:00
|
|
|
return "".join((
|
|
|
|
txt[:hwidth-1],
|
|
|
|
CHAR_ELLIPSIES,
|
|
|
|
txt[-hwidth-(self.width % 2):]
|
|
|
|
))
|
2015-12-02 16:21:15 +01:00
|
|
|
return txt
|
2015-12-01 21:21:39 +01:00
|
|
|
|
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
class ColorPrinter(TerminalPrinter):
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def start(self, path):
|
|
|
|
print(self.shorten(path), end="", flush=True)
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def skip(self, path):
|
|
|
|
print("\033[2m", self.shorten(path), "\033[0m", sep="")
|
2015-12-01 21:21:39 +01:00
|
|
|
|
2015-12-02 16:21:15 +01:00
|
|
|
def success(self, path, tries):
|
|
|
|
print("\r\033[1;32m", self.shorten(path), "\033[0m", sep="")
|
|
|
|
|
2017-04-06 16:45:16 +02:00
|
|
|
def error(self, path, error, tries, max_tries):
|
|
|
|
if tries <= 1 and path:
|
|
|
|
print("\r\033[1;31m", self.shorten(path), sep="")
|
|
|
|
print("\r\033[0;31m[Error]\033[0m ", error,
|
2017-01-30 19:40:15 +01:00
|
|
|
" (", tries, "/", max_tries, ")", sep="")
|
2015-12-01 21:21:39 +01:00
|
|
|
|
|
|
|
|
2016-10-25 15:44:36 +02:00
|
|
|
if os.name == "nt":
|
2015-12-01 23:54:57 +01:00
|
|
|
ANSI = os.environ.get("TERM") == "ANSI"
|
|
|
|
OFFSET = 1
|
2015-12-02 18:49:49 +01:00
|
|
|
CHAR_SKIP = "# "
|
|
|
|
CHAR_ERROR = "! "
|
|
|
|
CHAR_SUCCESS = "* "
|
2015-12-01 23:54:57 +01:00
|
|
|
CHAR_ELLIPSIES = "..."
|
|
|
|
else:
|
|
|
|
ANSI = True
|
|
|
|
OFFSET = 0
|
2015-12-02 18:49:49 +01:00
|
|
|
CHAR_SKIP = "# "
|
2017-04-06 16:45:16 +02:00
|
|
|
CHAR_ERROR = "✖ "
|
2015-12-02 18:49:49 +01:00
|
|
|
CHAR_SUCCESS = "✔ "
|
2015-12-01 23:54:57 +01:00
|
|
|
CHAR_ELLIPSIES = "…"
|