| 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 |
""" |
| 18 |
Tasks spawn a sequence of POINTS (P) separated by INTERVALS (I). |
| 19 |
Each task may have multiple sequences, e.g. 12-hourly and 6-hourly. |
| 20 |
""" |
| 21 |
|
| 22 |
from typing import ( |
| 23 |
Literal, |
| 24 |
Optional, |
| 25 |
Type, |
| 26 |
overload, |
| 27 |
) |
| 28 |
|
| 29 |
from metomi.isodatetime.data import Calendar |
| 30 |
|
| 31 |
from cylc.flow.cycling import ( |
| 32 |
PointBase, |
| 33 |
integer, |
| 34 |
iso8601, |
| 35 |
) |
| 36 |
|
| 37 |
|
| 38 |
ISO8601_CYCLING_TYPE = iso8601.CYCLER_TYPE_ISO8601 |
| 39 |
INTEGER_CYCLING_TYPE = integer.CYCLER_TYPE_INTEGER |
| 40 |
|
| 41 |
IS_OFFSET_ABSOLUTE_IMPLS = { |
| 42 |
INTEGER_CYCLING_TYPE: integer.is_offset_absolute, |
| 43 |
ISO8601_CYCLING_TYPE: iso8601.is_offset_absolute, |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
POINTS = {INTEGER_CYCLING_TYPE: integer.IntegerPoint, |
| 48 |
ISO8601_CYCLING_TYPE: iso8601.ISO8601Point} |
| 49 |
|
| 50 |
DUMP_FORMAT_GETTERS = {INTEGER_CYCLING_TYPE: integer.get_dump_format, |
| 51 |
ISO8601_CYCLING_TYPE: iso8601.get_dump_format} |
| 52 |
|
| 53 |
POINT_RELATIVE_GETTERS = { |
| 54 |
INTEGER_CYCLING_TYPE: integer.get_point_relative, |
| 55 |
ISO8601_CYCLING_TYPE: iso8601.get_point_relative |
| 56 |
} |
| 57 |
|
| 58 |
INTERVALS = {INTEGER_CYCLING_TYPE: integer.IntegerInterval, |
| 59 |
ISO8601_CYCLING_TYPE: iso8601.ISO8601Interval} |
| 60 |
|
| 61 |
SEQUENCES = {INTEGER_CYCLING_TYPE: integer.IntegerSequence, |
| 62 |
ISO8601_CYCLING_TYPE: iso8601.ISO8601Sequence} |
| 63 |
|
| 64 |
INIT_FUNCTIONS = {INTEGER_CYCLING_TYPE: integer.init_from_cfg, |
| 65 |
ISO8601_CYCLING_TYPE: iso8601.init_from_cfg} |
| 66 |
|
| 67 |
|
| 68 |
class DefaultCycler: |
| 69 |
|
| 70 |
"""Store the default TYPE for Cyclers.""" |
| 71 |
|
| 72 |
TYPE: str |
| 73 |
|
| 74 |
|
| 75 |
@overload |
| 76 |
def get_point(value: str, cycling_type: Optional[str] = None) -> PointBase: |
| 77 |
... |
| 78 |
|
| 79 |
|
| 80 |
@overload |
| 81 |
def get_point(value: None, cycling_type: Optional[str] = None) -> None: |
| 82 |
... |
| 83 |
|
| 84 |
|
| 85 |
def get_point( |
| 86 |
value: Optional[str], cycling_type: Optional[str] = None |
| 87 |
) -> Optional[PointBase]: |
| 88 |
"""Return a cylc.flow.cycling.PointBase-derived object from a string.""" |
| 89 |
if value is None: |
| 90 |
return None |
| 91 |
return get_point_cls(cycling_type=cycling_type)(value) |
| 92 |
|
| 93 |
|
| 94 |
def get_point_cls(cycling_type: Optional[str] = None) -> Type[PointBase]: |
| 95 |
"""Return the cylc.flow.cycling.PointBase-derived class we're using.""" |
| 96 |
if cycling_type is None: |
| 97 |
cycling_type = DefaultCycler.TYPE |
| 98 |
return POINTS[cycling_type] |
| 99 |
|
| 100 |
|
| 101 |
@overload |
| 102 |
def get_dump_format(cycling_type: Literal["integer"]) -> None: |
| 103 |
... |
| 104 |
|
| 105 |
|
| 106 |
@overload |
| 107 |
def get_dump_format(cycling_type: Literal["iso8601"]) -> str: |
| 108 |
... |
| 109 |
|
| 110 |
|
| 111 |
def get_dump_format(cycling_type: Literal["integer", "iso8601"]) -> str | None: |
| 112 |
"""Return cycle point dump format (None for integer mode).""" |
| 113 |
return DUMP_FORMAT_GETTERS[cycling_type]() |
| 114 |
|
| 115 |
|
| 116 |
def get_point_relative(*args, **kwargs): |
| 117 |
"""Return a point from an offset expression and a base point.""" |
| 118 |
cycling_type = kwargs.pop("cycling_type", DefaultCycler.TYPE) |
| 119 |
return POINT_RELATIVE_GETTERS[cycling_type](*args, **kwargs) |
| 120 |
|
| 121 |
|
| 122 |
def get_interval(*args, **kwargs): |
| 123 |
"""Return a cylc.flow.cycling.IntervalBase-derived object from a string.""" |
| 124 |
if args[0] is None: |
| 125 |
return None |
| 126 |
cycling_type = kwargs.pop("cycling_type", DefaultCycler.TYPE) |
| 127 |
return get_interval_cls(cycling_type=cycling_type)(*args, **kwargs) |
| 128 |
|
| 129 |
|
| 130 |
def get_interval_cls(cycling_type=None): |
| 131 |
"""Return the cylc.flow.cycling.IntervalBase-derived class we're using.""" |
| 132 |
if cycling_type is None: |
| 133 |
cycling_type = DefaultCycler.TYPE |
| 134 |
return INTERVALS[cycling_type] |
| 135 |
|
| 136 |
|
| 137 |
def get_sequence(*args, **kwargs): |
| 138 |
"""Return a cylc.flow.cycling.SequenceBase-derived object from a string.""" |
| 139 |
if args[0] is None: |
| 140 |
return None |
| 141 |
cycling_type = kwargs.pop("cycling_type", DefaultCycler.TYPE) |
| 142 |
return get_sequence_cls(cycling_type=cycling_type)(*args, **kwargs) |
| 143 |
|
| 144 |
|
| 145 |
def get_sequence_cls(cycling_type=None): |
| 146 |
"""Return the cylc.flow.cycling.SequenceBase-derived class we're using.""" |
| 147 |
if cycling_type is None: |
| 148 |
cycling_type = DefaultCycler.TYPE |
| 149 |
return SEQUENCES[cycling_type] |
| 150 |
|
| 151 |
|
| 152 |
def init_cyclers(cfg): |
| 153 |
"""Initialise cycling specifics using the workflow configuration (cfg).""" |
| 154 |
DefaultCycler.TYPE = cfg['scheduling']['cycling mode'] |
| 155 |
if DefaultCycler.TYPE in Calendar.MODES: |
| 156 |
DefaultCycler.TYPE = ISO8601_CYCLING_TYPE |
| 157 |
INIT_FUNCTIONS[DefaultCycler.TYPE](cfg) |
| 158 |
|
| 159 |
|
| 160 |
def is_offset_absolute(offset_string, **kwargs): |
| 161 |
"""Return True if offset_string is a point rather than an interval.""" |
| 162 |
cycling_type = kwargs.pop("cycling_type", DefaultCycler.TYPE) |
| 163 |
return IS_OFFSET_ABSOLUTE_IMPLS[cycling_type](offset_string) |
| 164 |
|
| 165 |
|
| 166 |
@overload |
| 167 |
def standardise_point_string( |
| 168 |
point_string: str, cycling_type: Optional[str] = None |
| 169 |
) -> str: |
| 170 |
... |
| 171 |
|
| 172 |
|
| 173 |
@overload |
| 174 |
def standardise_point_string( |
| 175 |
point_string: None, cycling_type: Optional[str] = None |
| 176 |
) -> None: |
| 177 |
... |
| 178 |
|
| 179 |
|
| 180 |
def standardise_point_string( |
| 181 |
point_string: Optional[str], cycling_type: Optional[str] = None, |
| 182 |
) -> Optional[str]: |
| 183 |
"""Return a standardised version of point_string.""" |
| 184 |
if point_string is None: |
| 185 |
return None |
| 186 |
point = get_point(point_string, cycling_type=cycling_type) |
| 187 |
if point is not None: |
| 188 |
point.standardise(allow_truncated=False) |
| 189 |
point_string = str(point) |
| 190 |
return point_string |