Debian Codegraph @

cylc/flow/graphnode.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 graph node parsing and caching service"""
17
18 import re
19
20 from cylc.flow.cycling.loader import (
21 get_interval, get_interval_cls, is_offset_absolute)
22 from cylc.flow.exceptions import GraphParseError
23 from cylc.flow.task_id import TaskID
24 from cylc.flow.task_trigger import TaskTrigger
25
26
27 class GraphNodeParser:
28 """Provide graph node parsing and caching service.
29
30 Optional output notation is stripped out before this class gets used.
31 TODO is any of this redundant with code in the graph_parser module?
32 """
33 # Match a graph node string.
34 REC_NODE = re.compile(
35 r"^" +
36 r"(" + TaskID.NAME_RE + r")" +
37 r"""(?:\[ # Begin optional [offset] syntax
38 (?!T[+-]) # Do not match a 'T-' or 'T+'
39 # (this is the old format)
40 (\^?) # Initial cycle point offset marker
41 ([^\]]*) # Continue until next ']'
42 \] # Stop at next ']'
43 )? # End optional [offset] syntax]
44 (?::([\w-]+))? # Optional output (e.g. :succeed)
45 $
46 """, re.X)
47
48 # A potentially non-regular offset, such as foo[01T+P1W].
49 REC_IRREGULAR_OFFSET = re.compile(
50 r"""^ # Start of string
51 ( # Begin group
52 ..+ # EITHER: Two or more characters
53 [+-]P # Then either +P or -P for start of duration
54 .* # Then anything for the rest of the duration
55 | # OR:
56 [^P]+ # No 'P' characters anywhere (e.g. T00).
57 ) # End group
58 $ # End of string
59 """, re.X)
60
61 _INSTANCE = None
62
63 @classmethod
64 def get_inst(cls):
65 """Return the singleton instance."""
66 if cls._INSTANCE is None:
67 cls._INSTANCE = cls()
68 return cls._INSTANCE
69
70 def __init__(self):
71 self._offsets = {}
72 self._nodes = {}
73
74 def clear(self):
75 """Clear caches."""
76 self._offsets.clear()
77 self._nodes.clear()
78
79 def _get_offset(self, offset=None):
80 """Return and cache the standardised offset string."""
81 if offset not in self._offsets:
82 if offset:
83 res = get_interval(offset).standardise()
84 else:
85 res = get_interval_cls().get_null_offset()
86 self._offsets[offset] = str(res)
87 return self._offsets[offset]
88
89 def parse(self, node):
90 """Parse graph node, and cache the result.
91
92 Args:
93 node (str): node to parse
94
95 Return:
96 tuple:
97 (name, offset, output,
98 offset_is_from_icp, offset_is_irregular, offset_is_absolute)
99
100 NOTE that offsets from ICP like foo[^] and foo[^+P1] are not considered
101 absolute like foo[2] etc.
102
103 Raise:
104 GraphParseError: on illegal syntax.
105 """
106 if node not in self._nodes:
107 match = self.REC_NODE.match(node)
108 if not match:
109 raise GraphParseError('Illegal graph node: %s' % node)
110 name, icp_mark, offset, output = match.groups()
111 offset_is_from_icp = (icp_mark == '^') # convert to boolean
112 if offset_is_from_icp and not offset:
113 offset = self._get_offset()
114 offset_is_irregular = False
115 offset_is_absolute = False
116 if offset:
117 if is_offset_absolute(offset):
118 offset_is_absolute = True
119 if self.REC_IRREGULAR_OFFSET.search(offset):
120 offset_is_irregular = True
121 else:
122 offset = self._get_offset(offset)
123 self._nodes[node] = (
124 name, offset, TaskTrigger.standardise_name(output),
125 offset_is_from_icp, offset_is_irregular, offset_is_absolute)
126 return self._nodes[node]