Skip to content

Commit

Permalink
Expanded docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Dec 24, 2024
1 parent 8605ff6 commit f47c313
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
11 changes: 7 additions & 4 deletions docs/pages/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ The tools mostly share the same configuration methods.
The following priority is given to options:

#. Command line arguments
#. Sections of config file ``tctools.toml``
#. Sections of general config file ``pyproject.toml``
#. Config file ``tctools.toml``
#. General config file ``pyproject.toml``
#. Extra config files (e.g. ``.editorconfig``)

In case options are available in multiple files, only the section with the highest priority is considered.
I.e. options are not merged down to the smallest level.
In case options are available in multiple files, only the one with the highest priority is considered.
I.e. options are not merged across levels: having entries in both ``tctools.toml`` and in ``pyproject.toml`` makes the ones in ``pyproject.toml`` be ignored.

The exception is for the :ref:`auto_formatter`, where the ``.editorconfig`` file(s) are still always considered.

Recommended
===========
Expand All @@ -36,3 +38,4 @@ Add options under a section:
Substitute ``tool`` for a specific tool and ``option`` and ``"value"`` for meaningful entries.

See pages about each tool for available options.
The options are named the same as the command line arguments, except with any dashes (``-``) replaced by underscores (``_``).
13 changes: 10 additions & 3 deletions src/tctools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def __init__(self, *args):
# Pre-create the arguments output and insert any configured values:
self.args = Namespace()

self.config_file: Optional[Path] = None

config = self.make_config()
if self.CONFIG_KEY:
config = config.get(self.CONFIG_KEY, {})
Expand All @@ -58,8 +60,13 @@ def __init__(self, *args):
# Now parse CLI options on top of file configurations:
parser.parse_args(args, self.args)

# Only after parsing all arguments we can establish a logger,
self.logger = self.get_logger()

if self.config_file:
# Use property for config file path so we can still log it now:
self.logger.debug(f"Loading from config: {self.config_file}")

@classmethod
def get_argument_parser(cls) -> ArgumentParser:
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
Expand Down Expand Up @@ -87,13 +94,13 @@ def set_arguments(cls, parser):
def make_config(self) -> Dict[str, Any]:
"""Get configuration from possible files."""
config = {}
config_file = self._find_files_upwards(
self.config_file = self._find_files_upwards(
Path.cwd(), ["tctools.toml", "pyproject.toml"]
)
if not config_file:
if not self.config_file:
return config

with open(config_file, "rb") as fh:
with open(self.config_file, "rb") as fh:
data = tomllib.load(fh)
if "tctools" in data:
config = data["tctools"]
Expand Down
23 changes: 22 additions & 1 deletion tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ def test_config_file(self, tmp_path, monkeypatch):
tool = MyTool()
assert tool.args.my_option == "xyz123"

# TODO: Add test for section priority
def test_config_file_priority(self, tmp_path, monkeypatch):
conf_dir = tmp_path / "project"
work_dir = conf_dir / "subdir1" / "subdir2"
work_dir.mkdir(parents=True)

conf_file2 = conf_dir / "pyproject.toml"
conf_file2.write_text(
"""[tctools.dummy]
my_option = "xyz123"
"""
)
conf_file = conf_dir / "tctools.toml"
conf_file.write_text(
"""[tctools.dummy]
my_option = "abc987"
"""
)

monkeypatch.chdir(work_dir)

tool = MyTool()
assert tool.args.my_option == "abc987"

0 comments on commit f47c313

Please sign in to comment.