Use context to read and store config

This commit is contained in:
Gabriel Augendre 2019-12-27 23:04:43 +01:00
parent 74bc5fffbf
commit c69638f714
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -1,5 +1,4 @@
import configparser import configparser
import functools
import os import os
import pathlib import pathlib
import subprocess import subprocess
@ -21,7 +20,8 @@ EDITOR = os.getenv("EDITOR", "vim")
@click.group() @click.group()
def cli(): @click.pass_context
def cli(ctx):
""" """
Provide an easy interface for deleting old toots. Provide an easy interface for deleting old toots.
@ -33,16 +33,8 @@ def cli():
3. run `clean --delete` 3. run `clean --delete`
""" """
pass ctx.obj = configparser.ConfigParser()
ctx.obj.read(CONFIG_FILE)
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
@cli.command() @cli.command()
@ -83,7 +75,7 @@ def setup_config():
@cli.command() @cli.command()
@load_config @click.pass_obj
def config(config): def config(config):
"""Display parsed config.""" """Display parsed config."""
for section_name in config.sections(): for section_name in config.sections():
@ -95,7 +87,7 @@ def config(config):
@cli.command() @cli.command()
@load_config @click.pass_obj
def login(config): def login(config):
"""Fetch credentials for each app described in config file.""" """Fetch credentials for each app described in config file."""
for section in config.sections(): for section in config.sections():
@ -126,7 +118,7 @@ def login(config):
"Without this flags, toots will only be listed.", "Without this flags, toots will only be listed.",
is_flag=True, is_flag=True,
) )
@load_config @click.pass_obj
def clean(delete, config): def clean(delete, config):
""" """
Delete Toots based on rules in config file. Delete Toots based on rules in config file.