Skip to content

Commit

Permalink
Feature/add wave alternative grid (#66)
Browse files Browse the repository at this point in the history
* add option to turn off hacky fix
* add wave grid hash
* match on step ranges
* chore: add pre-commit-config

---------

Co-authored-by: Peter Tsrunchev <[email protected]>
  • Loading branch information
mathleur and peshence committed Jan 24, 2025
1 parent 4f33041 commit 8ad5bee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 13 additions & 8 deletions polytope_server/common/datasource/coercion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import re
from datetime import datetime, timedelta
from typing import Any, Dict

Expand All @@ -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"]

Expand All @@ -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)
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions polytope_server/common/datasource/polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8ad5bee

Please sign in to comment.