Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit 7bd6a5bf authored by Jerome Touvier's avatar Jerome Touvier
Browse files

code refactoring

parent 56b3fd67
Pipeline #46879 passed with stage
in 49 seconds
......@@ -21,6 +21,6 @@ build:
DOCKERFILE: Dockerfile
IMAGE_NAME: $CI_REGISTRY_IMAGE/ws-statistics:$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
VERSION = "1.0.0"
parameters = {
"network": None,
"station": None,
"location": None,
"channel": None,
"starttime": None,
"endtime": None,
"net": "*",
"sta": "*",
"loc": "*",
"cha": "*",
"start": None,
"end": None,
"country": "*",
"year": None,
"type": "all",
"request": None,
"media": "all",
"granularity": None,
"orderby": "nslc",
"nodata": "204",
"format": "text",
}
class Parameters:
def __init__(self):
self.network = None
self.station = None
self.location = None
self.channel = None
self.starttime = None
self.endtime = None
self.net = "*"
self.sta = "*"
self.loc = "*"
self.cha = "*"
self.start = None
self.end = None
self.country = "*"
self.year = None
self.type = "all"
self.request = None
self.media = "all"
self.granularity = None
self.orderby = "nslc"
self.format = None
self.nodata = "204"
self.constraints = {
"booleans": [],
"floats": [],
"not_none": ["request"],
}
def todict(self):
return self.__dict__
ALIAS = [
......@@ -33,5 +42,3 @@ ALIAS = [
("starttime", "start"),
("endtime", "end"),
]
NOT_NONE = ["request"]
......@@ -200,13 +200,11 @@ def get_output(params):
try:
tic = time.time()
data = collect_data(params)
if data is None:
return data
if not data:
code = params["nodata"]
return error_request(msg=f"HTTP._{code}_", details=Error.NODATA, code=code)
nrows = len(data)
logging.info(f"Number of collected rows: {nrows}")
......
......@@ -5,8 +5,7 @@ from copy import copy
from multiprocessing import Process, Queue
from apps.constants import ALIAS
from apps.constants import NOT_NONE
from apps.constants import parameters
from apps.constants import Parameters
from apps.globals import Error
from apps.globals import HTTP
from apps.globals import TIMEOUT
......@@ -29,7 +28,7 @@ from apps.utils import is_valid_year
def check_parameters(params):
# check base parameters
(params, error) = check_base_parameters(params, not_none=NOT_NONE)
(params, error) = check_base_parameters(params)
if error["code"] != 200:
return (params, error)
......@@ -101,7 +100,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)
......
......@@ -178,32 +178,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
if params["network"].lower() == "temporary":
......
......@@ -124,6 +124,11 @@ class MyTest(unittest.TestCase):
p1["request"] = ""
p1["protocol"] = ""
p1["nodata"] = "204"
p1["constraints"] = {
"booleans": [],
"floats": [],
"not_none": ["request"],
}
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