From c69638f71406c15707440d8a6f6af5db2bc61158 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 27 Dec 2019 23:04:43 +0100 Subject: [PATCH] Use context to read and store config --- cleantoots/main.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/cleantoots/main.py b/cleantoots/main.py index 9ec68b4..4f88829 100644 --- a/cleantoots/main.py +++ b/cleantoots/main.py @@ -1,5 +1,4 @@ import configparser -import functools import os import pathlib import subprocess @@ -21,7 +20,8 @@ EDITOR = os.getenv("EDITOR", "vim") @click.group() -def cli(): +@click.pass_context +def cli(ctx): """ Provide an easy interface for deleting old toots. @@ -33,16 +33,8 @@ def cli(): 3. run `clean --delete` """ - pass - - -def load_config(function): - config = configparser.ConfigParser() - config.read(CONFIG_FILE) - func = functools.partial(function, config=config) - func.__name__ = function.__name__ - func.__doc__ = function.__doc__ - return func + ctx.obj = configparser.ConfigParser() + ctx.obj.read(CONFIG_FILE) @cli.command() @@ -83,7 +75,7 @@ def setup_config(): @cli.command() -@load_config +@click.pass_obj def config(config): """Display parsed config.""" for section_name in config.sections(): @@ -95,7 +87,7 @@ def config(config): @cli.command() -@load_config +@click.pass_obj def login(config): """Fetch credentials for each app described in config file.""" for section in config.sections(): @@ -126,7 +118,7 @@ def login(config): "Without this flags, toots will only be listed.", is_flag=True, ) -@load_config +@click.pass_obj def clean(delete, config): """ Delete Toots based on rules in config file.