diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..27e3c90 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,16 @@ +repos: + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + name: isort (python) + + - repo: https://github.com/psf/black + rev: 24.8.0 # Use the latest version of black + hooks: + - id: black + + - repo: https://github.com/pycqa/flake8 + rev: 7.1.1 # Use the latest version of flake8 + hooks: + - id: flake8 diff --git a/polytope_server/common/datasource/coercion.py b/polytope_server/common/datasource/coercion.py index 6cb73f5..66c444c 100644 --- a/polytope_server/common/datasource/coercion.py +++ b/polytope_server/common/datasource/coercion.py @@ -1,4 +1,5 @@ import copy +import re from datetime import datetime, timedelta from typing import Any, Dict @@ -8,7 +9,6 @@ class CoercionError(Exception): class Coercion: - allow_ranges = ["number", "step", "date", "time"] allow_lists = ["class", "stream", "type", "expver", "param", "number", "date", "step", "time"] @@ -29,7 +29,6 @@ def coerce_value(key: str, value: Any): coerced_values = [Coercion.coerce_value(key, v) for v in value] return coerced_values elif isinstance(value, str): - if "/to/" in value and key in Coercion.allow_ranges: # Handle ranges with possible "/by/" suffix start_value, rest = value.split("/to/", 1) @@ -104,22 +103,29 @@ def coerce_date(value: Any) -> str: @staticmethod def coerce_step(value: Any) -> str: - if isinstance(value, int): if value < 0: raise CoercionError("Step must be greater than or equal to 0.") else: return str(value) elif isinstance(value, str): - if not value.isdigit() or int(value) < 0: - raise CoercionError("Step must be greater than or equal to 0.") - return value + try: + if int(value) < 0: + raise CoercionError("Step must be greater than or equal to 0.") + else: + return value + except ValueError: + # value cannot be converted to a digit, but we would like to match step ranges too + pattern = r"^\d+-\d+$" + if re.match(pattern, value): + return value + else: + raise CoercionError("Invalid type, expected integer step or step range.") else: raise CoercionError("Invalid type, expected integer or string.") @staticmethod def coerce_number(value: Any) -> str: - if isinstance(value, int): if value <= 0: raise CoercionError("Number must be a positive value.") @@ -213,7 +219,6 @@ def coerce_time(value: Any) -> str: @staticmethod def coerce_expver(value: Any) -> str: - # Integers accepted, converted to 4-length strings if isinstance(value, int): if 0 <= value <= 9999: diff --git a/polytope_server/common/datasource/polytope.py b/polytope_server/common/datasource/polytope.py index 2a976f0..0935e63 100644 --- a/polytope_server/common/datasource/polytope.py +++ b/polytope_server/common/datasource/polytope.py @@ -171,6 +171,9 @@ def change_hash(self, request, config): ) hash = "1c409f6b78e87eeaeeb4a7294c28add7" return self.change_config_grid_hash(config, hash) + if request["stream"] == "wave": + hash = "386742a2dd1201b67f2d19ed421353ea" + return self.change_config_grid_hash(config, hash) # This only holds for operational data if request.get("dataset", None) is None: