Add headless friendly logging. Close #6
This commit is contained in:
parent
5063f2717e
commit
8dd39179f7
2 changed files with 61 additions and 12 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import logging.handlers
|
||||||
|
|
||||||
import click
|
import click
|
||||||
import html2text
|
import html2text
|
||||||
import pendulum
|
import pendulum
|
||||||
|
@ -5,6 +7,8 @@ from mastodon import Mastodon
|
||||||
|
|
||||||
from cleantoots.utils import _config_has_sections
|
from cleantoots.utils import _config_has_sections
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONTENT_PREVIEW = 78
|
CONTENT_PREVIEW = 78
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,8 +19,11 @@ CONTENT_PREVIEW = 78
|
||||||
"Without this flags, toots will only be listed.",
|
"Without this flags, toots will only be listed.",
|
||||||
is_flag=True,
|
is_flag=True,
|
||||||
)
|
)
|
||||||
|
@click.option(
|
||||||
|
"--headless", help="Use to make output more logging friendly.", is_flag=True
|
||||||
|
)
|
||||||
@click.pass_obj
|
@click.pass_obj
|
||||||
def clean(config, delete):
|
def clean(config, delete, headless):
|
||||||
"""
|
"""
|
||||||
Delete Toots based on rules in config file.
|
Delete Toots based on rules in config file.
|
||||||
|
|
||||||
|
@ -64,33 +71,42 @@ def clean(config, delete):
|
||||||
|
|
||||||
if not delete:
|
if not delete:
|
||||||
if not would_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
|
return
|
||||||
click.secho(
|
log(
|
||||||
"Would delete {count} toots/boost:".format(count=len(would_delete)),
|
"Would delete {count} toots/boost:".format(count=len(would_delete)),
|
||||||
|
headless,
|
||||||
fg="blue",
|
fg="blue",
|
||||||
)
|
)
|
||||||
for toot in would_delete:
|
for toot in would_delete:
|
||||||
message = format_toot(toot, prefix="=== ", separator=" ", suffix=" ===")
|
message = format_toot(toot)
|
||||||
click.echo(message)
|
log(message, headless, bold=True)
|
||||||
content = h.handle(toot["content"]).replace("\n", " ").strip()
|
content = h.handle(toot["content"]).replace("\n", " ").strip()
|
||||||
if len(content) > CONTENT_PREVIEW:
|
if len(content) > CONTENT_PREVIEW:
|
||||||
content = content[: CONTENT_PREVIEW - 3] + "..."
|
content = content[: CONTENT_PREVIEW - 3] + "..."
|
||||||
else:
|
else:
|
||||||
content = content[:CONTENT_PREVIEW]
|
content = content[:CONTENT_PREVIEW]
|
||||||
click.echo(content)
|
log(content, headless)
|
||||||
click.echo()
|
log("", headless)
|
||||||
else:
|
else:
|
||||||
click.echo("Deleting toots...")
|
log("Deleting toots...", headless)
|
||||||
with click.progressbar(would_delete) as bar:
|
with click.progressbar(would_delete) as bar:
|
||||||
for toot in bar:
|
for toot in bar:
|
||||||
mastodon.status_delete(toot)
|
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"):
|
if toot.get("reblog"):
|
||||||
message = f"{prefix}boost of{separator}{toot['reblog']['url']}{suffix}"
|
message = f"boost of toot {toot['reblog']['url']}"
|
||||||
else:
|
else:
|
||||||
message = f"{prefix}original toot{separator}{toot['url']}{suffix}"
|
message = f"original toot {toot['url']}"
|
||||||
return message
|
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)
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import configparser
|
import configparser
|
||||||
|
import logging.config
|
||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
import sys
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
|
@ -57,6 +59,37 @@ def cli(ctx, config_dir, config_file):
|
||||||
3. run `clean --delete`
|
3. run `clean --delete`
|
||||||
"""
|
"""
|
||||||
ctx.obj = CleanTootsConfig(config_dir, config_file)
|
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)
|
cli.add_command(config_commands.config_command)
|
||||||
|
|
Loading…
Reference in a new issue