Skip to content

Commit

Permalink
support string or a float in sci notation
Browse files Browse the repository at this point in the history
  • Loading branch information
atanas-balevsky committed Dec 13, 2023
1 parent b345546 commit 9ff5b1c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
37 changes: 34 additions & 3 deletions terracotta/server/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from typing import Optional, Any, Mapping, Dict, Tuple
import json
import re

from marshmallow import Schema, fields, validate, pre_load, ValidationError, EXCLUDE
from flask import request, send_file, Response
Expand All @@ -21,6 +22,36 @@ class RGBQuerySchema(Schema):
tile_x = fields.Int(required=True, description="x coordinate")


def validate_range(data):
if isinstance(data, str) and data.startswith("p"):
if not re.match("^p\d+$", data):
raise ValidationError("Percentile format is `p<digits>`")

Check warning on line 28 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L28

Added line #L28 was not covered by tests
else:
try:
float(data)
except ValueError:
raise ValidationError("Must be a number")

Check warning on line 33 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L32-L33

Added lines #L32 - L33 were not covered by tests



class StringOrNumber(fields.Field):
def _serialize(self, value, attr, obj, **kwargs):
if isinstance(value, (str, bytes)):
return fields.String()._serialize(value, attr, obj, **kwargs)

Check warning on line 40 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L40

Added line #L40 was not covered by tests
elif isinstance(value, (int, float)):
return fields.Float()._serialize(value, attr, obj, **kwargs)

Check warning on line 42 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L42

Added line #L42 was not covered by tests
else:
raise ValidationError("Must be a string or a number")

Check warning on line 44 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L44

Added line #L44 was not covered by tests

def _deserialize(self, value, attr, data, **kwargs):
if isinstance(value, (str, bytes)):
return fields.String()._deserialize(value, attr, data, **kwargs)
elif isinstance(value, (int, float)):
return fields.Float()._deserialize(value, attr, data, **kwargs)
else:
raise ValidationError("Must be a string or a number")

Check warning on line 52 in terracotta/server/rgb.py

View check run for this annotation

Codecov / codecov/patch

terracotta/server/rgb.py#L52

Added line #L52 was not covered by tests


class RGBOptionSchema(Schema):
class Meta:
unknown = EXCLUDE
Expand All @@ -29,7 +60,7 @@ class Meta:
g = fields.String(required=True, description="Key value for green band")
b = fields.String(required=True, description="Key value for blue band")
r_range = fields.List(
fields.String(allow_none=True, validate=validate.Regexp("^p?(\d*\.)?\d+$")),
StringOrNumber(allow_none=True, validate=validate_range),
validate=validate.Length(equal=2),
example="[0,1]",
missing=None,
Expand All @@ -39,7 +70,7 @@ class Meta:
),
)
g_range = fields.List(
fields.String(allow_none=True, validate=validate.Regexp("^p?(\d*\.)?\d+$")),
StringOrNumber(allow_none=True, validate=validate_range),
validate=validate.Length(equal=2),
example="[0,1]",
missing=None,
Expand All @@ -49,7 +80,7 @@ class Meta:
),
)
b_range = fields.List(
fields.String(allow_none=True, validate=validate.Regexp("^p?(\d*\.)?\d+$")),
StringOrNumber(allow_none=True, validate=validate_range),
validate=validate.Length(equal=2),
example="[0,1]",
missing=None,
Expand Down
2 changes: 2 additions & 0 deletions tests/server/test_flask_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ def test_get_rgb_stretch(client, use_testdb, raster_file_xyz):

for stretch_range in (
"[0,10000]",
"[\"1.0e%2B01\",\"1.0e%2B04\"]",
"[\"p2\",\"p98\"]",
"[0,null]",
"[null, 10000]",
"[null,null]",
Expand Down

0 comments on commit 9ff5b1c

Please sign in to comment.