Add headless friendly logging. Close #6

This commit is contained in:
Gabriel Augendre 2019-12-30 20:23:42 +01:00
parent 5063f2717e
commit 8dd39179f7
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
2 changed files with 61 additions and 12 deletions

View file

@ -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)

View file

@ -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)