From 67da4e69fa61d335c030360ae3576841928d9326 Mon Sep 17 00:00:00 2001 From: I-Al-Istannen Date: Fri, 7 Jun 2019 13:26:23 +0200 Subject: [PATCH] Add colorful log output Highlight the important operations (new, modified) in different colours. --- PFERD/ffm.py | 5 +++-- PFERD/ilias.py | 6 +++--- PFERD/norbert.py | 5 +++-- PFERD/organizer.py | 7 ++++--- PFERD/ti.py | 5 +++-- PFERD/utils.py | 31 +++++++++++++++++++++++++++++++ setup.py | 1 + 7 files changed, 48 insertions(+), 12 deletions(-) diff --git a/PFERD/ffm.py b/PFERD/ffm.py index 487b1b6..f72742e 100644 --- a/PFERD/ffm.py +++ b/PFERD/ffm.py @@ -8,10 +8,11 @@ import bs4 import requests from .organizer import Organizer -from .utils import stream_to_path +from .utils import stream_to_path, PrettyLogger __all__ = ["FfM"] logger = logging.getLogger(__name__) +pretty = PrettyLogger(logger) class FfM: BASE_URL = "http://www.math.kit.edu/" @@ -23,7 +24,7 @@ class FfM: self._session = requests.Session() def synchronize(self, urlpart, to_dir, transform=lambda x: x): - logger.info(f" Synchronizing {urlpart} to {to_dir} using the FfM synchronizer.") + pretty.starting_synchronizer(to_dir, "FfM", urlpart) sync_path = pathlib.Path(self.base_path, to_dir) diff --git a/PFERD/ilias.py b/PFERD/ilias.py index 07ecc5b..9b1b0a2 100644 --- a/PFERD/ilias.py +++ b/PFERD/ilias.py @@ -4,13 +4,13 @@ import logging import pathlib import re -import bs4 - from .ilias_authenticators import ShibbolethAuthenticator from .organizer import Organizer +from .utils import PrettyLogger __all__ = ["Ilias"] logger = logging.getLogger(__name__) +pretty = PrettyLogger(logger) class Ilias: FILE_RE = re.compile(r"goto\.php\?target=(file_\d+_download)") @@ -22,7 +22,7 @@ class Ilias: self._auth = ShibbolethAuthenticator(base_path / cookie_file) def synchronize(self, ref_id, to_dir, transform=lambda x: x, filter=lambda x: True): - logger.info(f" Synchronizing ref_id {ref_id} to {to_dir} using the Ilias synchronizer.") + pretty.starting_synchronizer(to_dir, "ILIAS", f"ref_id {ref_id}") sync_path = pathlib.Path(self.base_path, to_dir) orga = Organizer(self.base_path, sync_path) diff --git a/PFERD/norbert.py b/PFERD/norbert.py index 33c6350..1af79b7 100644 --- a/PFERD/norbert.py +++ b/PFERD/norbert.py @@ -9,10 +9,11 @@ import bs4 import requests from .organizer import Organizer -from .utils import rename, stream_to_path +from .utils import rename, stream_to_path, PrettyLogger __all__ = ["Norbert"] logger = logging.getLogger(__name__) +pretty = PrettyLogger(logger) class Norbert: BASE_URL = "https://studwww.informatik.kit.edu/~s_blueml/" @@ -24,7 +25,7 @@ class Norbert: self._session = requests.Session() def synchronize(self, to_dir, transform=lambda x: x, unzip=lambda _: True): - logger.info(f" Synchronizing to {to_dir} using the Norbert synchronizer.") + pretty.starting_synchronizer(to_dir, "Norbert") sync_path = pathlib.Path(self.base_path, to_dir) orga = Organizer(self.base_path, sync_path) diff --git a/PFERD/organizer.py b/PFERD/organizer.py index 6f19baf..38c58d2 100644 --- a/PFERD/organizer.py +++ b/PFERD/organizer.py @@ -7,6 +7,7 @@ from . import utils __all__ = ["Organizer"] logger = logging.getLogger(__name__) +pretty = utils.PrettyLogger(logger) class Organizer: def __init__(self, base_dir, sync_dir): @@ -64,7 +65,7 @@ class Organizer: if to_path.exists(): if filecmp.cmp(from_path, to_path, shallow=False): - logger.info(f"Ignored {to_path}") + pretty.ignored_file(to_path) # remember path for later reference self._added_files.add(to_path.resolve()) @@ -73,9 +74,9 @@ class Organizer: # No further action needed, especially not overwriting symlinks... return else: - logger.info(f"Different file at {to_path}") + pretty.modified_file(to_path) else: - logger.info(f"New file at {to_path}") + pretty.new_file(to_path) # copy the file from from_path to sync_dir/to_path # If the file being replaced was a symlink, the link itself is overwritten, diff --git a/PFERD/ti.py b/PFERD/ti.py index 434fc38..3559f6c 100644 --- a/PFERD/ti.py +++ b/PFERD/ti.py @@ -9,10 +9,11 @@ import bs4 import requests from .organizer import Organizer -from .utils import stream_to_path +from .utils import stream_to_path, PrettyLogger __all__ = ["Ti"] logger = logging.getLogger(__name__) +pretty = PrettyLogger(logger) class Ti: BASE_URL = "http://ti.ira.uka.de/" @@ -26,7 +27,7 @@ class Ti: def synchronize(self, urlpart, to_dir, transform=lambda x: x, filter=lambda x: True): - logger.info(f" Synchronizing {urlpart} to {to_dir} using the Ti synchronizer.") + pretty.starting_synchronizer(to_dir, "Ti", urlpart) sync_path = pathlib.Path(self.base_path, to_dir) diff --git a/PFERD/utils.py b/PFERD/utils.py index 0a9fb7a..4193e9b 100644 --- a/PFERD/utils.py +++ b/PFERD/utils.py @@ -1,5 +1,8 @@ import os +import sys import pathlib +from colorama import Style +from colorama import Fore __all__ = [ "get_base_dir", @@ -8,6 +11,7 @@ __all__ = [ "stream_to_path", "ContentTypeException", "FileNotFoundException", + "PrettyLogger", ] def get_base_dir(script_file): @@ -26,8 +30,35 @@ def stream_to_path(response, to_path, chunk_size=1024**2): for chunk in response.iter_content(chunk_size=chunk_size): fd.write(chunk) +def isOutputPipe(): + """Returns whether this program's output is attached to a pipe. + """ + return sys.stdout.isatty + class ContentTypeException(Exception): pass class FileNotFoundException(Exception): pass + +class PrettyLogger: + + def __init__(self, logger): + self.logger = logger + + def modified_file(self, file_name): + self.logger.info(f"{Fore.MAGENTA}{Style.BRIGHT}Modified {file_name}.{Style.RESET_ALL}") + + def new_file(self, file_name): + self.logger.info(f"{Fore.GREEN}{Style.BRIGHT}Created {file_name}.{Style.RESET_ALL}") + + def ignored_file(self, file_name): + self.logger.info(f"{Style.DIM}Ignored {file_name}.{Style.RESET_ALL}") + + def starting_synchronizer(self, target_directory, synchronizer_name, subject=None): + subject_str = f"{subject} " if subject else "" + self.logger.info("") + self.logger.info(( + f"{Fore.CYAN}{Style.BRIGHT}Synchronizing {subject_str}to {target_directory}" + f" using the {synchronizer_name} synchronizer.{Style.RESET_ALL}" + )) diff --git a/setup.py b/setup.py index cff70e1..1258880 100644 --- a/setup.py +++ b/setup.py @@ -7,6 +7,7 @@ setup( install_requires=[ "requests>=2.21.0", "beautifulsoup4>=4.7.1", + "colorama>=0.4.1" ], )