Add params for config folder and file name. Extract config to custom class.

This commit is contained in:
Gabriel Augendre 2019-12-28 00:38:18 +01:00
parent b46d77342f
commit 368b28a1de
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -1,29 +1,50 @@
import configparser import configparser
import os import os
import pathlib import pathlib
import subprocess import sys
import webbrowser
import click import click
import pendulum import pendulum
from click import Abort
from mastodon import Mastodon from mastodon import Mastodon
def config_file(filename):
return os.path.join(CONFIG_DIR, filename)
HOME = pathlib.Path.home() HOME = pathlib.Path.home()
CONFIG_DIR = click.get_app_dir("cleantoots") DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots")
CONFIG_FILE = config_file("config.ini") DEFAULT_CONFIG_FILENAME = "config.ini"
EDITOR = os.getenv("EDITOR", "vim") EDITOR = os.getenv("EDITOR", "vim")
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"]) CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
class CleanTootsConfig(configparser.ConfigParser):
def __init__(self, config_dir, config_file_name, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dir = config_dir
self.main_file = os.path.join(config_dir, config_file_name)
self.read(self.main_file)
def file(self, filename):
return os.path.join(self.dir, filename)
@click.group(context_settings=CONTEXT_SETTINGS) @click.group(context_settings=CONTEXT_SETTINGS)
@click.option(
"-d",
"--config-dir",
help="Custom configuration directory.",
default=DEFAULT_CONFIG_DIR,
show_default=True,
)
@click.option(
"-c",
"--config-file",
help="Custom configuration file name. "
"Must only contain the filename, not the whole path.",
default=DEFAULT_CONFIG_FILENAME,
show_default=True,
)
@click.pass_context @click.pass_context
def cli(ctx): def cli(ctx, config_dir, config_file):
""" """
Provide an easy interface for deleting old toots. Provide an easy interface for deleting old toots.
@ -33,34 +54,36 @@ def cli(ctx):
2. run `login` 2. run `login`
3. run `clean --delete` 3. run `clean --delete`
""" """
ctx.obj = configparser.ConfigParser() ctx.obj = CleanTootsConfig(config_dir, config_file)
ctx.obj.read(CONFIG_FILE)
@cli.command() @cli.command()
def setup_config(): @click.pass_obj
def setup_config(config):
"""Initial setup for configuration directories and files.""" """Initial setup for configuration directories and files."""
os.makedirs(CONFIG_DIR, exist_ok=True) os.makedirs(config.dir, exist_ok=True)
if os.path.isfile(CONFIG_FILE): if os.path.isfile(config.main_file):
click.secho("{} found. Not touching anything.".format(CONFIG_FILE), fg="red") click.secho(
return "{} found. Not touching anything.".format(config.main_file), fg="red"
)
raise Abort()
config = configparser.ConfigParser() default_config = configparser.ConfigParser()
config["DEFAULT"] = { default_config["DEFAULT"] = {
"boost_limit": 5, "boost_limit": 5,
"favorite_limit": 5, "favorite_limit": 5,
"days_count": 30, "days_count": 30,
"timezone": "Europe/Paris", "timezone": "Europe/Paris",
} }
config["Mastodon.social"] = { default_config["Mastodon.social"] = {
"api_base_url": "https://mastodon.social", "api_base_url": "https://mastodon.social",
"app_secret_file": "mastodon_social_app.secret", "app_secret_file": "mastodon_social_app.secret",
"user_secret_file": "mastodon_social_user.secret", "user_secret_file": "mastodon_social_user.secret",
"protected_toots": "1234\n5678", "protected_toots": "1234\n5678",
} }
with open(CONFIG_FILE, "w") as _file: with open(config.main_file, "w") as _file:
config.write(_file) default_config.write(_file)
click.secho("{} written.".format(CONFIG_FILE), fg="green") click.secho("{} written.".format(config.main_file), fg="green")
click.echo() click.echo()
click.secho("Next steps", bold=True) click.secho("Next steps", bold=True)
click.echo( click.echo(
@ -68,10 +91,11 @@ def setup_config():
) )
click.echo("* The base URL of your Mastodon instance") click.echo("* The base URL of your Mastodon instance")
click.echo("* The toots you want to protect") click.echo("* The toots you want to protect")
click.echo() if sys.stdout.isatty() and sys.stdin.isatty():
click.secho("We're going to open the file for you now.") click.echo()
click.pause() click.secho("We're going to open the file for you now.")
subprocess.run([EDITOR, CONFIG_FILE]) click.pause()
click.edit(filename=config.main_file)
@cli.command() @cli.command()
@ -95,20 +119,27 @@ def login(config):
Mastodon.create_app( Mastodon.create_app(
"cleantoots", "cleantoots",
api_base_url=section.get("api_base_url"), api_base_url=section.get("api_base_url"),
to_file=config_file(section.get("app_secret_file")), to_file=config.file(section.get("app_secret_file")),
) )
mastodon = Mastodon(client_id=config_file(section.get("app_secret_file"))) mastodon = Mastodon(client_id=config.file(section.get("app_secret_file")))
click.echo( if sys.stdout.isatty() and sys.stdin.isatty():
"We will now open a browser for each account set in the config file." click.echo(
) "We will now open a browser for each account set in the config file."
click.echo( )
"You'll need to authenticate and then copy the code provided in the web " click.echo(
"page back into this terminal, upon prompt." "You'll need to authenticate and then copy the code provided in the web "
) "page back into this terminal, upon prompt."
click.pause() )
webbrowser.open(mastodon.auth_request_url()) click.pause()
click.launch(mastodon.auth_request_url())
else:
click.echo(
"Go to {}, authenticate and enter the code below.".format(
mastodon.auth_request_url()
)
)
code = click.prompt("Enter code for {}".format(section.get("api_base_url"))) code = click.prompt("Enter code for {}".format(section.get("api_base_url")))
mastodon.log_in(code=code, to_file=config_file(section.get("user_secret_file"))) mastodon.log_in(code=code, to_file=config.file(section.get("user_secret_file")))
@cli.command() @cli.command()
@ -127,7 +158,7 @@ def clean(delete, config):
""" """
for section in config.sections(): for section in config.sections():
section = config[section] section = config[section]
user_secret_file = config_file(section.get("user_secret_file")) user_secret_file = config.file(section.get("user_secret_file"))
mastodon = Mastodon(access_token=user_secret_file) mastodon = Mastodon(access_token=user_secret_file)
user = mastodon.me() user = mastodon.me()
page = mastodon.account_statuses(user["id"]) page = mastodon.account_statuses(user["id"])