Add type hints to functions and move config class to utils

This commit is contained in:
Gabriel Augendre 2019-12-30 20:29:27 +01:00
parent 8dd39179f7
commit 97f99cffce
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
4 changed files with 25 additions and 26 deletions

View file

@ -5,7 +5,7 @@ import html2text
import pendulum import pendulum
from mastodon import Mastodon from mastodon import Mastodon
from cleantoots.utils import _config_has_sections from cleantoots.utils import _config_has_sections, CleanTootsConfig
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -23,7 +23,7 @@ CONTENT_PREVIEW = 78
"--headless", help="Use to make output more logging friendly.", is_flag=True "--headless", help="Use to make output more logging friendly.", is_flag=True
) )
@click.pass_obj @click.pass_obj
def clean(config, delete, headless): def clean(config: CleanTootsConfig, delete: bool, headless: bool):
""" """
Delete Toots based on rules in config file. Delete Toots based on rules in config file.

View file

@ -9,6 +9,7 @@ from cleantoots.utils import (
_open_url, _open_url,
_get_default_config, _get_default_config,
_is_tty, _is_tty,
CleanTootsConfig,
) )
@ -20,7 +21,7 @@ def config_command():
@config_command.command() @config_command.command()
@click.pass_obj @click.pass_obj
def setup(config): def setup(config: CleanTootsConfig):
"""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.main_file): if os.path.isfile(config.main_file):
@ -51,7 +52,7 @@ def setup(config):
@config_command.command(name="list") @config_command.command(name="list")
@click.pass_obj @click.pass_obj
def list_(config): def list_(config: CleanTootsConfig):
"""Display parsed config.""" """Display parsed config."""
if not _config_has_sections(config): if not _config_has_sections(config):
return return
@ -65,10 +66,10 @@ def list_(config):
@config_command.command() @config_command.command()
@click.pass_obj @click.pass_obj
def edit(config): def edit(config: CleanTootsConfig):
"""Edit config file.""" """Edit config file."""
if not _config_has_sections(config): if not _config_has_sections(config):
return click.pause()
if sys.stdout.isatty() and sys.stdin.isatty(): if sys.stdout.isatty() and sys.stdin.isatty():
click.edit(filename=config.main_file) click.edit(filename=config.main_file)
else: else:
@ -83,7 +84,7 @@ def edit(config):
is_flag=True, is_flag=True,
) )
@click.pass_obj @click.pass_obj
def login(config, only_missing): def login(config: CleanTootsConfig, only_missing: bool):
"""Fetch credentials for each app described in config file.""" """Fetch credentials for each app described in config file."""
if not _config_has_sections(config): if not _config_has_sections(config):
return return
@ -116,7 +117,7 @@ def login(config, only_missing):
"You will need to run `cleantoots config login` to re-authenticate." "You will need to run `cleantoots config login` to re-authenticate."
) )
@click.pass_obj @click.pass_obj
def clear_credentials(config): def clear_credentials(config: CleanTootsConfig):
"""Delete all credential files described in config file.""" """Delete all credential files described in config file."""
if not _config_has_sections(config): if not _config_has_sections(config):
return return

View file

@ -1,35 +1,18 @@
import configparser
import logging.config import logging.config
import os import os
import pathlib
import sys import sys
import click import click
from cleantoots.commands import clean as clean_commands, config as config_commands from cleantoots.commands import clean as clean_commands, config as config_commands
from cleantoots.utils import CleanTootsConfig
HOME = pathlib.Path.home()
DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots") DEFAULT_CONFIG_DIR = click.get_app_dir("cleantoots")
DEFAULT_CONFIG_FILENAME = "config.ini" DEFAULT_CONFIG_FILENAME = "config.ini"
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)
def isfile(self, filename):
return os.path.isfile(self.file(filename))
@click.group(context_settings=CONTEXT_SETTINGS) @click.group(context_settings=CONTEXT_SETTINGS)
@click.option( @click.option(
"-d", "-d",

View file

@ -1,4 +1,5 @@
import configparser import configparser
import os
import sys import sys
import click import click
@ -48,3 +49,17 @@ def _get_default_config():
"protected_toots": "1234\n5678", "protected_toots": "1234\n5678",
} }
return default_config return default_config
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)
def isfile(self, filename):
return os.path.isfile(self.file(filename))