|
| 1 | +import copy |
| 2 | +from datetime import datetime, timedelta |
| 3 | +from typing import Any, Dict |
| 4 | + |
| 5 | + |
| 6 | +class CoercionError(Exception): |
| 7 | + pass |
| 8 | + |
| 9 | + |
| 10 | +class Coercion: |
| 11 | + |
| 12 | + allow_ranges = [ |
| 13 | + "number", |
| 14 | + "step", |
| 15 | + "date", |
| 16 | + ] |
| 17 | + allow_lists = ["class", "stream", "type", "expver", "param", "number", "date", "step"] |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def coerce(request: Dict[str, Any]) -> Dict[str, Any]: |
| 21 | + request = copy.deepcopy(request) |
| 22 | + for key, value in request.items(): |
| 23 | + request[key] = Coercion.coerce_value(key, value) |
| 24 | + return request |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def coerce_value(key: str, value: Any): |
| 28 | + if key in Coercion.coercer: |
| 29 | + coercer_func = Coercion.coercer[key] |
| 30 | + |
| 31 | + if isinstance(value, list): |
| 32 | + # Coerce each item in the list |
| 33 | + coerced_values = [Coercion.coerce_value(key, v) for v in value] |
| 34 | + return "/".join(coerced_values) |
| 35 | + elif isinstance(value, str): |
| 36 | + |
| 37 | + if "/to/" in value and key in Coercion.allow_ranges: |
| 38 | + # Handle ranges with possible "/by/" suffix |
| 39 | + start_value, rest = value.split("/to/", 1) |
| 40 | + if not rest: |
| 41 | + raise CoercionError(f"Invalid range format for key {key}.") |
| 42 | + |
| 43 | + if "/by/" in rest: |
| 44 | + end_value, suffix = rest.split("/by/", 1) |
| 45 | + suffix = "/by/" + suffix # Add back the '/by/' |
| 46 | + else: |
| 47 | + end_value = rest |
| 48 | + suffix = "" |
| 49 | + |
| 50 | + # Coerce start_value and end_value |
| 51 | + start_coerced = coercer_func(start_value) |
| 52 | + end_coerced = coercer_func(end_value) |
| 53 | + |
| 54 | + return f"{start_coerced}/to/{end_coerced}{suffix}" |
| 55 | + elif "/" in value and key in Coercion.allow_lists: |
| 56 | + # Handle lists |
| 57 | + coerced_values = [coercer_func(v) for v in value.split("/")] |
| 58 | + return "/".join(coerced_values) |
| 59 | + else: |
| 60 | + # Single value |
| 61 | + return coercer_func(value) |
| 62 | + else: # not list or string |
| 63 | + return coercer_func(value) |
| 64 | + else: |
| 65 | + if isinstance(value, list): |
| 66 | + # Join list into '/' separated string |
| 67 | + coerced_values = [str(v) for v in value] |
| 68 | + return "/".join(coerced_values) |
| 69 | + else: |
| 70 | + return value |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def coerce_date(value: Any) -> str: |
| 74 | + try: |
| 75 | + # Attempt to convert the value to an integer |
| 76 | + int_value = int(value) |
| 77 | + if int_value > 0: |
| 78 | + # Positive integers are assumed to be dates in YYYYMMDD format |
| 79 | + date_str = str(int_value) |
| 80 | + try: |
| 81 | + datetime.strptime(date_str, "%Y%m%d") |
| 82 | + return date_str |
| 83 | + except ValueError: |
| 84 | + raise CoercionError("Invalid date format, expected YYYYMMDD or YYYY-MM-DD.") |
| 85 | + else: |
| 86 | + # Zero or negative integers represent relative days from today |
| 87 | + target_date = datetime.today() + timedelta(days=int_value) |
| 88 | + return target_date.strftime("%Y%m%d") |
| 89 | + except (ValueError, TypeError): |
| 90 | + # The value is not an integer or cannot be converted to an integer |
| 91 | + pass |
| 92 | + |
| 93 | + if isinstance(value, str): |
| 94 | + value_stripped = value.strip() |
| 95 | + # Try parsing as YYYYMMDD |
| 96 | + try: |
| 97 | + datetime.strptime(value_stripped, "%Y%m%d") |
| 98 | + return value_stripped |
| 99 | + except ValueError: |
| 100 | + # Try parsing as YYYY-MM-DD |
| 101 | + try: |
| 102 | + date_obj = datetime.strptime(value_stripped, "%Y-%m-%d") |
| 103 | + return date_obj.strftime("%Y%m%d") |
| 104 | + except ValueError: |
| 105 | + raise CoercionError("Invalid date format, expected YYYYMMDD or YYYY-MM-DD.") |
| 106 | + else: |
| 107 | + raise CoercionError("Invalid date format, expected YYYYMMDD or YYYY-MM-DD.") |
| 108 | + |
| 109 | + @staticmethod |
| 110 | + def coerce_step(value: Any) -> str: |
| 111 | + |
| 112 | + if isinstance(value, int): |
| 113 | + if value < 0: |
| 114 | + raise CoercionError("Step must be greater than or equal to 0.") |
| 115 | + else: |
| 116 | + return str(value) |
| 117 | + elif isinstance(value, str): |
| 118 | + if not value.isdigit() or int(value) < 0: |
| 119 | + raise CoercionError("Step must be greater than or equal to 0.") |
| 120 | + return value |
| 121 | + else: |
| 122 | + raise CoercionError("Invalid type, expected integer or string.") |
| 123 | + |
| 124 | + @staticmethod |
| 125 | + def coerce_number(value: Any) -> str: |
| 126 | + |
| 127 | + if isinstance(value, int): |
| 128 | + if value <= 0: |
| 129 | + raise CoercionError("Number must be a positive value.") |
| 130 | + else: |
| 131 | + return str(value) |
| 132 | + elif isinstance(value, str): |
| 133 | + if not value.isdigit() or int(value) <= 0: |
| 134 | + raise CoercionError("Number must be a positive integer.") |
| 135 | + return value |
| 136 | + else: |
| 137 | + raise CoercionError("Invalid type, expected integer or string.") |
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def coerce_param(value: Any) -> str: |
| 141 | + if isinstance(value, int): |
| 142 | + return str(value) |
| 143 | + elif isinstance(value, str): |
| 144 | + return value |
| 145 | + else: |
| 146 | + raise CoercionError("Invalid param type, expected integer or string.") |
| 147 | + |
| 148 | + @staticmethod |
| 149 | + def coerce_time(value: Any) -> str: |
| 150 | + if isinstance(value, int): |
| 151 | + if value < 0: |
| 152 | + raise CoercionError("Invalid time format, expected HHMM or HH greater than zero.") |
| 153 | + elif value < 24: |
| 154 | + # Treat as hour with minute=0 |
| 155 | + hour = value |
| 156 | + minute = 0 |
| 157 | + elif 100 <= value <= 2359: |
| 158 | + # Possible HHMM format |
| 159 | + hour = value // 100 |
| 160 | + minute = value % 100 |
| 161 | + else: |
| 162 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 163 | + elif isinstance(value, str): |
| 164 | + value_stripped = value.strip() |
| 165 | + # Check for colon-separated time (e.g., "12:00") |
| 166 | + if ":" in value_stripped: |
| 167 | + parts = value_stripped.split(":") |
| 168 | + if len(parts) != 2: |
| 169 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 170 | + hour_str, minute_str = parts |
| 171 | + if not (hour_str.isdigit() and minute_str.isdigit()): |
| 172 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 173 | + hour = int(hour_str) |
| 174 | + minute = int(minute_str) |
| 175 | + else: |
| 176 | + if value_stripped.isdigit(): |
| 177 | + num_digits = len(value_stripped) |
| 178 | + if num_digits == 4: |
| 179 | + # Format is "HHMM" |
| 180 | + hour = int(value_stripped[:2]) |
| 181 | + minute = int(value_stripped[2:]) |
| 182 | + elif num_digits <= 2: |
| 183 | + # Format is "H" or "HH" |
| 184 | + hour = int(value_stripped) |
| 185 | + minute = 0 |
| 186 | + else: |
| 187 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 188 | + else: |
| 189 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 190 | + else: |
| 191 | + raise CoercionError("Invalid type for time, expected string or integer.") |
| 192 | + |
| 193 | + # Validate hour and minute |
| 194 | + if not (0 <= hour <= 23): |
| 195 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 196 | + if not (0 <= minute <= 59): |
| 197 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 198 | + if minute != 0: |
| 199 | + raise CoercionError("Invalid time format, expected HHMM or HH.") |
| 200 | + |
| 201 | + # Format time as HHMM |
| 202 | + time_str = f"{hour:02d}{minute:02d}" |
| 203 | + return time_str |
| 204 | + |
| 205 | + # Validate hour and minute |
| 206 | + if not (0 <= hour <= 23): |
| 207 | + raise CoercionError("Hour must be between 0 and 23.") |
| 208 | + if not (0 <= minute <= 59): |
| 209 | + raise CoercionError("Minute must be between 0 and 59.") |
| 210 | + if minute != 0: |
| 211 | + # In your test cases, minute must be zero |
| 212 | + raise CoercionError("Minute must be zero.") |
| 213 | + |
| 214 | + # Format time as HHMM |
| 215 | + time_str = f"{hour:02d}{minute:02d}" |
| 216 | + return time_str |
| 217 | + |
| 218 | + @staticmethod |
| 219 | + def coerce_expver(value: Any) -> str: |
| 220 | + |
| 221 | + # Integers accepted, converted to 4-length strings |
| 222 | + if isinstance(value, int): |
| 223 | + if 0 <= value <= 9999: |
| 224 | + return f"{value:0>4d}" |
| 225 | + else: |
| 226 | + raise CoercionError("expver integer must be between 0 and 9999 inclusive.") |
| 227 | + |
| 228 | + # Strings accepted if they are convertible to integer or exactly 4 characters long |
| 229 | + elif isinstance(value, str): |
| 230 | + if value.isdigit(): |
| 231 | + int_value = int(value.lstrip("0") or "0") |
| 232 | + if 0 <= int_value <= 9999: |
| 233 | + return f"{int_value:0>4d}" |
| 234 | + else: |
| 235 | + raise CoercionError("expver integer string must represent a number between 0 and 9999 inclusive.") |
| 236 | + elif len(value) == 4: |
| 237 | + return value |
| 238 | + else: |
| 239 | + raise CoercionError("expver string length must be 4 characters exactly.") |
| 240 | + |
| 241 | + else: |
| 242 | + raise CoercionError("expver must be an integer or a string.") |
| 243 | + |
| 244 | + coercer = { |
| 245 | + "date": coerce_date, |
| 246 | + "step": coerce_step, |
| 247 | + "number": coerce_number, |
| 248 | + "param": coerce_param, |
| 249 | + "time": coerce_time, |
| 250 | + "expver": coerce_expver, |
| 251 | + } |
0 commit comments