diff --git a/sf_daq_broker/broker_manager.py b/sf_daq_broker/broker_manager.py index 8fdc3fe..8b8e508 100644 --- a/sf_daq_broker/broker_manager.py +++ b/sf_daq_broker/broker_manager.py @@ -33,11 +33,10 @@ class BrokerManager: def close_pgroup_writing(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "pgroup") beamline = get_beamline(remote_ip) - validate.request_has_pgroup(request) pgroup = request["pgroup"] path_to_pgroup = f"/sf/{beamline}/data/{pgroup}/raw/" @@ -73,18 +72,18 @@ class BrokerManager: def set_pvlist(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "pv_list") beamline = get_beamline(remote_ip) config_file = f"/home/dbe/service_configs/sf.{beamline}.epics_buffer.json" validate.epics_config_file_exists(config_file, beamline) - pv_list = request.get("pv_list", []) + pv_list = request["pv_list"] pv_list = list(dict.fromkeys(pv_list)) config_epics = { - "pulse_id_pv": "SLAAR11-LTIM01-EVR0:RX-PULSEID", + "pulse_id_pv": "SLAAR11-LTIM01-EVR0:RX-PULSEID", #TODO: this should be matched to the BL "pv_list": pv_list } @@ -103,11 +102,10 @@ class BrokerManager: def get_next_run_number(self, request, remote_ip, increment_run_number=True): - validate.request(request) + validate.request_has(request, "pgroup") beamline = get_beamline(remote_ip) - validate.request_has_pgroup(request) pgroup = request["pgroup"] path_to_pgroup = f"/sf/{beamline}/data/{pgroup}/raw/" @@ -123,15 +121,14 @@ class BrokerManager: def power_on_detector(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "detector_name") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - detector_name = request.get("detector_name", None) - validate.detector_name(detector_name) + detector_name = request["detector_name"] validate.detector_name_in_allowed_detectors_beamline(detector_name, allowed_detectors_beamline, beamline) @@ -208,7 +205,7 @@ class BrokerManager: def take_pedestal(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "pgroup", "detectors") beamline = get_beamline(remote_ip) @@ -217,14 +214,11 @@ class BrokerManager: rate_multiplicator = request.get("rate_multiplicator", 1) - validate.request_has_detectors(request) - detectors = list(request["detectors"]) validate.detectors(detectors) validate.all_detector_names_in_allowed_detectors_beamline(detectors, allowed_detectors_beamline, beamline) - validate.request_has_pgroup(request) pgroup = request["pgroup"] path_to_pgroup = f"/sf/{beamline}/data/{pgroup}/raw/" @@ -266,7 +260,7 @@ class BrokerManager: "path_to_pgroup": path_to_pgroup, "run_info_directory": run_info_directory, "output_file_prefix": f"{full_path}/{pedestal_name}", - "directory_name": request.get("directory_name"), + "directory_name": directory_name, "request_time": str(request_time) } @@ -288,14 +282,12 @@ class BrokerManager: def retrieve_from_buffers(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "pgroup", "start_pulseid", "stop_pulseid") beamline = get_beamline(remote_ip) - validate.request_has_pgroup(request) pgroup = request["pgroup"] - validate.request_has_pulseids(request) validate.request_has_integer_pulseids(request) start_pulse_id = request["start_pulseid"] diff --git a/sf_daq_broker/broker_manager_slow.py b/sf_daq_broker/broker_manager_slow.py index 4e3afef..6326d36 100644 --- a/sf_daq_broker/broker_manager_slow.py +++ b/sf_daq_broker/broker_manager_slow.py @@ -36,15 +36,14 @@ conv_detector_gain_settings_reverse = dict(zip(conv_detector_gain_settings.value class DetectorManager: def get_detector_settings(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "detector_name") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - detector_name = request.get("detector_name", None) - validate.detector_name(detector_name) + detector_name = request["detector_name"] validate.detector_name_in_allowed_detectors_beamline(detector_name, allowed_detectors_beamline, beamline) @@ -67,15 +66,14 @@ class DetectorManager: def set_detector_settings(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "detector_name") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - detector_name = request.get("detector_name", None) - validate.detector_name(detector_name) + detector_name = request["detector_name"] validate.detector_name_in_allowed_detectors_beamline(detector_name, allowed_detectors_beamline, beamline) @@ -133,14 +131,13 @@ class DetectorManager: def copy_user_files(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "pgroup", "run_number") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - validate.request_has_pgroup(request) pgroup = request["pgroup"] path_to_pgroup = f"/sf/{beamline}/data/{pgroup}/raw/" @@ -150,8 +147,7 @@ class DetectorManager: validate.directory_exists(daq_directory) validate.pgroup_is_not_closed_yet(daq_directory, path_to_pgroup) - run_number = request.get("run_number", None) - validate.request_has_run_number(run_number) + run_number = request["run_number"] list_data_directories_run = glob(f"{path_to_pgroup}/run{run_number:04}*") validate.run_dir_exists(list_data_directories_run, run_number) @@ -188,15 +184,14 @@ class DetectorManager: def get_dap_settings(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "detector_name") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - detector_name = request.get("detector_name", None) - validate.detector_name(detector_name) + detector_name = request["detector_name"] validate.detector_name_in_allowed_detectors_beamline(detector_name, allowed_detectors_beamline, beamline) @@ -208,15 +203,14 @@ class DetectorManager: def set_dap_settings(self, request, remote_ip): - validate.request(request) + validate.request_has(request, "detector_name") beamline = get_beamline(remote_ip) allowed_detectors_beamline = configured_detectors_for_beamline(beamline) validate.allowed_detectors_beamline(allowed_detectors_beamline) - detector_name = request.get("detector_name", None) - validate.detector_name(detector_name) + detector_name = request["detector_name"] validate.detector_name_in_allowed_detectors_beamline(detector_name, allowed_detectors_beamline, beamline) diff --git a/sf_daq_broker/validate.py b/sf_daq_broker/validate.py index cc47eea..698f202 100644 --- a/sf_daq_broker/validate.py +++ b/sf_daq_broker/validate.py @@ -10,20 +10,12 @@ ALLOWED_RATE_MULTIPLICATORS = [1, 2, 4, 8, 10, 20, 40, 50, 100] #TEMPL_MISSING_PARAM = "no {what} provided in the request parameters" -# just check truthiness - -def request(req): - if not req: - raise RuntimeError("no request parameters provided") +## just check truthiness def allowed_detectors_beamline(adb): if not adb: raise RuntimeError("no detectors configured for this beamline") -def detector_name(dn): - if not dn: - raise RuntimeError('no "detector_name" provided in the request parameters') - def detectors(ds): if not ds: raise RuntimeError('no "detectors" provided in the request parameters') @@ -43,14 +35,19 @@ def detectors(ds): # check more complex things +def request_has(req, *args): + if not req: + raise RuntimeError("no request parameters provided") + + for i in args: + if i not in req: + raise RuntimeError(f'no "{i}" provided in the request parameters') + + def request_is_empty(req): if req: raise RuntimeError(f"this endpoint does not accept request parameters but received {req}") -def request_has_pgroup(req): - if "pgroup" not in req: - raise RuntimeError('no "pgroup" provided in the request parameters') - def path_to_pgroup_exists(ptp): if not os.path.exists(ptp): raise RuntimeError(f"pgroup directory {ptp} not reachable") @@ -75,18 +72,6 @@ def all_detector_names_in_allowed_detectors_beamline(dns, adb, bl): for dn in dns: detector_name_in_allowed_detectors_beamline(dn, adb, bl) -def request_has_detectors(req): - if "detectors" not in req: - raise RuntimeError('no "detectors" provided in the request parameters') - -def request_has_pulseids(req): - helper_request_has_pulseid(req, "start_pulseid") - helper_request_has_pulseid(req, "stop_pulseid") - -def helper_request_has_pulseid(req, key): - if key not in req: - raise RuntimeError(f'no "{key}" provided in the request parameters') - def allowed_pulseid_range(pid_start, pid_stop): delta = pid_stop - pid_start if delta > MAX_PULSEID_DELTA: @@ -115,10 +100,6 @@ def dap_parameters_file_exists(dpf): if not os.path.exists(dpf): raise RuntimeError(f"DAP parameter file {dpf} does not exist") -def request_has_run_number(rn): - if rn is None: - raise RuntimeError('no "run_number" provided in the request parameters') - def run_dir_exists(lddr, rn): if not lddr: raise RuntimeError(f"run {rn:04} does not exist in this pgroup")