From 8dd39179f7ba890ecfc1677803de2a96b5181895 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 30 Dec 2019 20:23:42 +0100 Subject: [PATCH] Add headless friendly logging. Close #6 --- cleantoots/commands/clean.py | 40 +++++++++++++++++++++++++----------- cleantoots/main.py | 33 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/cleantoots/commands/clean.py b/cleantoots/commands/clean.py index f4dabba..df86109 100644 --- a/cleantoots/commands/clean.py +++ b/cleantoots/commands/clean.py @@ -1,3 +1,5 @@ +import logging.handlers + import click import html2text import pendulum @@ -5,6 +7,8 @@ from mastodon import Mastodon from cleantoots.utils import _config_has_sections +logger = logging.getLogger(__name__) + CONTENT_PREVIEW = 78 @@ -15,8 +19,11 @@ CONTENT_PREVIEW = 78 "Without this flags, toots will only be listed.", is_flag=True, ) +@click.option( + "--headless", help="Use to make output more logging friendly.", is_flag=True +) @click.pass_obj -def clean(config, delete): +def clean(config, delete, headless): """ Delete Toots based on rules in config file. @@ -64,33 +71,42 @@ def clean(config, delete): if not delete: if not would_delete: - click.secho("No toot would be deleted given the rules.", fg="blue") + log("No toot would be deleted given the rules.", headless, fg="blue") return - click.secho( + log( "Would delete {count} toots/boost:".format(count=len(would_delete)), + headless, fg="blue", ) for toot in would_delete: - message = format_toot(toot, prefix="=== ", separator=" ", suffix=" ===") - click.echo(message) + message = format_toot(toot) + log(message, headless, bold=True) content = h.handle(toot["content"]).replace("\n", " ").strip() if len(content) > CONTENT_PREVIEW: content = content[: CONTENT_PREVIEW - 3] + "..." else: content = content[:CONTENT_PREVIEW] - click.echo(content) - click.echo() + log(content, headless) + log("", headless) else: - click.echo("Deleting toots...") + log("Deleting toots...", headless) with click.progressbar(would_delete) as bar: for toot in bar: mastodon.status_delete(toot) - click.secho("Deleted {}".format(format_toot(toot)), fg="green") + log("Deleted {}".format(format_toot(toot)), headless, fg="green") -def format_toot(toot, prefix="", separator="\t", suffix=""): +def format_toot(toot): if toot.get("reblog"): - message = f"{prefix}boost of{separator}{toot['reblog']['url']}{suffix}" + message = f"boost of toot {toot['reblog']['url']}" else: - message = f"{prefix}original toot{separator}{toot['url']}{suffix}" + message = f"original toot {toot['url']}" return message + + +def log(message, headless, level=logging.INFO, *args, **kwargs): + if headless: + if message and message.strip(): + logger.log(level, message) + else: + click.secho(message, *args, **kwargs) diff --git a/cleantoots/main.py b/cleantoots/main.py index e2d2fa7..1765bf1 100644 --- a/cleantoots/main.py +++ b/cleantoots/main.py @@ -1,6 +1,8 @@ import configparser +import logging.config import os import pathlib +import sys import click @@ -57,6 +59,37 @@ def cli(ctx, config_dir, config_file): 3. run `clean --delete` """ ctx.obj = CleanTootsConfig(config_dir, config_file) + log_file = os.path.join(click.get_app_dir("cleantoots"), "cleantoots.log") + logging.config.dictConfig( + { + "version": 1, + "formatters": { + "precise": { + "format": "{asctime} | {name} | {levelname} | {message}", + "style": "{", + } + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "stream": sys.stdout, + "level": "INFO", + "formatter": "precise", + }, + "file": { + "class": "logging.handlers.RotatingFileHandler", + "filename": log_file, + "maxBytes": 1024 * 100, + "backupCount": 3, + "level": "INFO", + "formatter": "precise", + }, + }, + "loggers": { + "cleantoots": {"level": "INFO", "handlers": ["console", "file"]} + }, + } + ) cli.add_command(config_commands.config_command)