Debian Codegraph @

cylc/flow/task_id.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 """Task ID utilities."""
17
18
19 import re
20 from typing import Optional, TYPE_CHECKING
21
22 from cylc.flow.cycling.loader import get_point, standardise_point_string
23 from cylc.flow.exceptions import PointParsingError
24 from cylc.flow.id import Tokens
25
26 if TYPE_CHECKING:
27 from cylc.flow.cycling import PointBase
28
29
30 _TASK_NAME_PREFIX = r'\w'
31 _TASK_NAME_CHARACTERS = [r'\w', r'\-', '+', '%', '@']
32
33
34 class TaskID:
35 """Task ID utilities."""
36
37 DELIM = '.'
38 DELIM_RE = r"\."
39 DELIM2 = '/'
40 NAME_SUFFIX_RE = r"[\w\-+%@]+"
41 NAME_SUFFIX_REC = re.compile(r"\A" + NAME_SUFFIX_RE + r"\Z")
42 NAME_RE = rf'{_TASK_NAME_PREFIX}[{"".join(_TASK_NAME_CHARACTERS)}]*'
43 NAME_REC = re.compile(r"\A" + NAME_RE + r"\Z")
44 POINT_RE = r"\S+"
45 POINT_REC = re.compile(r"\A" + POINT_RE + r"\Z")
46 SYNTAX = 'NAME' + DELIM + 'CYCLE_POINT'
47 SYNTAX_OPT_POINT = 'NAME[' + DELIM + 'CYCLE_POINT]'
48 ID_RE = NAME_RE + DELIM_RE + POINT_RE
49
50 @classmethod
51 def get(cls, name, point):
52 """Return a task id from name and a point string."""
53 return name + cls.DELIM + str(point)
54
55 @classmethod
56 def split(cls, id_str):
57 """Return a name and a point string from an id."""
58 return id_str.split(cls.DELIM, 1)
59
60 @classmethod
61 def is_valid_name(cls, name):
62 """Return whether a task name is valid."""
63 return name and cls.NAME_REC.match(name)
64
65 @classmethod
66 def is_valid_id(cls, id_str):
67 """Return whether a task id is valid "NAME.POINT" format."""
68 if cls.DELIM not in id_str:
69 return False
70 name, point = cls.split(id_str)
71 # N.B. only basic cycle point check
72 return (name and cls.NAME_REC.match(name) and
73 point and cls.POINT_REC.match(point))
74
75 @classmethod
76 def is_valid_id_2(cls, id_str):
77 """Return whether id_str is good as a client argument for e.g. insert.
78
79 Return True if "." or "/" appears once in the string. Cannot really
80 do more as the string may have wildcards.
81 """
82 return id_str.count(cls.DELIM) == 1 or id_str.count(cls.DELIM2) == 1
83
84 @classmethod
85 def get_standardised_point_string(cls, point_string):
86 """Return a standardised point string.
87
88 Used to process incoming command arguments.
89 """
90 try:
91 point_string = standardise_point_string(point_string)
92 except PointParsingError as exc:
93 # (This is only needed to raise a clearer error message).
94 raise ValueError(
95 "Invalid cycle point: %s (%s)" % (point_string, exc)
96 ) from None
97 return point_string
98
99 @classmethod
100 def get_standardised_point(
101 cls,
102 point_string: str,
103 ) -> 'Optional[PointBase]':
104 """Return a standardised point."""
105 return get_point(cls.get_standardised_point_string(point_string))
106
107 @classmethod
108 def get_standardised_taskid(cls, task_id):
109 """Return task ID with standardised cycle point."""
110 tokens = Tokens(task_id, relative=True)
111 return tokens.duplicate(
112 cycle=cls.get_standardised_point_string(tokens['cycle'])
113 ).relative_id