Commit 92ccd9f7 authored by Jerome Touvier's avatar Jerome Touvier
Browse files

code refactoring

parent 679dc6e4
Pipeline #46883 passed with stage
in 1 minute and 49 seconds
......@@ -21,6 +21,6 @@ build:
DOCKERFILE: Dockerfile
IMAGE_NAME: $CI_REGISTRY_IMAGE/ws-evalresp:$CI_COMMIT_SHORT_SHA
script:
- echo $CI_COMMIT_SHORT_SHA > ./static/commit.txt
- $IMAGE_BUILD --file $DOCKERFILE $BUILD_ARG --tag $IMAGE_NAME .
- $IMAGE_PUSH $IMAGE_NAME $IMAGE_NAME
- echo $CI_COMMIT_SHORT_SHA > ./static/commit.txt
......@@ -4,31 +4,43 @@ from apps.globals import WIDTH
VERSION = "1.0.0"
parameters = {
"network": None,
"station": None,
"location": None,
"channel": None,
"net": "*",
"sta": "*",
"loc": "*",
"cha": "*",
"time": None,
"height": HEIGHT,
"width": WIDTH,
"color": PLOT_COLOR,
"degrees": "F",
"annotate": "T",
"minfreq": 0.00001,
"maxfreq": None,
"nfreq": 200,
"spacing": "log",
"units": "def",
"format": None,
"nodata": "204",
}
POST_PARAMS = [k for k in parameters.keys() if k not in ("net", "sta", "cha", "loc")]
class Parameters:
def __init__(self):
self.network = None
self.station = None
self.location = None
self.channel = None
self.net = "*"
self.sta = "*"
self.loc = "*"
self.cha = "*"
self.time = None
self.height = HEIGHT
self.width = WIDTH
self.color = PLOT_COLOR
self.degrees = "F"
self.annotate = "T"
self.minfreq = 0.00001
self.maxfreq = None
self.nfreq = 200
self.spacing = "log"
self.units = "def"
self.format = None
self.nodata = "204"
self.constraints = {
"booleans": ["annotate", "degrees"],
"floats": ["minfreq", "maxfreq"],
"not_none": ["format"],
}
def todict(self):
return self.__dict__
POST_PARAMS = [
k for k in Parameters().todict().keys() if k not in ("net", "sta", "cha", "loc")
]
ALIAS = [
("network", "net"),
......@@ -36,7 +48,3 @@ ALIAS = [
("location", "loc"),
("channel", "cha"),
]
BOOLEANS = ["annotate", "degrees"]
FLOATS = ["minfreq", "maxfreq"]
NOT_NONE = ["format"]
......@@ -299,7 +299,7 @@ def get_output(param_dic_list):
return error_500(Error.UNSPECIFIED)
tmp.seek(0)
headers = {"Content-Disposition": f"attachment; filename=evalresp-resif.zip"}
headers = {"Content-Disposition": "attachment; filename=evalresp-resif.zip"}
response = make_response(tmp.read(), headers)
response.headers["Content-type"] = "application/x-zip-compressed"
response.mimetype = "application/x-zip-comp"
......
......@@ -5,10 +5,7 @@ from copy import copy
from multiprocessing import Process, Queue
from apps.constants import ALIAS
from apps.constants import BOOLEANS
from apps.constants import FLOATS
from apps.constants import NOT_NONE
from apps.constants import parameters
from apps.constants import Parameters
from apps.globals import AREA_MAX
from apps.globals import Error
from apps.globals import HEIGHT_MIN, HEIGHT_MAX
......@@ -36,9 +33,7 @@ from apps.utils import is_valid_output
def check_parameters(params):
# check base parameters
(params, error) = check_base_parameters(
params, MAX_DAYS, NOT_NONE, BOOLEANS, FLOATS
)
(params, error) = check_base_parameters(params, MAX_DAYS)
if error["code"] != 200:
return (params, error)
......@@ -111,7 +106,7 @@ def check_parameters(params):
def checks_get(request):
# get default parameters
params = copy(parameters)
params = Parameters().todict()
# check if the parameters are unknown
(p, result) = check_request(request, params, ALIAS)
......@@ -136,7 +131,7 @@ def checks_get(request):
def checks_post(params):
for key, val in parameters.items():
for key, val in Parameters().todict().items():
if key not in params:
params[key] = val
......
......@@ -155,32 +155,27 @@ def check_request(request, params, alias):
return (keys, {"msg": HTTP._200_, "details": Error.VALID_PARAM, "code": 200})
def check_base_parameters(
params, max_days=None, not_none=None, booleans=None, floats=None
):
def check_base_parameters(params, max_days=None):
# Search for missing mandatory parameters
if not_none is not None:
for key in not_none:
if params[key] is None:
return error_param(params, Error.MISSING + key)
for key in params["constraints"]["not_none"]:
if params[key] is None:
return error_param(params, Error.MISSING + key)
# Boolean parameter validations
if booleans is not None:
for key in booleans:
val = params[key]
if not is_valid_bool_string(val):
return error_param(params, f"Invalid {key} value: {val} {Error.BOOL}.")
params[key] = True if val.lower() in STRING_TRUE else False
for key in params["constraints"]["booleans"]:
val = params[key]
if not is_valid_bool_string(val):
return error_param(params, f"Invalid {key} value: {val} {Error.BOOL}.")
params[key] = True if val.lower() in STRING_TRUE else False
# Float parameter validations
if floats is not None:
for key in floats:
val = params[key]
if is_valid_float(val):
params[key] = float(val)
elif val is not None:
return error_param(params, f"Invalid {key} value: {val}")
for key in params["constraints"]["floats"]:
val = params[key]
if is_valid_float(val):
params[key] = float(val)
elif val is not None:
return error_param(params, f"Invalid {key} value: {val}")
# Station validations
network = params["network"].split(",")
......
......@@ -119,10 +119,14 @@ class MyTest(unittest.TestCase):
p1["nfreq"] = 200
p1["units"] = "def"
p1["degrees"] = "F"
p1["annotate"] = "T"
p1["spacing"] = "log"
p1["format"] = "cs"
p1["nodata"] = "204"
p1["constraints"] = {
"booleans": ["annotate", "degrees"],
"floats": ["minfreq", "maxfreq"],
"not_none": ["format"],
}
valid_param = {"msg": HTTP._200_, "details": Error.VALID_PARAM, "code": 200}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment