From 97f99cffce38c6af73d5bbacca7d159d93e2c0e1 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Mon, 30 Dec 2019 20:29:27 +0100 Subject: [PATCH] Add type hints to functions and move config class to utils --- cleantoots/commands/clean.py | 4 ++-- cleantoots/commands/config.py | 13 +++++++------ cleantoots/main.py | 19 +------------------ cleantoots/utils.py | 15 +++++++++++++++ 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cleantoots/commands/clean.py b/cleantoots/commands/clean.py index df86109..56c3704 100644 --- a/cleantoots/commands/clean.py +++ b/cleantoots/commands/clean.py @@ -5,7 +5,7 @@ import html2text import pendulum from mastodon import Mastodon -from cleantoots.utils import _config_has_sections +from cleantoots.utils import _config_has_sections, CleanTootsConfig logger = logging.getLogger(__name__) @@ -23,7 +23,7 @@ CONTENT_PREVIEW = 78 "--headless", help="Use to make output more logging friendly.", is_flag=True ) @click.pass_obj -def clean(config, delete, headless): +def clean(config: CleanTootsConfig, delete: bool, headless: bool): """ Delete Toots based on rules in config file. diff --git a/cleantoots/commands/config.py b/cleantoots/commands/config.py index d4ad55d..9bb4cd1 100644 --- a/cleantoots/commands/config.py +++ b/cleantoots/commands/config.py @@ -9,6 +9,7 @@ from cleantoots.utils import ( _open_url, _get_default_config, _is_tty, + CleanTootsConfig, ) @@ -20,7 +21,7 @@ def config_command(): @config_command.command() @click.pass_obj -def setup(config): +def setup(config: CleanTootsConfig): """Initial setup for configuration directories and files.""" os.makedirs(config.dir, exist_ok=True) if os.path.isfile(config.main_file): @@ -51,7 +52,7 @@ def setup(config): @config_command.command(name="list") @click.pass_obj -def list_(config): +def list_(config: CleanTootsConfig): """Display parsed config.""" if not _config_has_sections(config): return @@ -65,10 +66,10 @@ def list_(config): @config_command.command() @click.pass_obj -def edit(config): +def edit(config: CleanTootsConfig): """Edit config file.""" if not _config_has_sections(config): - return + click.pause() if sys.stdout.isatty() and sys.stdin.isatty(): click.edit(filename=config.main_file) else: @@ -83,7 +84,7 @@ def edit(config): is_flag=True, ) @click.pass_obj -def login(config, only_missing): +def login(config: CleanTootsConfig, only_missing: bool): """Fetch credentials for each app described in config file.""" if not _config_has_sections(config): return @@ -116,7 +117,7 @@ def login(config, only_missing): "You will need to run `cleantoots config login` to re-authenticate." ) @click.pass_obj -def clear_credentials(config): +def clear_credentials(config: CleanTootsConfig): """Delete all credential files described in config file.""" if not _config_has_sections(config): return diff --git a/cleantoots/main.py b/cleantoots/main.py index 1765bf1..c564983 100644 --- a/cleantoots/main.py +++ b/cleantoots/main.py @@ -1,35 +1,18 @@ -import configparser import logging.config import os -import pathlib import sys import click 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_FILENAME = "config.ini" -EDITOR = os.getenv("EDITOR", "vim") 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.option( "-d", diff --git a/cleantoots/utils.py b/cleantoots/utils.py index f7cf214..c5f6ca8 100644 --- a/cleantoots/utils.py +++ b/cleantoots/utils.py @@ -1,4 +1,5 @@ import configparser +import os import sys import click @@ -48,3 +49,17 @@ def _get_default_config(): "protected_toots": "1234\n5678", } 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))