Skip to content

Commit

Permalink
Include xtrigger function signatures in cylc config (#6071)
Browse files Browse the repository at this point in the history
  • Loading branch information
MetRonnie authored Apr 18, 2024
1 parent df10bf5 commit e621d9c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
1 change: 1 addition & 0 deletions changes.d/6071.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`cylc config` now shows xtrigger function signatures.
14 changes: 14 additions & 0 deletions cylc/flow/subprocctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,17 @@ def get_signature(self):
args = self.func_args + [
"%s=%s" % (i, self.func_kwargs[i]) for i in skeys]
return "%s(%s)" % (self.func_name, ", ".join([str(a) for a in args]))

def dump(self) -> str:

Check warning on line 162 in cylc/flow/subprocctx.py

View check run for this annotation

Codecov / codecov/patch

cylc/flow/subprocctx.py#L162

Added line #L162 was not covered by tests
"""Output for logging."""
return SubProcContext.__str__(self)

def __str__(self) -> str:

Check warning on line 166 in cylc/flow/subprocctx.py

View check run for this annotation

Codecov / codecov/patch

cylc/flow/subprocctx.py#L166

Added line #L166 was not covered by tests
"""
>>> str(SubFuncContext('label', 'my_func', [1, 2], {'a': 3}))
'my_func(1, 2, a=3):10.0'
"""
return f"{self.get_signature()}:{self.intvl}"

def __repr__(self) -> str:
return f"<{type(self).__name__} {self}>"

Check warning on line 174 in cylc/flow/subprocctx.py

View check run for this annotation

Codecov / codecov/patch

cylc/flow/subprocctx.py#L173-L174

Added lines #L173 - L174 were not covered by tests
2 changes: 1 addition & 1 deletion cylc/flow/xtrigger_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def callback(self, ctx: 'SubFuncContext'):
Raises:
ValueError: if the context given is not active
"""
LOG.debug(ctx)
LOG.debug(ctx.dump())
sig = ctx.get_signature()
self.active.remove(sig)
try:
Expand Down
16 changes: 9 additions & 7 deletions tests/integration/test_subprocctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""

from logging import DEBUG
from textwrap import dedent


async def test_log_xtrigger_stdout(
Expand All @@ -37,13 +38,14 @@ async def test_log_xtrigger_stdout(
# Create an xtrigger:
xt_lib = run_dir / id_ / 'lib/python/myxtrigger.py'
xt_lib.parent.mkdir(parents=True, exist_ok=True)
xt_lib.write_text(
"from sys import stderr\n\n\n"
"def myxtrigger():\n"
" print('Hello World')\n"
" print('Hello Hades', file=stderr)\n"
" return True, {}"
)
xt_lib.write_text(dedent(r"""
from sys import stderr
def myxtrigger():
print('Hello World')
print('Hello Hades', file=stderr)
return True, {}
"""))
schd = scheduler(id_)
async with start(schd, level=DEBUG) as log:
# Set off check for x-trigger:
Expand Down
48 changes: 44 additions & 4 deletions tests/unit/scripts/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import asyncio
import os
from pathlib import Path
from types import SimpleNamespace
from textwrap import dedent
from typing import Any, Optional, List
from cylc.flow.exceptions import InputError

import pytest
from pytest import param

from cylc.flow.scripts.config import get_config_file_hierarchy
from cylc.flow.cfgspec.globalcfg import GlobalConfig
from cylc.flow.option_parsers import Options
from cylc.flow.scripts.config import (
_main,
get_config_file_hierarchy,
get_option_parser,
)
from cylc.flow.workflow_files import WorkflowFiles


Fixture = Any
Expand Down Expand Up @@ -200,3 +205,38 @@ def test_cylc_site_conf_path_env_var(
GlobalConfig.get_inst()

assert capload == files


def test_cylc_config_xtriggers(tmp_run_dir, capsys: pytest.CaptureFixture):
"""Test `cylc config` outputs any xtriggers properly"""
run_dir: Path = tmp_run_dir('constellation')
flow_file = run_dir / WorkflowFiles.FLOW_FILE
flow_file.write_text(dedent("""
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 2020-05-05
[[xtriggers]]
clock_1 = wall_clock(offset=PT1H):PT4S
rotund = xrandom(90, 2)
[[graph]]
R1 = @rotund => foo
"""))
option_parser = get_option_parser()

asyncio.run(
_main(option_parser, Options(option_parser)(), 'constellation')
)
assert capsys.readouterr().out == dedent("""\
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 2020-05-05
[[xtriggers]]
clock_1 = wall_clock(offset=PT1H):4.0
rotund = xrandom(90, 2):10.0
[[graph]]
R1 = @rotund => foo
[runtime]
[[root]]
""")

0 comments on commit e621d9c

Please sign in to comment.