From d7c3176c92a8c7a0cff7f8bfc70bb06a47d1e373 Mon Sep 17 00:00:00 2001 From: RobertoRoos Date: Fri, 17 Jan 2025 08:15:54 +0100 Subject: [PATCH] Fixed config loading, tested with real project --- docs/pages/config.rst | 4 ++-- src/tctools/common.py | 20 +++++++++++++------ src/tctools/format/format_class.py | 2 ++ src/tctools/git_info/git_info_class.py | 4 ++++ .../make_release/make_release_class.py | 4 ++++ tests/test_xml_sorter.py | 18 +++++++++++++++++ 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/docs/pages/config.rst b/docs/pages/config.rst index d3198f6..f8868e0 100644 --- a/docs/pages/config.rst +++ b/docs/pages/config.rst @@ -35,7 +35,7 @@ Add options under a section: option = "value" # ... -Substitute ``tool`` for a specific tool and ``option`` and ``"value"`` for meaningful entries. +Substitute ``tool`` for either ``format``, ``xml_sort``, ``git_info`` or ``make_release``, and replace ``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 (``_``). +The options are named the same as the command line arguments, except without any leading dashes and with any other dashes (``-``) replaced by underscores (``_``). diff --git a/src/tctools/common.py b/src/tctools/common.py index 2476e0f..8cfe264 100644 --- a/src/tctools/common.py +++ b/src/tctools/common.py @@ -42,23 +42,27 @@ def __init__(self, *args): action.dest for action in parser._actions if action.dest != "help" # noqa } - # 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, {}) + + # Change the default values of the parser to the config results: for key, value in config.items(): if key not in fields: raise ValueError( f"Config field `{key}` is not recognized as a valid option" ) - setattr(self.args, key, value) + parser.set_defaults(**{key: value}) + + for action in parser._actions: + if action.dest == key: + action.required = False + # Unfortunately this wouldn't disable required flags yet # Now parse CLI options on top of file configurations: - parser.parse_args(args, self.args) + self.args: Namespace = parser.parse_args(args) # Only after parsing all arguments we can establish a logger, self.logger = self.get_logger() @@ -212,7 +216,11 @@ def find_files(self) -> List[Path]: if not self.args.target: return files - for target in self.args.target: + targets = self.args.target + if isinstance(targets, str): + targets = [targets] + + for target in targets: path = Path(target).resolve() if path.is_file(): files.append(path) diff --git a/src/tctools/format/format_class.py b/src/tctools/format/format_class.py index b31314a..1dd2cb3 100644 --- a/src/tctools/format/format_class.py +++ b/src/tctools/format/format_class.py @@ -95,6 +95,8 @@ class Formatter(TcTool): FILTER_DEFAULT = ["*.TcPOU", "*.TcGVL", "*.TcDUT"] + CONFIG_KEY = "format" + _RULE_CLASSES: List[Type[FormattingRule]] = [] def __init__(self, *args): diff --git a/src/tctools/git_info/git_info_class.py b/src/tctools/git_info/git_info_class.py index 67b1f5a..aebed85 100644 --- a/src/tctools/git_info/git_info_class.py +++ b/src/tctools/git_info/git_info_class.py @@ -14,6 +14,10 @@ class GitInfo(Tool): collide with XML brackets in ``$key`` the dollar sign is a key for string constants. """ + LOGGER_NAME = "git_info" + + CONFIG_KEY = "git_info" + def __init__(self, *args): super().__init__(*args) diff --git a/src/tctools/make_release/make_release_class.py b/src/tctools/make_release/make_release_class.py index 32c8da4..81c37d5 100644 --- a/src/tctools/make_release/make_release_class.py +++ b/src/tctools/make_release/make_release_class.py @@ -16,6 +16,10 @@ class MakeRelease(Tool): """Tool to create a release archive from a TwinCAT project.""" + LOGGER_NAME = "make_release" + + CONFIG_KEY = "make_release" + def __init__(self, *args): super().__init__(*args) diff --git a/tests/test_xml_sorter.py b/tests/test_xml_sorter.py index ae54e07..bc01f3e 100644 --- a/tests/test_xml_sorter.py +++ b/tests/test_xml_sorter.py @@ -216,3 +216,21 @@ def test_project(plc_code, caplog): for exp in expected: assert exp in result + + +def test_config_file(plc_code, caplog, monkeypatch): + """Make sure options can be picked from a config file.""" + + monkeypatch.chdir(plc_code) + + conf_file = plc_code / "tctools.toml" + conf_file.write_text( + """[tctools.xml_sort] +target = "TwinCAT Project1/TwinCAT Project1.tsproj" +skip_nodes = ["Device", "DeploymentEvents", "TcSmItem", "DataType"] +""" + ) + xml_sort_main() + + assert "Checked 1 path(s)" in caplog.messages + assert "Re-saved 1 path(s)" in caplog.messages