Debian Codegraph @

cylc/flow/xtriggers/echo.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """A Cylc xtrigger function."""
18
19 from typing import Any, Dict, Tuple
20 from cylc.flow.exceptions import WorkflowConfigError
21
22
23 def echo(*args, **kwargs) -> Tuple:
24 """Print arguments to stdout, return kwargs['succeed'] and kwargs.
25
26 This may be a useful aid to understanding how xtriggers work.
27
28 Args:
29 succeed: Set the success of failure of this xtrigger.
30 *args: Print to stdout.
31 **kwargs: Print to stdout, and return as output.
32
33 Returns:
34 (True/False, kwargs)
35
36 Examples:
37
38 >>> echo('Breakfast Time', succeed=True, egg='poached')
39 echo: ARGS: ('Breakfast Time',)
40 echo: KWARGS: {'succeed': True, 'egg': 'poached'}
41 (True, {'succeed': True, 'egg': 'poached'})
42
43 """
44 print("echo: ARGS:", args)
45 print("echo: KWARGS:", kwargs)
46
47 return kwargs["succeed"], kwargs
48
49
50 def validate(all_args: Dict[str, Any]):
51 """
52 Validate the xtrigger function arguments parsed from the workflow config.
53
54 This is separate from the xtrigger to allow parse-time validation.
55
56 """
57 # NOTE: with (*args, **kwargs) pattern, all_args looks like:
58 # {
59 # 'args': (arg1, arg2, ...),
60 # 'kwargs': {kwarg1: val, kwarg2: val, ...}
61 # }
62 succeed = all_args['kwargs'].get("succeed")
63 if not isinstance(succeed, bool):
64 raise WorkflowConfigError("Requires 'succeed=True/False' arg")