Skip to content

Commit

Permalink
Fix tool show-config, fixes #2702
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Feb 19, 2025
1 parent 25314b3 commit 0ca77cb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
5 changes: 5 additions & 0 deletions docs/changes/2703.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix ``Tool`` not exposing all options defined by ``traitlets.Application``
by default.

Fix ``--show-config`` and ``--show-config-json`` by not running ``setup`` and
``finish`` steps in case those options are given.
17 changes: 17 additions & 0 deletions src/ctapipe/core/tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ class MyTool(Tool):
assert not tool.provenance_log.exists()


def test_show_config(tmpdir, capsys):
"""check --show-config and --show-config-json work"""
from ctapipe.core.tool import run_tool

class MyTool(Tool):
description = "test"
userparam = Float(5.0, help="parameter").tag(config=True)

tool = MyTool()

for o in ["--show-config", "--show-config-json"]:
assert run_tool(tool, [o, "--MyTool.userparam=3.1415926"], cwd=tmpdir) == 0
captured = capsys.readouterr()
assert "userparam" in captured.out
assert "3.1415926" in captured.out


def test_export_config_to_yaml():
"""test that we can export a Tool's config to YAML"""
import yaml
Expand Down
34 changes: 20 additions & 14 deletions src/ctapipe/core/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,20 @@ def __init__(self, **kwargs):
# makes sure user defined aliases override default aliases
self.aliases = {**aliases, **self.aliases}

flags = {
("q", "quiet"): ({"Tool": {"quiet": True}}, "Disable console logging."),
("v", "verbose"): (
{"Tool": {"log_level": "DEBUG"}},
"Set log level to DEBUG",
),
"overwrite": (
{"Tool": {"overwrite": True}},
"Overwrite existing output files without asking",
),
}
flags = Application.flags.copy()
flags.update(
{
("q", "quiet"): ({"Tool": {"quiet": True}}, "Disable console logging."),
("v", "verbose"): (
{"Tool": {"log_level": "DEBUG"}},
"Set log level to DEBUG",
),
"overwrite": (
{"Tool": {"overwrite": True}},
"Overwrite existing output files without asking",
),
}
)
self.flags = {**flags, **self.flags}

self.is_setup = False
Expand Down Expand Up @@ -423,8 +426,9 @@ def run(self, argv=None, raises=False):

self.initialize(argv)

self.setup()
self.is_setup = True
if not self.show_config:
self.setup()
self.is_setup = True

self.log.debug("CONFIG: %s", self.get_current_config())
Provenance().add_config(self.get_current_config())
Expand All @@ -437,7 +441,9 @@ def run(self, argv=None, raises=False):
self.log.removeHandler(self.trait_warning_handler)

self.start()
self.finish()
if not self.show_config:
self.finish()

except (ToolConfigurationError, TraitError) as err:
current_exception = err
self.log.error("%s", err)
Expand Down

0 comments on commit 0ca77cb

Please sign in to comment.