Debian Codegraph @

cylc/flow/broadcast_report.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 """Provide a function to report modification to broadcast settings."""
17
18
19 BAD_OPTIONS_FMT = "\n --%s=%s"
20 BAD_OPTIONS_TITLE = "No broadcast to cancel/clear for these options:"
21 BAD_OPTIONS_TITLE_SET = ("Rejected broadcast: settings are not"
22 " compatible with the workflow")
23 CHANGE_FMT = "\n%(change)s [%(point)s/%(namespace)s] %(key)s=%(value)s"
24 CHANGE_PREFIX_CANCEL = "-"
25 CHANGE_PREFIX_SET = "+"
26 CHANGE_TITLE_CANCEL = "Broadcast cancelled:"
27 CHANGE_TITLE_SET = "Broadcast set:"
28
29 CLI_OPT_MAP = {
30 # broadcast field: cli option
31 'point_strings': 'point',
32 'namespaces': 'namespace',
33 'settings': 'set',
34 }
35
36
37 def get_broadcast_bad_options_report(bad_options, is_set=False):
38 """Return a string to report bad options for broadcast cancel/clear."""
39 if not bad_options:
40 return None
41 if is_set:
42 msg = BAD_OPTIONS_TITLE_SET
43 else:
44 msg = BAD_OPTIONS_TITLE
45 for key, values in sorted(bad_options.items()):
46 for value in values:
47 if isinstance(value, (list, tuple)):
48 value_str = ""
49 values = list(value)
50 while values:
51 val = values.pop(0)
52 if values:
53 value_str += "[" + val + "]"
54 else:
55 value_str += val
56 else:
57 value_str = value
58 if isinstance(value, dict):
59 value_str = ', '.join(
60 f'{key}={val}' for key, val in value.items()
61 )
62 msg += BAD_OPTIONS_FMT % (CLI_OPT_MAP.get(key, key), value_str)
63 return msg
64
65
66 def get_broadcast_change_iter(modified_settings, is_cancel=False):
67 """Return an iterator of broadcast changes.
68
69 Each broadcast change is a dict with keys:
70 change, point, namespace, key, value
71
72 """
73 if not modified_settings:
74 return
75 if is_cancel:
76 change = CHANGE_PREFIX_CANCEL
77 else:
78 change = CHANGE_PREFIX_SET
79 for modified_setting in sorted(modified_settings,
80 key=lambda x: (x[0], x[1])):
81 # sorted by (point, namespace)
82 point, namespace, setting = modified_setting
83 value = setting
84 keys_str = ""
85 while isinstance(value, dict):
86 key, value = next(iter(value.items()))
87 if isinstance(value, dict):
88 keys_str += "[" + key + "]"
89 else:
90 keys_str += key
91 yield {
92 "change": change,
93 "point": point,
94 "namespace": namespace,
95 "key": keys_str,
96 "value": str(value)}
97
98
99 def get_broadcast_change_report(modified_settings, is_cancel=False):
100 """Return a string for reporting modification to broadcast settings."""
101 if not modified_settings:
102 return ""
103 if is_cancel:
104 msg = CHANGE_TITLE_CANCEL
105 else:
106 msg = CHANGE_TITLE_SET
107 for broadcast_change in get_broadcast_change_iter(
108 modified_settings, is_cancel):
109 msg += CHANGE_FMT % broadcast_change
110 return msg