From cf6ac7944d08e64a1392a9ee4ec46a3624732418 Mon Sep 17 00:00:00 2001 From: Dylan Araps Date: Sun, 1 Apr 2018 12:28:32 +1000 Subject: [PATCH] general: Added logging. --- pywal/__main__.py | 2 ++ pywal/backends/colorthief.py | 12 ++++++------ pywal/backends/colorz.py | 5 +++-- pywal/backends/haishoku.py | 5 +++-- pywal/backends/schemer2.py | 5 +++-- pywal/backends/wal.py | 12 ++++++------ pywal/colors.py | 9 +++++---- pywal/export.py | 9 +++++---- pywal/image.py | 7 ++++--- pywal/reload.py | 11 ++++++----- pywal/scripts/gtk_reload.py | 2 +- pywal/sequences.py | 3 ++- pywal/theme.py | 3 ++- pywal/util.py | 14 +++++++++++++- pywal/wallpaper.py | 5 +++-- 15 files changed, 64 insertions(+), 40 deletions(-) diff --git a/pywal/__main__.py b/pywal/__main__.py index c1c4002..63be494 100644 --- a/pywal/__main__.py +++ b/pywal/__main__.py @@ -10,6 +10,7 @@ Created by Dylan Araps. """ import argparse +import logging import os import shutil import sys @@ -173,6 +174,7 @@ def process_args(args): def main(): """Main script function.""" + util.setup_logging() args = get_args(sys.argv[1:]) process_args(args) diff --git a/pywal/backends/colorthief.py b/pywal/backends/colorthief.py index 180af5f..7b2e2bc 100644 --- a/pywal/backends/colorthief.py +++ b/pywal/backends/colorthief.py @@ -1,14 +1,15 @@ """ Generate a colorscheme using ColorThief. """ +import logging import sys try: from colorthief import ColorThief except ImportError: - print("error: ColorThief wasn't found on your system.", - "Try another backend. (wal --backend)") + logging.error("ColorThief wasn't found on your system.") + logging.error("Try another backend. (wal --backend)") sys.exit(1) from .. import util @@ -25,13 +26,12 @@ def gen_colors(img): break elif i == 19: - print("colors: ColorThief couldn't generate a suitable palette", - "for the image. Exiting...") + logging.error("ColorThief couldn't generate a suitable palette.") sys.exit(1) else: - print("colors: ColorThief couldn't create a suitable palette, " - "trying a larger palette size", 8 + i) + logging.warning("ColorThief couldn't generate a palette.") + logging.warning("Trying a larger palette size %s", 8 + i) return [util.rgb_to_hex(color) for color in raw_colors] diff --git a/pywal/backends/colorz.py b/pywal/backends/colorz.py index 0c5148d..490f585 100644 --- a/pywal/backends/colorz.py +++ b/pywal/backends/colorz.py @@ -1,6 +1,7 @@ """ Generate a colorscheme using Colorz. """ +import logging import shutil import subprocess import sys @@ -28,8 +29,8 @@ def adjust(cols, light): def get(img, light=False): """Get colorscheme.""" if not shutil.which("colorz"): - print("error: Colorz wasn't found on your system.", - "Try another backend. (wal --backend)") + logging.error("Colorz wasn't found on your system.") + logging.error("Try another backend. (wal --backend)") sys.exit(1) cols = [col.decode('UTF-8').split()[0] for col in gen_colors(img)] diff --git a/pywal/backends/haishoku.py b/pywal/backends/haishoku.py index 413c9d7..00025b2 100644 --- a/pywal/backends/haishoku.py +++ b/pywal/backends/haishoku.py @@ -1,14 +1,15 @@ """ Generate a colorscheme using Haishoku. """ +import logging import sys try: from haishoku.haishoku import Haishoku except ImportError: - print("error: Haishoku wasn't found on your system.", - "Try another backend. (wal --backend)") + logging.error("Haishoku wasn't found on your system.") + logging.error("Try another backend. (wal --backend)") sys.exit(1) from .. import colors diff --git a/pywal/backends/schemer2.py b/pywal/backends/schemer2.py index 87873ec..adae650 100644 --- a/pywal/backends/schemer2.py +++ b/pywal/backends/schemer2.py @@ -1,6 +1,7 @@ """ Generate a colorscheme using Schemer2. """ +import logging import shutil import subprocess import sys @@ -26,8 +27,8 @@ def adjust(cols, light): def get(img, light=False): """Get colorscheme.""" if not shutil.which("schemer2"): - print("error: Schemer2 wasn't found on your system.", - "Try another backend. (wal --backend)") + logging.error("Schemer2 wasn't found on your system.") + logging.error("Try another backend. (wal --backend)") sys.exit(1) cols = [col.decode('UTF-8') for col in gen_colors(img)] diff --git a/pywal/backends/wal.py b/pywal/backends/wal.py index 678c66c..cd13092 100644 --- a/pywal/backends/wal.py +++ b/pywal/backends/wal.py @@ -1,6 +1,7 @@ """ Generate a colorscheme using imagemagick. """ +import logging import re import shutil import subprocess @@ -26,8 +27,8 @@ def has_im(): elif shutil.which("convert"): return ["convert"] - print("error: ImageMagick wasn't found on your system.", - "Try another backend. (wal --backend)") + logging.error("Imagemagick wasn't found on your system.") + logging.error("Try another backend. (wal --backend)") sys.exit(1) @@ -43,13 +44,12 @@ def gen_colors(img): break elif i == 19: - print("colors: Imagemagick couldn't generate a suitable palette", - "for the image. Exiting...") + logging.error("Imagemagick couldn't generate a suitable palette.") sys.exit(1) else: - print("colors: Imagemagick couldn't generate a suitable palette, " - "trying a larger palette size", 16 + i) + logging.warning("Imagemagick couldn't generate a palette.") + logging.warning("Trying a larger palette size %s", 16 + i) return [re.search("#.{6}", str(col)).group(0) for col in raw_colors[1:]] diff --git a/pywal/colors.py b/pywal/colors.py index 5472ef5..c0a0f1f 100644 --- a/pywal/colors.py +++ b/pywal/colors.py @@ -1,6 +1,7 @@ """ Generate a palette using various backends. """ +import logging import os import random import re @@ -90,17 +91,17 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR): if os.path.isfile(cache_file): colors = theme.file(cache_file) util.Color.alpha_num = colors["alpha"] - print("colors: Found cached colorscheme.") + logging.info("Found cached colorscheme.") else: - print("wal: Generating a colorscheme...") + logging.info("Generating a colorscheme...") if backend == "random": backends = list_backends() random.shuffle(backends) backend = backends[0] - print("wal: Using", backend, "backend.") + logging.info("Using %s backend.", backend) # Dynamically import the backend we want to use. # This keeps the dependencies "optional". @@ -113,7 +114,7 @@ def get(img, light=False, backend="wal", cache_dir=CACHE_DIR): colors = colors_to_dict(getattr(backend, "get")(img, light), img) util.save_file_json(colors, cache_file) - print("wal: Generation complete.") + logging.info("Generation complete.") return colors diff --git a/pywal/export.py b/pywal/export.py index 120150d..ed7a846 100644 --- a/pywal/export.py +++ b/pywal/export.py @@ -1,6 +1,7 @@ """ Export colors in various formats. """ +import logging import os from .settings import CACHE_DIR, MODULE_DIR, CONF_DIR @@ -65,8 +66,8 @@ def every(colors, output_dir=CACHE_DIR): if file.name != '.DS_Store': template(colors, file.path, join(output_dir, file.name)) - print("export: Exported all files.") - print("export: Exported all user files.") + logging.info("Exported all files.") + logging.info("Exported all user files.") def color(colors, export_type, output_file=None): @@ -79,6 +80,6 @@ def color(colors, export_type, output_file=None): if os.path.isfile(template_file): template(all_colors, template_file, output_file) - print("export: Exported %s." % export_type) + logging.info("Exported %s.", export_type) else: - print("warning: template '%s' doesn't exist." % export_type) + logging.warning("Template '%s' doesn't exist.", export_type) diff --git a/pywal/image.py b/pywal/image.py index d62cc26..f30a82d 100644 --- a/pywal/image.py +++ b/pywal/image.py @@ -1,6 +1,7 @@ """ Get the image file. """ +import logging import os import random import sys @@ -24,7 +25,7 @@ def get_random_image(img_dir): images.remove(current_wall) elif not images: - print("error: No images found in directory.") + logging.error("No images found in directory.") sys.exit(1) random.shuffle(images) @@ -41,7 +42,7 @@ def get(img, cache_dir=CACHE_DIR): wal_img = get_random_image(img) else: - print("error: No valid image file found.") + logging.error("No valid image file found.") sys.exit(1) wal_img = os.path.abspath(wal_img) @@ -49,5 +50,5 @@ def get(img, cache_dir=CACHE_DIR): # Cache the image file path. util.save_file(wal_img, os.path.join(cache_dir, "wal")) - print("image: Using image", wal_img) + logging.info("Using image %s.", wal_img) return wal_img diff --git a/pywal/reload.py b/pywal/reload.py index e815d64..7869d0b 100644 --- a/pywal/reload.py +++ b/pywal/reload.py @@ -1,6 +1,7 @@ """ Reload programs. """ +import logging import os import shutil import subprocess @@ -32,17 +33,17 @@ def oomox(gen_theme): """Call oomox to generate a theme.""" if gen_theme: if not shutil.which("oomox-cli"): - print("gtk: oomox not found, skipping...") + logging.warning("Oomox not found, skipping...") return oomox_file = os.path.join(CACHE_DIR, "colors-oomox") - print("reload: Waiting for oomox...") + logging.info("Waiting for oomox...") subprocess.run(["oomox-cli", "-o", "wal", oomox_file], stdout=subprocess.DEVNULL) else: - print("gtk: Use -g to generate an oomox theme.") + logging.info("Use -g to generate an oomox theme.") def gtk(): @@ -55,7 +56,7 @@ def gtk(): util.disown(["python2", gtk_reload]) else: - print("warning: GTK2 reload support requires Python 2.") + logging.warning("GTK2 reload support requires Python 2.") def i3(): @@ -93,5 +94,5 @@ def env(xrdb_file=None, tty_reload=True): i3() sway() polybar() - print("reload: Reloaded environment.") + logging.info("Reloaded environment.") tty(tty_reload) diff --git a/pywal/scripts/gtk_reload.py b/pywal/scripts/gtk_reload.py index ffb267d..e4c2bdf 100644 --- a/pywal/scripts/gtk_reload.py +++ b/pywal/scripts/gtk_reload.py @@ -10,7 +10,7 @@ Original source: https://crunchbang.org/forums/viewtopic.php?id=39646 try: import gtk except ImportError: - print("error: GTK reload requires PyGTK.") + print("[ERROR] gtk_reload: GTK reload requires PyGTK.") exit(1) diff --git a/pywal/sequences.py b/pywal/sequences.py index 0376342..0a6a99e 100644 --- a/pywal/sequences.py +++ b/pywal/sequences.py @@ -2,6 +2,7 @@ Send sequences to all open terminals. """ import glob +import logging import os from .settings import CACHE_DIR, OS @@ -89,4 +90,4 @@ def send(colors, cache_dir=CACHE_DIR, to_send=True): util.save_file(sequences, term) util.save_file(sequences, os.path.join(cache_dir, "sequences")) - print("colors: Set terminal colors.") + logging.info("Set terminal colors.") diff --git a/pywal/theme.py b/pywal/theme.py index 6439d40..96f86b9 100644 --- a/pywal/theme.py +++ b/pywal/theme.py @@ -1,6 +1,7 @@ """ Theme file handling. """ +import logging import os import random import sys @@ -72,5 +73,5 @@ def file(input_file): return parse_theme(theme_file) else: - print("No colorscheme file found, exiting...") + logging.error("No colorscheme file found, exiting...") sys.exit(1) diff --git a/pywal/util.py b/pywal/util.py index 65d4557..0c678c9 100644 --- a/pywal/util.py +++ b/pywal/util.py @@ -3,6 +3,7 @@ Misc helper functions. """ import colorsys import json +import logging import os import subprocess @@ -75,7 +76,7 @@ def save_file(data, export_file): with open(export_file, "w") as file: file.write(data) except PermissionError: - print("warning: Couldn't write to %s." % export_file) + logging.warning("Couldn't write to %s.", export_file) def save_file_json(data, export_file): @@ -91,6 +92,17 @@ def create_dir(directory): os.makedirs(directory, exist_ok=True) +def setup_logging(): + """Logging config.""" + logging.basicConfig(format=("[%(levelname)s\033[0m] " + "\033[1;31m%(module)s\033[0m: " + "%(message)s"), + level=logging.INFO) + logging.addLevelName(logging.ERROR, '\033[1;31mE') + logging.addLevelName(logging.INFO, '\033[1;32mI') + logging.addLevelName(logging.WARNING, '\033[1;33mW') + + def hex_to_rgb(color): """Convert a hex color to rgb.""" return tuple(bytes.fromhex(color.strip("#"))) diff --git a/pywal/wallpaper.py b/pywal/wallpaper.py index e4ebb59..1dd25f7 100644 --- a/pywal/wallpaper.py +++ b/pywal/wallpaper.py @@ -1,5 +1,6 @@ """Set the wallpaper.""" import ctypes +import logging import os import shutil import subprocess @@ -61,7 +62,7 @@ def set_wm_wallpaper(img): util.disown(["display", "-backdrop", "-window", "root", img]) else: - print("error: No wallpaper setter found.") + logging.error("No wallpaper setter found.") return @@ -135,7 +136,7 @@ def change(img): else: set_desktop_wallpaper(desktop, img) - print("wallpaper: Set the new wallpaper.") + logging.info("Set the new wallpaper.") def get(cache_dir=CACHE_DIR):