Skip to content

Commit

Permalink
Fixed config loading, tested with real project
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Jan 17, 2025
1 parent c4deec7 commit d7c3176
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/pages/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 (``_``).
20 changes: 14 additions & 6 deletions src/tctools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/tctools/format/format_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class Formatter(TcTool):

FILTER_DEFAULT = ["*.TcPOU", "*.TcGVL", "*.TcDUT"]

CONFIG_KEY = "format"

_RULE_CLASSES: List[Type[FormattingRule]] = []

def __init__(self, *args):
Expand Down
4 changes: 4 additions & 0 deletions src/tctools/git_info/git_info_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions src/tctools/make_release/make_release_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 18 additions & 0 deletions tests/test_xml_sorter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d7c3176

Please sign in to comment.