Forked from
Owen Graham / visualizer-synthesis
This fork has diverged from the upstream repository.
-
Owen Graham authoredOwen Graham authored
parameters.py 916 B
"""Get (and convert) URL parameters."""
from flask import abort, request
from .data import measurements
def get_param(key, to=str, call_abort=True):
"""Get a parameter from the query string.
Can call `abort(400)` if the value is the wrong type or missing.
"""
val = request.args.get(key)
if val is None:
if call_abort:
abort(400)
return None
try:
return to(val)
except ValueError:
if call_abort:
abort(400)
return None
def meas_type(s):
"""Type converter for valid measurements."""
val = measurements.get(s)
if val is not None:
return val
raise ValueError(f'bad measurement arg: {s!r}')
def year_type(s):
"""Type converter for valid years."""
if s.isdigit():
val = int(s)
if val in range(1, 10000):
return val
raise ValueError(f'bad year arg: {s!r}')