Skip to content

Commit

Permalink
Postpone evaluation of get_default_config_path(). #355
Browse files Browse the repository at this point in the history
Previously, get_default_config_path() was called upon import, which makes it
hard to override the return value in tests. With this change, the config path
is determined when cli() is called and not sooner.
  • Loading branch information
maksverver committed Oct 10, 2024
1 parent ae5bc32 commit 4aaf2f2
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/reader/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,27 @@ def wrapper(*args, **kwargs):

def config_option(*args, **kwargs):
def callback(ctx, param, value):
# TODO: the default file is allowed to not exist, a user specified file must exist
config_path = value if value is not None else get_default_config_path()
try:
with open(value) as file:
with open(config_path) as file:
config = make_reader_config(yaml.safe_load(file))
except FileNotFoundError as e:
if value != param.default:
raise click.BadParameter(str(e), ctx=ctx, param=param) from None
assert value is None
config = make_reader_config({})

ctx.default_map = config['cli'].get('defaults', {})

ctx.obj = config
return config

def inner(fn):
return click.option(
*args,
type=click.Path(dir_okay=False),
callback=callback,
is_eager=True,
expose_value=False,
**kwargs,
)(fn)

return inner
return click.option(
*args,
type=click.Path(exists=True, dir_okay=False),
callback=callback,
is_eager=True,
expose_value=False,
**kwargs,
)


def pass_reader(fn):
Expand All @@ -166,7 +162,6 @@ def wrapper(*args, **kwargs):
'--db',
type=click.Path(dir_okay=False),
envvar=reader._DB_ENVVAR,
show_default=True,
help=f"Path to the reader database. [default: {get_default_db_path()}]",
)
@click.option(
Expand All @@ -184,9 +179,7 @@ def wrapper(*args, **kwargs):
@config_option(
'--config',
envvar=reader._CONFIG_ENVVAR,
help="Path to the reader config.",
default=get_default_config_path(),
show_default=True,
help=f"Path to the reader config. [default: {get_default_config_path()}]",
)
@click.option(
'--feed-root',
Expand Down

0 comments on commit 4aaf2f2

Please sign in to comment.