1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 11:12:40 +01:00
gallery-dl/gallery_dl/output.py

222 lines
6.4 KiB
Python
Raw Normal View History

2015-12-01 21:21:39 +01:00
# -*- coding: utf-8 -*-
# Copyright 2015-2019 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
import logging
from . import config, util
2015-12-01 21:21:39 +01:00
2017-01-30 19:40:15 +01:00
# --------------------------------------------------------------------
# Logging
LOG_FORMAT = "[{name}][{levelname}] {message}"
LOG_FORMAT_DATE = "%Y-%m-%d %H:%M:%S"
LOG_LEVEL = logging.INFO
class Logger(logging.Logger):
"""Custom logger that includes extractor and job info in log records"""
extractor = util.NONE
job = util.NONE
def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
func=None, extra=None, sinfo=None,
factory=logging._logRecordFactory):
rv = factory(name, level, fn, lno, msg, args, exc_info, func, sinfo)
rv.extractor = self.extractor
rv.job = self.job
return rv
def initialize_logging(loglevel):
"""Setup basic logging functionality before configfiles have been loaded"""
# convert levelnames to lowercase
for level in (10, 20, 30, 40, 50):
name = logging.getLevelName(level)
logging.addLevelName(level, name.lower())
# register custom Logging class
logging.Logger.manager.setLoggerClass(Logger)
# setup basic logging to stderr
formatter = logging.Formatter(LOG_FORMAT, LOG_FORMAT_DATE, "{")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(loglevel)
root = logging.getLogger()
root.setLevel(logging.NOTSET)
root.addHandler(handler)
return logging.getLogger("gallery-dl")
def setup_logging_handler(key, fmt=LOG_FORMAT, lvl=LOG_LEVEL):
"""Setup a new logging handler"""
opts = config.interpolate(("output", key))
if not opts:
return None
if not isinstance(opts, dict):
opts = {"path": opts}
path = opts.get("path")
mode = opts.get("mode", "w")
encoding = opts.get("encoding", "utf-8")
try:
path = util.expand_path(path)
handler = logging.FileHandler(path, mode, encoding)
except (OSError, ValueError) as exc:
logging.getLogger("gallery-dl").warning(
"%s: %s", key, exc)
return None
except TypeError as exc:
logging.getLogger("gallery-dl").warning(
"%s: missing or invalid path (%s)", key, exc)
return None
level = opts.get("level", lvl)
logfmt = opts.get("format", fmt)
datefmt = opts.get("format-date", LOG_FORMAT_DATE)
formatter = logging.Formatter(logfmt, datefmt, "{")
handler.setFormatter(formatter)
handler.setLevel(level)
return handler
def configure_logging_handler(key, handler):
"""Configure a logging handler"""
opts = config.interpolate(("output", key))
if not opts:
return
if isinstance(opts, str):
opts = {"format": opts}
if handler.level == LOG_LEVEL and "level" in opts:
handler.setLevel(opts["level"])
if "format" in opts or "format-date" in opts:
logfmt = opts.get("format", LOG_FORMAT)
datefmt = opts.get("format-date", LOG_FORMAT_DATE)
formatter = logging.Formatter(logfmt, datefmt, "{")
handler.setFormatter(formatter)
# --------------------------------------------------------------------
# Utility functions
def replace_std_streams(errors="replace"):
"""Replace standard streams and set their error handlers to 'errors'"""
for name in ("stdout", "stdin", "stderr"):
stream = getattr(sys, name)
setattr(sys, name, stream.__class__(
stream.buffer,
errors=errors,
newline=stream.newlines,
line_buffering=stream.line_buffering,
))
# --------------------------------------------------------------------
# Downloader output
2015-12-01 21:21:39 +01:00
def select():
2017-04-26 11:33:19 +02:00
"""Automatically select a suitable output class"""
pdict = {
2017-04-26 11:33:19 +02:00
"default": PipeOutput,
"pipe": PipeOutput,
"term": TerminalOutput,
"terminal": TerminalOutput,
"color": ColorOutput,
"null": NullOutput,
}
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():
2017-04-26 11:33:19 +02:00
return ColorOutput() if ANSI else TerminalOutput()
else:
2017-04-26 11:33:19 +02:00
return PipeOutput()
2015-12-01 21:21:39 +01:00
else:
raise Exception("invalid output mode: " + omode)
2015-12-02 16:21:15 +01:00
2017-01-30 19:40:15 +01:00
2017-04-26 11:33:19 +02:00
class NullOutput():
2015-12-01 21:21:39 +01:00
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"""
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-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"""
2017-04-26 11:33:19 +02:00
class PipeOutput(NullOutput):
def skip(self, path):
print(CHAR_SKIP, path, sep="", flush=True)
def success(self, path, tries):
print(path, flush=True)
class TerminalOutput(NullOutput):
2015-12-01 21:21:39 +01:00
2015-12-02 16:21:15 +01:00
def __init__(self):
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):
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(self.shorten(CHAR_SKIP + path))
2015-12-02 16:21:15 +01:00
def success(self, path, tries):
print("\r", self.shorten(CHAR_SUCCESS + path), 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"""
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
2017-04-26 11:33:19 +02:00
class ColorOutput(TerminalOutput):
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="")
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_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 = "# "
CHAR_SUCCESS = ""
2015-12-01 23:54:57 +01:00
CHAR_ELLIPSIES = ""