Skip to content

Commit

Permalink
add support for configuration file (WIP)
Browse files Browse the repository at this point in the history
Current status:

- break --verbose

- override doesn't work properly, e.g., --case-sensitive in config should be
  overridable with --ignore-case on the CLI, but is not (because we do not
  distinguish between the origin and, when given both on the CLI,
  --case-sensitive wins)

Related #8
  • Loading branch information
zacchiro committed Jul 9, 2024
1 parent 63eba32 commit 2927d56
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ classifiers = [
dependencies = [
"beancount>=2.3.5",
"click>=8.1",
"pyxdg>=0.28",
]
# version = "0.1.0"
dynamic = [ "version" ]
Expand Down
2 changes: 1 addition & 1 deletion src/beangrep/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
__license__ = "GPL-2.0-or-later"

from .beangrep import * # noqa:F401,F403
from .cli import cli # noqa:F401
from .cli import cli, _cli # noqa:F401
48 changes: 47 additions & 1 deletion src/beangrep/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import logging
import shutil
import sys
from pathlib import Path
from tempfile import NamedTemporaryFile

import beancount.loader # type: ignore
import click
from beancount.parser import printer # type: ignore
from xdg.BaseDirectory import xdg_config_home

from .beangrep import (
INTERNALS_META,
Expand All @@ -22,6 +24,38 @@
filter_entries,
)

CONFFILE_PATH = Path(xdg_config_home) / "beangreprc"


def load_conffile(path: Path) -> list[str]:
"""Load a configuration file and return its content, as a list of strings (usually
CLI flags starting with "-").
"""
logging.debug("Reading configuration flags from file: %s ...", path)
with open(path) as f:
flags = []
line_no = 0
for line in f.readlines():
line_no += 1
line = line.strip()
if not line: # empty line, skip
continue
elif line.startswith("#"): # comment, skip
continue
elif line.startswith("-"): # flag, return it
flags.append(line)
else: # invalid line, warn and skip it
logging.warning(
'Line %d in configuration file %s is invalid (content: "%s"), '
"ignoring it.",
line_no,
path,
line,
)

return flags


@click.command(
help="""Search for entries matching given criteria in Beancount journals. Pretty
Expand Down Expand Up @@ -234,7 +268,7 @@
)
@click.version_option(None, "--version", "-V")
@click.pass_context
def cli(
def _cli(
ctx,
args,
accounts,
Expand All @@ -256,6 +290,7 @@ def cli(
invert_match,
verbose,
):
"""CLI entry point, *after* processing options from configuration file."""
match verbose:
case 0:
log_level = logging.WARNING
Expand Down Expand Up @@ -350,5 +385,16 @@ def cli(
ctx.exit(exit_status)


def cli(*args, **kwargs):
"""CLI entry point, *before* processing options from configuration file."""
if CONFFILE_PATH.is_file():
conf_flags = load_conffile(CONFFILE_PATH)
logging.info("Using configuration flags: %s", conf_flags)
if conf_flags: # patch sys.argv with CLI flags from configuration file
sys.argv = [sys.argv[0]] + conf_flags + sys.argv[1:]

_cli(*args, **kwargs)


if __name__ == "__main__":
cli()
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from click.testing import CliRunner
from test_beangrep import SAMPLE_LEDGER, SAMPLE_LEDGER_SMALL

from beangrep import cli
from beangrep import _cli as cli


def test_basic():
Expand Down

0 comments on commit 2927d56

Please sign in to comment.