From 69d7af6e2dc9a91393bc0b8db81744f696a04d9a Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Tue, 10 Nov 2026 14:57:28 +0000 Subject: [PATCH 01/12] restart working --- BlockServer/core/ioc_control.py | 4 ++- BlockServer/epics/gateway.py | 4 +-- DatabaseServer/database_server.py | 28 +++++++++++++++-- block_server.py | 52 +++++++++++++++++++++++++++++-- 4 files changed, 80 insertions(+), 8 deletions(-) diff --git a/BlockServer/core/ioc_control.py b/BlockServer/core/ioc_control.py index a10de230..df43749b 100644 --- a/BlockServer/core/ioc_control.py +++ b/BlockServer/core/ioc_control.py @@ -13,10 +13,12 @@ # along with this program; if not, you can obtain a copy from # https://www.eclipse.org/org/documents/epl-v10.php or # http://opensource.org/licenses/eclipse-1.0.php - +import sys from time import sleep, time from typing import List +from server_common.channel_access import verify_manager_mode, ChannelAccess + from BlockServer.alarm.load_alarm_config import AlarmConfigLoader from BlockServer.epics.procserv_utils import ProcServWrapper from server_common.constants import IOCS_NOT_TO_STOP diff --git a/BlockServer/epics/gateway.py b/BlockServer/epics/gateway.py index ff658171..9ec94a77 100644 --- a/BlockServer/epics/gateway.py +++ b/BlockServer/epics/gateway.py @@ -121,7 +121,7 @@ def exists(self) -> bool: """ return ChannelAccess.caget(self._gateway_prefix + "pvtotal") is not None - def _reload(self) -> None: + def reload(self) -> None: print_and_log("Reloading gateway") try: # Have to wait after put as the gateway does not do completion callbacks @@ -194,4 +194,4 @@ def set_new_aliases(self, blocks, configures_block_gateway: bool, config_dir: st self._generate_alias_file(blocks) else: self._generate_alias_file(blocks) - self._reload() + self.reload() diff --git a/DatabaseServer/database_server.py b/DatabaseServer/database_server.py index 07665252..78363042 100644 --- a/DatabaseServer/database_server.py +++ b/DatabaseServer/database_server.py @@ -25,7 +25,7 @@ from functools import partial from threading import RLock, Thread from time import sleep -from typing import Callable, Literal +from typing import Callable from pcaspy import Driver @@ -39,6 +39,7 @@ from DatabaseServer.options_holder import OptionsHolder from DatabaseServer.options_loader import OptionsLoader from DatabaseServer.procserv_utils import ProcServWrapper +from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer from server_common.constants import IOCS_NOT_TO_STOP from server_common.ioc_data import IOCData @@ -73,6 +74,8 @@ class DatabaseServer(Driver): The class for handling all the static PV access and monitors etc. """ + RESTART_PV = {DbPVNames.RESTART: {"type": "int"}} + def __init__( self, ca_server: CAServer | MockCAServer, @@ -209,7 +212,7 @@ def read(self, reason: str) -> bytes: else self.getParam(reason) ) - def write(self, reason: str, value: str) -> Literal[True]: + def write(self, reason: str, value: str) -> bool: """ A method called by SimpleServer when a PV is written to the DatabaseServer over Channel Access. @@ -219,8 +222,11 @@ def write(self, reason: str, value: str) -> Literal[True]: value: The data being written to the 'reason' PV Returns: - True + True if the value was accepted, False if the put was rejected """ + if reason == DbPVNames.RESTART: + return self._restart() + try: if reason == "ED:RBNUMBER:SP": self._ed.update_experiment_id(value) @@ -239,6 +245,21 @@ def write(self, reason: str, value: str) -> Literal[True]: self.setParam(reason, value) return True + def _restart(self) -> bool: + """ + Exits the process so that the parent procServ restarts it. os._exit is used rather than + sys.exit as non-daemon threads would otherwise keep the process alive. + + Returns: + False if manager mode is not enabled (the put is rejected); otherwise does not return + """ + verify_manager_mode(message="Attempting to restart the database server") + print_and_log("Restarting Database Server process", INFO_MSG, LOG_TARGET) + + sys.stdout.flush() + sys.stderr.flush() + os._exit(0) + def _update_ioc_monitors(self) -> None: """ Updates all the PVs that hold information on the IOCS and their associated PVs. @@ -434,6 +455,7 @@ def _get_iocs_not_to_stop() -> list: SERVER = CAServer(BLOCKSERVER_PREFIX) SERVER.createPV(BLOCKSERVER_PREFIX, DatabaseServer.generate_pv_info()) + SERVER.createPV(BLOCKSERVER_PREFIX, DatabaseServer.RESTART_PV) SERVER.createPV(MACROS["$(MYPVPREFIX)"], ExpData.EDPV) SERVER.createPV(MACROS["$(MYPVPREFIX)"], MoxaData.MDPV) diff --git a/block_server.py b/block_server.py index be3e2eb9..3a472654 100644 --- a/block_server.py +++ b/block_server.py @@ -20,7 +20,7 @@ import os import sys import traceback -from typing import TYPE_CHECKING, Any, Dict +from typing import TYPE_CHECKING, Any, Dict, List from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer @@ -41,7 +41,7 @@ from server_common.channel_access import ChannelAccess from server_common.file_path_manager import FILEPATH_MANAGER from server_common.helpers import BLOCK_PREFIX, CONTROL_SYSTEM_PREFIX, MACROS, PVPREFIX_MACRO -from server_common.pv_names import BlockserverPVNames +from server_common.pv_names import BlockserverPVNames, DatabasePVNames from server_common.utilities import ( char_waveform, convert_from_json, @@ -117,6 +117,7 @@ "value": CURR_CONFIG_NAME_SEVR_VALUE, "enums": ["NO_ALARM"], }, + BlockserverPVNames.RESTART_PR_IOC: char_waveform(1000), } @@ -357,6 +358,14 @@ def write(self, reason: str, value: str) -> bool: # type: ignore "RESTART_IOCS", ) ) + elif reason == BlockserverPVNames.RESTART_PR_IOC: + self.write_queue.put( + ( + self.restart_protected_ioc, + (convert_from_json(data), True), + "PROTECTED_RESTART_IOC", + ) + ) elif reason == BlockserverPVNames.SET_CURR_CONFIG_DETAILS: self.write_queue.put((self._set_curr_config, (data,), "SETTING_CONFIG")) elif reason == BlockserverPVNames.SAVE_NEW_CONFIG: @@ -398,6 +407,45 @@ def write(self, reason: str, value: str) -> bool: # type: ignore self.setParam(reason, value) return status + def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> None: + """Restart protected IOCs. + + Args: + iocs (list): The protected IOCs to restart + reapply_auto (bool): Whether to reapply auto restart settings automatically + """ + + for ioc in iocs: + # auto = self._ioc_control.get_autorestart(ioc) + + #if not ioc.startswith(IOCS_NOT_TO_STOP): + #continue + # TODO other checks + + verify_manager_mode( + message=f"Attempting to restart a protected IOC')", + ) + + match ioc: + case "BLOCKSVR": + print_and_log("Restarting BlockServer process") + sys.stdout.flush() + sys.stderr.flush() + os._exit(0) + case "GWBLOCK": + self._gateway.reload() + case "DBSRV": + ChannelAccess.caput( + CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART, 1, + ) + case _: + self._ioc_control.restart_ioc(ioc, force=True) + + # Reapply auto-restart settings + #if reapply_auto: + #self._ioc_control.waitfor_running(ioc) + #self._ioc_control.set_autorestart(ioc, auto) + def load_last_config(self) -> None: """Loads the last configuration used. From e03bcc3ac9105b20d8fb4f0970560d7fef399840 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Wed, 11 Nov 2026 10:08:15 +0000 Subject: [PATCH 02/12] rename pv --- block_server.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/block_server.py b/block_server.py index 3a472654..b7f5149a 100644 --- a/block_server.py +++ b/block_server.py @@ -24,6 +24,7 @@ from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer +from server_common.constants import IOCS_NOT_TO_STOP sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) @@ -117,7 +118,7 @@ "value": CURR_CONFIG_NAME_SEVR_VALUE, "enums": ["NO_ALARM"], }, - BlockserverPVNames.RESTART_PR_IOC: char_waveform(1000), + BlockserverPVNames.RESTART_PROTECTED_IOC: char_waveform(1000), } @@ -158,6 +159,7 @@ def __init__(self, ca_server: CAServer) -> None: self.group_rules = GroupRules(self) self.config_desc = ConfigurationDescriptionRules(self) self.spangle_banner = json.dumps(ConfigurationFileManager.get_banner_config()) + self._service_iocs = tuple(set(IOCS_NOT_TO_STOP) - {"PSCTRL"}) # Connect to version control try: @@ -358,7 +360,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore "RESTART_IOCS", ) ) - elif reason == BlockserverPVNames.RESTART_PR_IOC: + elif reason == BlockserverPVNames.RESTART_PROTECTED_IOC: self.write_queue.put( ( self.restart_protected_ioc, @@ -416,11 +418,9 @@ def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> """ for ioc in iocs: - # auto = self._ioc_control.get_autorestart(ioc) - #if not ioc.startswith(IOCS_NOT_TO_STOP): - #continue - # TODO other checks + if not ioc.startswith(self._service_iocs): + continue verify_manager_mode( message=f"Attempting to restart a protected IOC')", @@ -441,11 +441,6 @@ def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> case _: self._ioc_control.restart_ioc(ioc, force=True) - # Reapply auto-restart settings - #if reapply_auto: - #self._ioc_control.waitfor_running(ioc) - #self._ioc_control.set_autorestart(ioc, auto) - def load_last_config(self) -> None: """Loads the last configuration used. From 96519ec4173448f8f17e61ad426025ccbd8bfb51 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Wed, 11 Nov 2026 12:14:55 +0000 Subject: [PATCH 03/12] name changes --- DatabaseServer/database_server.py | 17 +++++++---------- block_server.py | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/DatabaseServer/database_server.py b/DatabaseServer/database_server.py index 78363042..581ddbb7 100644 --- a/DatabaseServer/database_server.py +++ b/DatabaseServer/database_server.py @@ -25,7 +25,7 @@ from functools import partial from threading import RLock, Thread from time import sleep -from typing import Callable +from typing import Callable, Literal from pcaspy import Driver @@ -212,7 +212,7 @@ def read(self, reason: str) -> bytes: else self.getParam(reason) ) - def write(self, reason: str, value: str) -> bool: + def write(self, reason: str, value: str) -> Literal[True]: """ A method called by SimpleServer when a PV is written to the DatabaseServer over Channel Access. @@ -222,10 +222,11 @@ def write(self, reason: str, value: str) -> bool: value: The data being written to the 'reason' PV Returns: - True if the value was accepted, False if the put was rejected + True """ if reason == DbPVNames.RESTART: - return self._restart() + self._restart() + return True try: if reason == "ED:RBNUMBER:SP": @@ -245,13 +246,9 @@ def write(self, reason: str, value: str) -> bool: self.setParam(reason, value) return True - def _restart(self) -> bool: + def _restart(self) -> None: """ - Exits the process so that the parent procServ restarts it. os._exit is used rather than - sys.exit as non-daemon threads would otherwise keep the process alive. - - Returns: - False if manager mode is not enabled (the put is rejected); otherwise does not return + Exits the process so that the parent procServ restarts it. """ verify_manager_mode(message="Attempting to restart the database server") print_and_log("Restarting Database Server process", INFO_MSG, LOG_TARGET) diff --git a/block_server.py b/block_server.py index b7f5149a..e0ce9e4b 100644 --- a/block_server.py +++ b/block_server.py @@ -24,7 +24,7 @@ from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer -from server_common.constants import IOCS_NOT_TO_STOP +from server_common.constants import SERVICE_IOCS sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) @@ -118,7 +118,7 @@ "value": CURR_CONFIG_NAME_SEVR_VALUE, "enums": ["NO_ALARM"], }, - BlockserverPVNames.RESTART_PROTECTED_IOC: char_waveform(1000), + BlockserverPVNames.RESTART_SERVICE_IOC: char_waveform(1000), } @@ -159,7 +159,7 @@ def __init__(self, ca_server: CAServer) -> None: self.group_rules = GroupRules(self) self.config_desc = ConfigurationDescriptionRules(self) self.spangle_banner = json.dumps(ConfigurationFileManager.get_banner_config()) - self._service_iocs = tuple(set(IOCS_NOT_TO_STOP) - {"PSCTRL"}) + self._service_iocs = SERVICE_IOCS # Connect to version control try: @@ -360,12 +360,12 @@ def write(self, reason: str, value: str) -> bool: # type: ignore "RESTART_IOCS", ) ) - elif reason == BlockserverPVNames.RESTART_PROTECTED_IOC: + elif reason == BlockserverPVNames.RESTART_SERVICE_IOC: self.write_queue.put( ( - self.restart_protected_ioc, + self.restart_service_ioc, (convert_from_json(data), True), - "PROTECTED_RESTART_IOC", + "RESTART_SERVICE_IOC", ) ) elif reason == BlockserverPVNames.SET_CURR_CONFIG_DETAILS: @@ -409,11 +409,11 @@ def write(self, reason: str, value: str) -> bool: # type: ignore self.setParam(reason, value) return status - def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> None: - """Restart protected IOCs. + def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> None: + """Restart service IOCs. Args: - iocs (list): The protected IOCs to restart + iocs (list): The service IOCs to restart reapply_auto (bool): Whether to reapply auto restart settings automatically """ @@ -423,7 +423,7 @@ def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> continue verify_manager_mode( - message=f"Attempting to restart a protected IOC')", + message=f"Attempting to restart a service IOC')", ) match ioc: @@ -434,7 +434,7 @@ def restart_protected_ioc(self, iocs: List[str], reapply_auto: bool = False) -> os._exit(0) case "GWBLOCK": self._gateway.reload() - case "DBSRV": + case "DBSVR": ChannelAccess.caput( CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART, 1, ) From a3a7128bc2b1a7c14980a59e7f590b5636120c00 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Wed, 11 Nov 2026 15:31:21 +0000 Subject: [PATCH 04/12] make restart function for blockserver --- block_server.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/block_server.py b/block_server.py index e0ce9e4b..7fb6a927 100644 --- a/block_server.py +++ b/block_server.py @@ -428,10 +428,7 @@ def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> No match ioc: case "BLOCKSVR": - print_and_log("Restarting BlockServer process") - sys.stdout.flush() - sys.stderr.flush() - os._exit(0) + self._restart() case "GWBLOCK": self._gateway.reload() case "DBSVR": @@ -441,6 +438,15 @@ def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> No case _: self._ioc_control.restart_ioc(ioc, force=True) + def _restart(self) -> None: + """Restart the blockserver. + + Assumes it is set up to autorestart by its parent procserv process.""" + print_and_log("Restarting BlockServer process") + sys.stdout.flush() + sys.stderr.flush() + os._exit(0) + def load_last_config(self) -> None: """Loads the last configuration used. From 29cf692b2c88a53cd67870dad8f5285f00a9a6c0 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Wed, 11 Nov 2026 15:50:22 +0000 Subject: [PATCH 05/12] update reload name --- BlockServer/core/ioc_control.py | 3 --- BlockServer/test_modules/test_epics_gateway.py | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/BlockServer/core/ioc_control.py b/BlockServer/core/ioc_control.py index df43749b..27f492ba 100644 --- a/BlockServer/core/ioc_control.py +++ b/BlockServer/core/ioc_control.py @@ -13,12 +13,9 @@ # along with this program; if not, you can obtain a copy from # https://www.eclipse.org/org/documents/epl-v10.php or # http://opensource.org/licenses/eclipse-1.0.php -import sys from time import sleep, time from typing import List -from server_common.channel_access import verify_manager_mode, ChannelAccess - from BlockServer.alarm.load_alarm_config import AlarmConfigLoader from BlockServer.epics.procserv_utils import ProcServWrapper from server_common.constants import IOCS_NOT_TO_STOP diff --git a/BlockServer/test_modules/test_epics_gateway.py b/BlockServer/test_modules/test_epics_gateway.py index db41e9d3..cc032b94 100644 --- a/BlockServer/test_modules/test_epics_gateway.py +++ b/BlockServer/test_modules/test_epics_gateway.py @@ -167,7 +167,7 @@ def test_GIVEN_local_lowercase_PV_without_suffix_or_SP_WHEN_alias_generated_THEN self._assert_lines_correct(lines, expected_lines) @patch("BlockServer.epics.gateway.copyfile") - @patch("BlockServer.epics.gateway.Gateway._reload") + @patch("BlockServer.epics.gateway.Gateway.reload") @patch("builtins.open", new_callable=mock_open, mock=FileStub) def test_GIVEN_configuration_has_pvlist_WHEN_set_new_aliases_THEN_pvlist_copied( self, mock_file, reload_mock, copyfile_mock @@ -186,7 +186,7 @@ def test_GIVEN_configuration_has_pvlist_WHEN_set_new_aliases_THEN_pvlist_copied( assert_that(mock_file.file_contents, empty()) @patch("BlockServer.epics.gateway.copyfile") - @patch("BlockServer.epics.gateway.Gateway._reload") + @patch("BlockServer.epics.gateway.Gateway.reload") @patch("builtins.open", new_callable=mock_open, mock=FileStub) def test_GIVEN_configuration_claims_has_pvlist_BUT_does_not_WHEN_set_new_aliases_THEN_pvlist_copied( self, mock_file, reload_mock, copyfile_mock From 94ba9970098bdcd3ee62b70fb3e9ebfe50caa66a Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 12 Nov 2026 14:08:42 +0000 Subject: [PATCH 06/12] ruff/pyright --- BlockServer/core/ioc_control.py | 44 ++-- BlockServer/epics/gateway.py | 35 ++- BlockServer/epics/procserv_utils.py | 6 +- .../test_modules/test_epics_gateway.py | 86 ++++--- DatabaseServer/database_server.py | 110 +++++---- DatabaseServer/exp_data.py | 44 ++-- DatabaseServer/test_modules/test_exp_data.py | 5 +- block_server.py | 217 ++++++++++++------ 8 files changed, 350 insertions(+), 197 deletions(-) diff --git a/BlockServer/core/ioc_control.py b/BlockServer/core/ioc_control.py index 27f492ba..20d18f38 100644 --- a/BlockServer/core/ioc_control.py +++ b/BlockServer/core/ioc_control.py @@ -14,13 +14,14 @@ # https://www.eclipse.org/org/documents/epl-v10.php or # http://opensource.org/licenses/eclipse-1.0.php from time import sleep, time -from typing import List -from BlockServer.alarm.load_alarm_config import AlarmConfigLoader -from BlockServer.epics.procserv_utils import ProcServWrapper +from server_common.common_exceptions import MaxAttemptsExceededException from server_common.constants import IOCS_NOT_TO_STOP from server_common.utilities import print_and_log +from BlockServer.alarm.load_alarm_config import AlarmConfigLoader +from BlockServer.epics.procserv_utils import ProcServWrapper + class IocControl: """A class for starting, stopping and restarting IOCs""" @@ -44,10 +45,12 @@ def start_ioc(self, ioc: str, restart_alarm_server: bool = True) -> None: self._proc.start_ioc(ioc) if ioc != "ALARM" and restart_alarm_server: AlarmConfigLoader.restart_alarm_server(self) - except Exception as err: + except OSError as err: print_and_log(f"Could not start IOC {ioc}: {err}", "MAJOR") - def restart_ioc(self, ioc: str, force: bool = False, restart_alarm_server: bool = True) -> None: + def restart_ioc( + self, ioc: str, force: bool = False, restart_alarm_server: bool = True + ) -> None: """Restart an IOC. Note: restarting an IOC automatically sets the IOC to auto-restart, @@ -65,7 +68,7 @@ def restart_ioc(self, ioc: str, force: bool = False, restart_alarm_server: bool self._proc.restart_ioc(ioc) if ioc != "ALARM" and restart_alarm_server: AlarmConfigLoader.restart_alarm_server(self) - except Exception as err: + except OSError as err: print_and_log(f"Could not restart IOC {ioc}: {err}", "MAJOR") def stop_ioc(self, ioc: str, force: bool = False) -> None: @@ -82,7 +85,7 @@ def stop_ioc(self, ioc: str, force: bool = False) -> None: self._proc.stop_ioc(ioc) if ioc != "ALARM": AlarmConfigLoader.restart_alarm_server(self) - except Exception as err: + except OSError as err: print_and_log(f"Could not stop IOC {ioc}: {err}", "MAJOR") def get_ioc_status(self, ioc: str) -> str: @@ -107,7 +110,7 @@ def ioc_restart_pending(self, ioc: str) -> bool: """ return self._proc.ioc_restart_pending(ioc) - def start_iocs(self, iocs: List[str]) -> None: + def start_iocs(self, iocs: list[str]) -> None: """Start a number of IOCs. Args: @@ -116,14 +119,14 @@ def start_iocs(self, iocs: List[str]) -> None: for ioc in iocs: self.start_ioc(ioc) - def restart_iocs(self, iocs: List[str], reapply_auto: bool = False) -> None: + def restart_iocs(self, iocs: list[str], reapply_auto: bool = False) -> None: """Restart a number of IOCs. Args: iocs (list): The IOCs to restart reapply_auto (bool): Whether to reapply auto restart settings automatically """ - auto = dict() + auto = {} for ioc in iocs: auto[ioc] = self.get_autorestart(ioc) self.restart_ioc(ioc) @@ -134,7 +137,7 @@ def restart_iocs(self, iocs: List[str], reapply_auto: bool = False) -> None: self.waitfor_running(ioc) self.set_autorestart(ioc, auto[ioc]) - def stop_iocs(self, iocs: List[str]) -> None: + def stop_iocs(self, iocs: list[str]) -> None: """Stop a number of IOCs. Args: @@ -155,7 +158,7 @@ def ioc_exists(self, ioc: str) -> bool: try: self.get_ioc_status(ioc) return True - except Exception: + except OSError: return False def set_autorestart(self, ioc: str, enable: bool) -> None: @@ -176,8 +179,10 @@ def set_autorestart(self, ioc: str, enable: bool) -> None: return print_and_log(f"Auto-restart for IOC {ioc} is already {enable}") else: - print_and_log(f"Auto-restart for IOC {ioc} unchanged as IOC is not running") - except Exception as err: + print_and_log( + f"Auto-restart for IOC {ioc} unchanged as IOC is not running" + ) + except MaxAttemptsExceededException as err: print_and_log(f"Could not set auto-restart IOC {ioc}: {err}", "MAJOR") def get_autorestart(self, ioc: str) -> bool: @@ -191,8 +196,11 @@ def get_autorestart(self, ioc: str) -> bool: """ try: return self._proc.get_autorestart(ioc) - except Exception as err: - print_and_log(f"Could not get auto-restart setting for IOC {ioc}: {err}", "MAJOR") + except MaxAttemptsExceededException as err: + print_and_log( + f"Could not get auto-restart setting for IOC {ioc}: {err}", "MAJOR" + ) + return False def waitfor_running(self, ioc: str, timeout: int = 15) -> None: """Waits for the IOC to start running. @@ -206,7 +214,9 @@ def waitfor_running(self, ioc: str, timeout: int = 15) -> None: """ if self.ioc_exists(ioc): start = time() - while self.ioc_restart_pending(ioc) or self.get_ioc_status(ioc) != "RUNNING": + while ( + self.ioc_restart_pending(ioc) or self.get_ioc_status(ioc) != "RUNNING" + ): sleep(0.5) if time() - start >= timeout: print_and_log( diff --git a/BlockServer/epics/gateway.py b/BlockServer/epics/gateway.py index 9ec94a77..24f668ad 100644 --- a/BlockServer/epics/gateway.py +++ b/BlockServer/epics/gateway.py @@ -38,13 +38,16 @@ {0}CS:SB:[^:]*:[ADR]C:.* DENY ## ignore any request not related to local blocks or gateway itself !{0}CS:\(SB\|GATEWAY\):.* DENY -""" # noqa: E501 +""" def build_block_alias_lines( - full_block_pv: str, pv_suffix: str, underlying_pv: str, include_comments: bool = True + full_block_pv: str, + pv_suffix: str | None, + underlying_pv: str, + include_comments: bool = True, ) -> list[str]: - lines = list() + lines = [] if underlying_pv.endswith(":SP"): # The block points at a setpoint if include_comments: @@ -53,7 +56,7 @@ def build_block_alias_lines( "genie_python will append an additional :SP" ) - full_block_pv = r"{}\(:SP\)?".format(full_block_pv) + full_block_pv = rf"{full_block_pv}\(:SP\)?" # Pattern match is for picking up any extras like :RBV or .EGU lines.append(f"{full_block_pv}\\([.:].*\\) ALIAS {underlying_pv}\\2") @@ -131,7 +134,7 @@ def reload(self) -> None: while ChannelAccess.caget(self._gateway_prefix + "newAsFlag") == 1: time.sleep(1) print_and_log("Gateway reloaded") - except Exception as err: + except OSError as err: print_and_log(f"Problem with reloading the gateway {err}") def _generate_alias_file(self, blocks=None) -> None: @@ -140,14 +143,16 @@ def _generate_alias_file(self, blocks=None) -> None: header = ALIAS_HEADER.format(self._inst_prefix) f.write(header) if blocks is not None: - for name, value in blocks.items(): + for value in blocks.values(): lines = self.generate_alias(value.name, value.pv, value.local) f.write("\n".join(lines) + "\n") f.write(ALIAS_FOOTER.format(self._inst_prefix)) # Add a blank line at the end! f.write("\n") - def generate_alias(self, block_name: str, underlying_pv: str, local: bool) -> list[str]: + def generate_alias( + self, block_name: str, underlying_pv: str, local: bool + ) -> list[str]: print_and_log(f"Creating block: {block_name} for {underlying_pv}") underlying_pv = underlying_pv.replace(".VAL", "") @@ -167,15 +172,21 @@ def generate_alias(self, block_name: str, underlying_pv: str, local: bool) -> li # Create a case insensitive alias so clients don't have to worry about getting case right if full_block_pv != full_block_pv.upper(): - lines.append("## Add full caps equivalent so clients need not be case sensitive") + lines.append( + "## Add full caps equivalent so clients need not be case sensitive" + ) lines.extend( - build_block_alias_lines(full_block_pv.upper(), pv_suffix, underlying_pv, False) + build_block_alias_lines( + full_block_pv.upper(), pv_suffix, underlying_pv, False + ) ) lines.append("") # New line to seperate out each block return lines - def set_new_aliases(self, blocks, configures_block_gateway: bool, config_dir: str) -> None: + def set_new_aliases( + self, blocks, configures_block_gateway: bool, config_dir: str + ) -> None: """Creates the aliases for the blocks and restarts the gateway. Args: @@ -187,10 +198,10 @@ def set_new_aliases(self, blocks, configures_block_gateway: bool, config_dir: st """ pvlist_file = os.path.join(config_dir, "gwblock.pvlist") if configures_block_gateway and os.path.exists(pvlist_file): - print_and_log("Using {} to configure block gateway".format(pvlist_file)) + print_and_log(f"Using {pvlist_file} to configure block gateway") copyfile(pvlist_file, self._pvlist_file) elif configures_block_gateway: - print_and_log("File: {} not found generating gwblock.pvlist".format(pvlist_file)) + print_and_log(f"File: {pvlist_file} not found generating gwblock.pvlist") self._generate_alias_file(blocks) else: self._generate_alias_file(blocks) diff --git a/BlockServer/epics/procserv_utils.py b/BlockServer/epics/procserv_utils.py index caac5387..81beed65 100644 --- a/BlockServer/epics/procserv_utils.py +++ b/BlockServer/epics/procserv_utils.py @@ -13,6 +13,8 @@ # along with this program; if not, you can obtain a copy from # https://www.eclipse.org/org/documents/epl-v10.php or # http://opensource.org/licenses/eclipse-1.0.php +import typing + from server_common.channel_access import ChannelAccess from server_common.utilities import ioc_restart_pending, print_and_log, retry @@ -75,9 +77,9 @@ def get_ioc_status(self, ioc: str): string : The status """ pv_name = f"{self.procserv_prefix}{ioc}:STATUS" - ans = ChannelAccess.caget(pv_name, as_string=True) + ans = typing.cast(str, ChannelAccess.caget(pv_name, as_string=True)) if ans is None: - raise Exception(f"Could not find IOC {ioc} (using pv {pv_name})") + raise OSError(f"Could not find IOC {ioc} (using pv {pv_name})") return ans.upper() def toggle_autorestart(self, ioc: str): diff --git a/BlockServer/test_modules/test_epics_gateway.py b/BlockServer/test_modules/test_epics_gateway.py index cc032b94..ce62b826 100644 --- a/BlockServer/test_modules/test_epics_gateway.py +++ b/BlockServer/test_modules/test_epics_gateway.py @@ -16,9 +16,9 @@ import os import unittest +from unittest.mock import MagicMock, mock_open, patch -from hamcrest import * -from mock import MagicMock, mock_open, patch +from hamcrest import assert_that, empty from ArchiverAccess.test_modules.stubs import FileStub from BlockServer.config.block import Block @@ -32,7 +32,9 @@ def setUp(self): self.prefix = "INST:" self.block_prefix = self.prefix + "BLOCK:" self.control_sys_prefix = self.prefix + "CONTROL:" - self.config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "settings") + self.config_dir = os.path.join( + os.path.dirname(os.path.realpath(__file__)), "settings" + ) self.gateway = Gateway( self.gateway_prefix, @@ -51,24 +53,28 @@ def test_GIVEN_gateway_pv_doesnt_exist_WHEN_exist_called_THEN_returns_false( channel_access.caget.assert_called_with(self.gateway_prefix + "pvtotal") @patch("BlockServer.epics.gateway.ChannelAccess") - def test_GIVEN_gateway_pv_exists_WHEN_exist_called_THEN_returns_true(self, channel_access): + def test_GIVEN_gateway_pv_exists_WHEN_exist_called_THEN_returns_true( + self, channel_access + ): channel_access.caget = MagicMock(return_value="Hi") self.assertTrue(self.gateway.exists()) channel_access.caget.assert_called_with(self.gateway_prefix + "pvtotal") def _assert_lines_correct(self, actual_lines, expected_lines): - sanitised_lines = list() + sanitised_lines = [] for line in actual_lines: if not line.startswith("##") and line != "": sanitised_lines.append(line.split()) self.assertListEqual(sanitised_lines, expected_lines) - def test_GIVEN_local_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as_expected(self): + def test_GIVEN_local_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as_expected( + self, + ): blockname, block_pv = "MY_BLOCK", "MY_PV" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "INST:MY_PV\\1"], + [rf"{alias}\([.:].*\)", "ALIAS", "INST:MY_PV\\1"], [alias, "ALIAS", "INST:MY_PV"], ] @@ -76,11 +82,13 @@ def test_GIVEN_local_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as_ self._assert_lines_correct(lines, expected_lines) - def test_GIVEN_remote_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as_expected(self): + def test_GIVEN_remote_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as_expected( + self, + ): blockname, block_pv = "MY_BLOCK", "MY_PV" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "MY_PV\\1"], + [rf"{alias}\([.:].*\)", "ALIAS", "MY_PV\\1"], [alias, "ALIAS", "MY_PV"], ] @@ -88,12 +96,14 @@ def test_GIVEN_remote_PV_without_suffix_or_SP_WHEN_alias_generated_THEN_lines_as self._assert_lines_correct(lines, expected_lines) - def test_GIVEN_local_PV_with_suffix_WHEN_alias_generated_THEN_lines_as_expected(self): + def test_GIVEN_local_PV_with_suffix_WHEN_alias_generated_THEN_lines_as_expected( + self, + ): blockname, block_pv = "MY_BLOCK", "MY_PV.EGU" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "INST:MY_PV\\1"], - ["{}[.]VAL".format(alias), "ALIAS", "INST:MY_PV.EGU"], + [rf"{alias}\([.:].*\)", "ALIAS", "INST:MY_PV\\1"], + [f"{alias}[.]VAL", "ALIAS", "INST:MY_PV.EGU"], [alias, "ALIAS", "INST:MY_PV.EGU"], ] @@ -101,12 +111,14 @@ def test_GIVEN_local_PV_with_suffix_WHEN_alias_generated_THEN_lines_as_expected( self._assert_lines_correct(lines, expected_lines) - def test_GIVEN_remote_PV_with_suffix_WHEN_alias_generated_THEN_lines_as_expected(self): + def test_GIVEN_remote_PV_with_suffix_WHEN_alias_generated_THEN_lines_as_expected( + self, + ): blockname, block_pv = "MY_BLOCK", "MY_PV.RBV" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "MY_PV\\1"], - ["{}[.]VAL".format(alias), "ALIAS", "MY_PV.RBV"], + [rf"{alias}\([.:].*\)", "ALIAS", "MY_PV\\1"], + [f"{alias}[.]VAL", "ALIAS", "MY_PV.RBV"], [alias, "ALIAS", "MY_PV.RBV"], ] @@ -118,8 +130,8 @@ def test_GIVEN_local_PV_with_SP_WHEN_alias_generated_THEN_lines_as_expected(self blockname, block_pv = "MY_BLOCK", "MY_PV:SP" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\(:SP\)?\([.:].*\)".format(alias), "ALIAS", "INST:MY_PV:SP\\2"], - [r"{}\(:SP\)?".format(alias), "ALIAS", "INST:MY_PV:SP"], + [rf"{alias}\(:SP\)?\([.:].*\)", "ALIAS", "INST:MY_PV:SP\\2"], + [rf"{alias}\(:SP\)?", "ALIAS", "INST:MY_PV:SP"], ] lines = self.gateway.generate_alias(blockname, block_pv, True) @@ -130,8 +142,8 @@ def test_GIVEN_remote_PV_with_SP_WHEN_alias_generated_THEN_lines_as_expected(sel blockname, block_pv = "MY_BLOCK", "MY_PV:SP" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\(:SP\)?\([.:].*\)".format(alias), "ALIAS", "MY_PV:SP\\2"], - [r"{}\(:SP\)?".format(alias), "ALIAS", "MY_PV:SP"], + [rf"{alias}\(:SP\)?\([.:].*\)", "ALIAS", "MY_PV:SP\\2"], + [rf"{alias}\(:SP\)?", "ALIAS", "MY_PV:SP"], ] lines = self.gateway.generate_alias(blockname, block_pv, False) @@ -142,8 +154,8 @@ def test_GIVEN_remote_PV_with_VAL_WHEN_alias_generated_THEN_lines_as_expected(se blockname, block_pv = "MY_BLOCK", "MY_TEST.VAL" alias = "INST:BLOCK:MY_BLOCK" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "MY_TEST\\1"], - ["{}".format(alias), "ALIAS", "MY_TEST"], + [rf"{alias}\([.:].*\)", "ALIAS", "MY_TEST\\1"], + [f"{alias}", "ALIAS", "MY_TEST"], ] lines = self.gateway.generate_alias(blockname, block_pv, False) @@ -156,9 +168,9 @@ def test_GIVEN_local_lowercase_PV_without_suffix_or_SP_WHEN_alias_generated_THEN blockname, block_pv = "My_Block", "MY_PV" alias = "INST:BLOCK:My_Block" expected_lines = [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "MY_PV\\1"], + [rf"{alias}\([.:].*\)", "ALIAS", "MY_PV\\1"], [alias, "ALIAS", "MY_PV"], - [r"{}\([.:].*\)".format(alias.upper()), "ALIAS", "MY_PV\\1"], + [rf"{alias.upper()}\([.:].*\)", "ALIAS", "MY_PV\\1"], [alias.upper(), "ALIAS", "MY_PV"], ] @@ -176,7 +188,15 @@ def test_GIVEN_configuration_has_pvlist_WHEN_set_new_aliases_THEN_pvlist_copied( config_dir = os.path.join(self.config_dir, "non_empty") expected_name = "block" expected_pv = "pv" - blocks = [Block(expected_name, expected_pv, log_periodic=True, log_rate=0, log_deadband=1)] + blocks = [ + Block( + expected_name, + expected_pv, + log_periodic=True, + log_rate=0, + log_deadband=1, + ) + ] self.gateway.set_new_aliases(blocks, True, config_dir) @@ -197,7 +217,11 @@ def test_GIVEN_configuration_claims_has_pvlist_BUT_does_not_WHEN_set_new_aliases expected_pv = "pv" blocks = { expected_name: Block( - expected_name, expected_pv, log_periodic=True, log_rate=0, log_deadband=1 + expected_name, + expected_pv, + log_periodic=True, + log_rate=0, + log_deadband=1, ) } alias = "INST:BLOCK:block" @@ -206,9 +230,13 @@ def test_GIVEN_configuration_claims_has_pvlist_BUT_does_not_WHEN_set_new_aliases for line in ALIAS_HEADER.format("INST:").splitlines() if not line.startswith("##") and line != "" ] + [ - [r"{}\([.:].*\)".format(alias), "ALIAS", "INST:{}\\1".format(expected_pv)], + [rf"{alias}\([.:].*\)", "ALIAS", f"INST:{expected_pv}\\1"], [alias, "ALIAS", "INST:" + expected_pv], - [r"{}\([.:].*\)".format(alias.upper()), "ALIAS", "INST:{}\\1".format(expected_pv)], + [ + rf"{alias.upper()}\([.:].*\)", + "ALIAS", + f"INST:{expected_pv}\\1", + ], [alias.upper(), "ALIAS", "INST:" + expected_pv], ["INST:CS:GATEWAY:BLOCKSERVER:.*", "ALLOW", "ANYBODY", "1"], ["INST:CS:GATEWAY:BLOCKSERVER:report[1-9]Flag", "ALLOW", "ANYBODY", "1"], @@ -226,4 +254,6 @@ def test_GIVEN_configuration_claims_has_pvlist_BUT_does_not_WHEN_set_new_aliases print("Expected Lines") for line in expected_lines: print(line) - self._assert_lines_correct(mock_file.file_contents[self.gateway_file_path], expected_lines) + self._assert_lines_correct( + mock_file.file_contents[self.gateway_file_path], expected_lines + ) diff --git a/DatabaseServer/database_server.py b/DatabaseServer/database_server.py index 581ddbb7..b82a8bae 100644 --- a/DatabaseServer/database_server.py +++ b/DatabaseServer/database_server.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - import argparse import json @@ -22,24 +20,20 @@ import os import sys import traceback +import zlib +from collections.abc import Callable from functools import partial from threading import RLock, Thread from time import sleep -from typing import Callable, Literal +from typing import Literal +import mysql.connector from pcaspy import Driver sys.path.insert(0, os.path.abspath(os.environ["MYDIRBLOCK"])) from genie_python.mysql_abstraction_layer import SQLAbstraction - -from DatabaseServer.exp_data import ExpData, ExpDataSource -from DatabaseServer.mocks.mock_exp_data import MockExpData -from DatabaseServer.moxa_data import MoxaData, MoxaDataSource -from DatabaseServer.options_holder import OptionsHolder -from DatabaseServer.options_loader import OptionsLoader -from DatabaseServer.procserv_utils import ProcServWrapper -from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode +from server_common.channel_access import verify_manager_mode from server_common.channel_access_server import CAServer from server_common.constants import IOCS_NOT_TO_STOP from server_common.ioc_data import IOCData @@ -56,6 +50,13 @@ set_logger, ) +from DatabaseServer.exp_data import ExpData, ExpDataSource +from DatabaseServer.mocks.mock_exp_data import MockExpData +from DatabaseServer.moxa_data import MoxaData, MoxaDataSource +from DatabaseServer.options_holder import OptionsHolder +from DatabaseServer.options_loader import OptionsLoader +from DatabaseServer.procserv_utils import ProcServWrapper + set_logger(IsisLogger()) MACROS = { @@ -74,8 +75,6 @@ class DatabaseServer(Driver): The class for handling all the static PV access and monitors etc. """ - RESTART_PV = {DbPVNames.RESTART: {"type": "int"}} - def __init__( self, ca_server: CAServer | MockCAServer, @@ -99,7 +98,7 @@ def __init__( test_mode: Enables starting the server in a mode suitable for unit tests """ if not test_mode: - super(DatabaseServer, self).__init__() + super().__init__() self._blockserver_prefix = blockserver_prefix self._ca_server = ca_server @@ -116,6 +115,10 @@ def __init__( monitor_thread.daemon = True # Daemonise thread monitor_thread.start() + @staticmethod + def restart_pv() -> dict[str, dict[str, str]]: + return {DbPVNames.RESTART: {"type": "int"}} + def _generate_pv_acquisition_info(self) -> dict: """ Generates information needed to get the data for the DB PVs. @@ -125,14 +128,24 @@ def _generate_pv_acquisition_info(self) -> dict: """ enhanced_info = DatabaseServer.generate_pv_info() - def add_get_method(pv: str, get_function: Callable[[], list | str | dict]) -> None: + def add_get_method( + pv: str, get_function: Callable[[], list | str | dict] + ) -> None: enhanced_info[pv]["get"] = get_function add_get_method(DbPVNames.IOCS, self._get_iocs_info) - add_get_method(DbPVNames.HIGH_INTEREST, partial(self._get_interesting_pvs, "HIGH")) - add_get_method(DbPVNames.MEDIUM_INTEREST, partial(self._get_interesting_pvs, "MEDIUM")) - add_get_method(DbPVNames.LOW_INTEREST, partial(self._get_interesting_pvs, "LOW")) - add_get_method(DbPVNames.FACILITY, partial(self._get_interesting_pvs, "FACILITY")) + add_get_method( + DbPVNames.HIGH_INTEREST, partial(self._get_interesting_pvs, "HIGH") + ) + add_get_method( + DbPVNames.MEDIUM_INTEREST, partial(self._get_interesting_pvs, "MEDIUM") + ) + add_get_method( + DbPVNames.LOW_INTEREST, partial(self._get_interesting_pvs, "LOW") + ) + add_get_method( + DbPVNames.FACILITY, partial(self._get_interesting_pvs, "FACILITY") + ) add_get_method(DbPVNames.ACTIVE_PVS, self._get_active_pvs) add_get_method(DbPVNames.ALL_PVS, partial(self._get_interesting_pvs, "")) add_get_method(DbPVNames.SAMPLE_PARS, self._get_sample_par_names) @@ -208,7 +221,7 @@ def read(self, reason: str) -> bytes: """ return ( self.get_data_for_pv(reason) - if reason in self._pv_info.keys() + if reason in self._pv_info else self.getParam(reason) ) @@ -237,7 +250,7 @@ def write(self, reason: str, value: str) -> Literal[True]: ) elif reason == "UPDATE_MM": self._moxa_data.update_mappings() - except Exception as e: + except (OSError, ValueError, KeyError, TypeError, zlib.error) as e: value_bytes = compress_and_hex(convert_to_json("Error: " + str(e))) print_and_log(str(e), MAJOR_MSG) self.setParam(reason, value_bytes) @@ -275,7 +288,7 @@ def _update_ioc_monitors(self) -> None: ]: encoded_data = self.get_data_for_pv(pv) # No need to update monitors if data hasn't changed - if not self.getParam(pv) == encoded_data: + if self.getParam(pv) != encoded_data: self.setParam(pv, encoded_data) # Update them with self.monitor_lock: @@ -293,8 +306,8 @@ def _check_pv_capacity(self, pv: str, size: int, prefix: str) -> None: """ if size > self._pv_info[pv]["count"]: print_and_log( - "Too much data to encode PV {0}. Current size is {1} characters " - "but {2} are required".format(prefix + pv, self._pv_info[pv]["count"], size), + f"Too much data to encode PV {prefix + pv}. Current size is {self._pv_info[pv]['count']} characters " + f"but {size} are required", MAJOR_MSG, LOG_TARGET, ) @@ -302,13 +315,16 @@ def _check_pv_capacity(self, pv: str, size: int, prefix: str) -> None: def _get_iocs_info(self) -> dict: iocs = self._iocs.get_iocs() options = self._options_holder.get_config_options() - for iocname in iocs.keys(): + for iocname in iocs: if iocname in options: iocs[iocname].update(options[iocname]) return iocs def _get_pvs( - self, get_method: Callable[[], list | str | dict], replace_pv_prefix: bool, *get_args: str + self, + get_method: Callable[[], list | str | dict], + replace_pv_prefix: bool, + *get_args: str, ) -> list | str | dict: """ Method to get pv data using the given method called with the given arguments and optionally @@ -441,19 +457,21 @@ def _get_iocs_not_to_stop() -> list: BLOCKSERVER_PREFIX = args.blockserver_prefix[0] if not BLOCKSERVER_PREFIX.endswith(":"): BLOCKSERVER_PREFIX += ":" - BLOCKSERVER_PREFIX = BLOCKSERVER_PREFIX.replace("%MYPVPREFIX%", MACROS["$(MYPVPREFIX)"]) - print_and_log("BLOCKSERVER PREFIX = %s" % BLOCKSERVER_PREFIX, INFO_MSG, LOG_TARGET) + BLOCKSERVER_PREFIX = BLOCKSERVER_PREFIX.replace( + "%MYPVPREFIX%", MACROS["$(MYPVPREFIX)"] + ) + print_and_log(f"BLOCKSERVER PREFIX = {BLOCKSERVER_PREFIX}", INFO_MSG, LOG_TARGET) OPTIONS_DIR = os.path.abspath(args.options_dir[0]) - print_and_log("OPTIONS DIRECTORY = %s" % OPTIONS_DIR, INFO_MSG, LOG_TARGET) + print_and_log(f"OPTIONS DIRECTORY = {OPTIONS_DIR}", INFO_MSG, LOG_TARGET) if not os.path.isdir(os.path.abspath(OPTIONS_DIR)): # Create it then os.makedirs(os.path.abspath(OPTIONS_DIR)) SERVER = CAServer(BLOCKSERVER_PREFIX) SERVER.createPV(BLOCKSERVER_PREFIX, DatabaseServer.generate_pv_info()) - SERVER.createPV(BLOCKSERVER_PREFIX, DatabaseServer.RESTART_PV) - SERVER.createPV(MACROS["$(MYPVPREFIX)"], ExpData.EDPV) + SERVER.createPV(BLOCKSERVER_PREFIX, DatabaseServer.restart_pv()) + SERVER.createPV(MACROS["$(MYPVPREFIX)"], ExpData.edpv()) SERVER.createPV(MACROS["$(MYPVPREFIX)"], MoxaData.MDPV) ioc_data = None @@ -470,9 +488,9 @@ def _get_iocs_not_to_stop() -> list: MACROS["$(MYPVPREFIX)"], ) print_and_log("Connected to IOCData database", INFO_MSG, LOG_TARGET) - except Exception: + except (OSError, mysql.connector.Error): print_and_log( - "Problem initialising IOCData DB connection: {}".format(traceback.format_exc()), + f"Problem initialising IOCData DB connection: {traceback.format_exc()}", MAJOR_MSG, LOG_TARGET, ) @@ -481,12 +499,12 @@ def _get_iocs_not_to_stop() -> list: if exp_data is None: try: exp_data = ExpData(MACROS["$(MYPVPREFIX)"], ExpDataSource()) - print_and_log("Connected to experimental details database", INFO_MSG, LOG_TARGET) - except Exception: print_and_log( - "Problem connecting to experimental details database: {}".format( - traceback.format_exc() - ), + "Connected to experimental details database", INFO_MSG, LOG_TARGET + ) + except (OSError, mysql.connector.Error): + print_and_log( + f"Problem connecting to experimental details database: {traceback.format_exc()}", MAJOR_MSG, LOG_TARGET, ) @@ -497,12 +515,12 @@ def _get_iocs_not_to_stop() -> list: MoxaDataSource(SQLAbstraction("moxa_details", "moxas", "$moxas")), MACROS["$(MYPVPREFIX)"], ) - print_and_log("Connected to moxa details database", INFO_MSG, LOG_TARGET) - except Exception: print_and_log( - "Problem connecting to moxa details database: {}".format( - traceback.format_exc() - ), + "Connected to moxa details database", INFO_MSG, LOG_TARGET + ) + except (OSError, mysql.connector.Error): + print_and_log( + f"Problem connecting to moxa details database: {traceback.format_exc()}", MAJOR_MSG, LOG_TARGET, ) @@ -513,12 +531,14 @@ def _get_iocs_not_to_stop() -> list: else: sleep(15) - DRIVER = DatabaseServer(SERVER, ioc_data, exp_data, moxa_data, OPTIONS_DIR, BLOCKSERVER_PREFIX) + DRIVER = DatabaseServer( + SERVER, ioc_data, exp_data, moxa_data, OPTIONS_DIR, BLOCKSERVER_PREFIX + ) # Process CA transactions while True: try: SERVER.process(0.1) - except Exception: + except Exception: # noqa BLE001 pcaspy can raise anything from driver callbacks print_and_log(traceback.format_exc(), MAJOR_MSG) break diff --git a/DatabaseServer/exp_data.py b/DatabaseServer/exp_data.py index f3f22d4d..8dd17e6a 100644 --- a/DatabaseServer/exp_data.py +++ b/DatabaseServer/exp_data.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - # This file is part of the ISIS IBEX application. # Copyright (C) 2012-2016 Science & Technology Facilities Council. # All rights reserved. @@ -17,11 +15,11 @@ # http://opensource.org/licenses/eclipse-1.0.php import json import traceback +import typing import unicodedata from typing import TYPE_CHECKING, Union from genie_python.mysql_abstraction_layer import SQLAbstraction - from server_common.channel_access import ChannelAccess from server_common.mocks.mock_ca import MockChannelAccess from server_common.utilities import char_waveform, compress_and_hex, print_and_log @@ -30,7 +28,7 @@ from DatabaseServer.test_modules.test_exp_data import MockExpDataSource -class User(object): +class User: """ A user class to allow for easier conversions from database to json. """ @@ -43,7 +41,7 @@ def __init__( self.role = role -class ExpDataSource(object): +class ExpDataSource: """ This is a humble object containing all the code for accessing the database. """ @@ -75,11 +73,11 @@ def get_team(self, experiment_id: str) -> list: team = [list(element) for element in result] if len(team) == 0: raise ValueError( - "unable to find team details for experiment ID {}".format(experiment_id) + f"unable to find team details for experiment ID {experiment_id}" ) else: return team - except Exception: + except (OSError, ValueError, AssertionError): print_and_log(traceback.format_exc()) return [] @@ -101,25 +99,21 @@ def experiment_exists(self, experiment_id: str) -> bool: if id is None: return False return len(id) >= 1 - except Exception: + except (OSError, AssertionError): print_and_log(traceback.format_exc()) return False -class ExpData(object): +class ExpData: """ A wrapper to connect to the IOC database via MySQL. """ - EDPV = {"ED:RBNUMBER:SP": char_waveform(16000), "ED:USERNAME:SP": char_waveform(16000)} - - _to_ascii = {} - def __init__( self, prefix: str, db: Union[ExpDataSource, "MockExpDataSource"], - ca: Union[ChannelAccess, MockChannelAccess] = ChannelAccess(), + ca: ChannelAccess | MockChannelAccess | None = None, ) -> None: """ Constructor. @@ -138,13 +132,17 @@ def __init__( self._orgspv = prefix + "ED:ORGS" # Set the channel access server to use - self.ca = ca + self.ca = typing.cast(ChannelAccess, ca if not None else ChannelAccess()) # Set the data source to use self._db = db - # Create ascii mappings - ExpData._to_ascii = self._make_ascii_mappings() + @staticmethod + def edpv() -> dict[str, dict[str, str | list[int] | typing.Any]]: + return { + "ED:RBNUMBER:SP": char_waveform(16000), + "ED:USERNAME:SP": char_waveform(16000), + } @staticmethod def _make_ascii_mappings() -> dict: @@ -173,7 +171,7 @@ def encode_for_return(self, data: dict | list) -> bytes: def _get_surname_from_fullname(self, fullname: str) -> str: try: return fullname.split(" ")[-1] - except ValueError | IndexError: + except (ValueError, IndexError): return fullname def update_experiment_id(self, experiment_id: str) -> None: @@ -201,7 +199,7 @@ def update_experiment_id(self, experiment_id: str) -> None: self.ca.caput(self._simnames, self.encode_for_return(names)) self.ca.caput(self._surnamepv, self.encode_for_return(surnames)) self.ca.caput(self._orgspv, self.encode_for_return(orgs)) - raise Exception("error finding the experiment: %s" % experiment_id) + raise OSError(f"error finding the experiment: {experiment_id}") # Get the user information from the database and update the associated PVs if self._db is not None: @@ -211,7 +209,7 @@ def update_experiment_id(self, experiment_id: str) -> None: fullname = str(member[0]) org = str(member[1]) role = str(member[2]) - if not role == "Contact": + if role != "Contact": surnames.append(self._get_surname_from_fullname(fullname)) orgs.append(org) name = User(fullname, org, role.lower()) @@ -250,7 +248,7 @@ def update_username(self, user_str: str) -> None: else: org = team_member["institute"] role = team_member["role"] - if not role == "Contact": + if role != "Contact": surnames.append(self._get_surname_from_fullname(fullname)) orgs.append(org) name = User(fullname, org, role.lower()) @@ -281,4 +279,6 @@ def make_name_list_ascii(names: list) -> bytes: nlist = ",".join(names) nfkd_form = unicodedata.normalize("NFKD", nlist) nlist_no_sc = "".join([c for c in nfkd_form if not unicodedata.combining(c)]) - return nlist_no_sc.translate(ExpData._to_ascii).encode("ascii", "ignore") + return nlist_no_sc.translate(ExpData._make_ascii_mappings()).encode( + "ascii", "ignore" + ) diff --git a/DatabaseServer/test_modules/test_exp_data.py b/DatabaseServer/test_modules/test_exp_data.py index 193b2858..c135b184 100644 --- a/DatabaseServer/test_modules/test_exp_data.py +++ b/DatabaseServer/test_modules/test_exp_data.py @@ -65,11 +65,8 @@ def test_update_experiment_id_set_orgs_pv(self): self.assertCountEqual(data, ["ESS", "ISIS"]) def test_update_experiment_id_throws_if_experiment_does_not_exists(self): - try: + with self.assertRaises(OSError): self.exp_data.update_experiment_id("000000") - self.fail("Setting invalid experiment id did not throw") - except Exception: - pass def test_single_surname_returns_surname(self): # Arrange diff --git a/block_server.py b/block_server.py index 7fb6a927..07250310 100644 --- a/block_server.py +++ b/block_server.py @@ -20,7 +20,9 @@ import os import sys import traceback -from typing import TYPE_CHECKING, Any, Dict, List +import zlib +import zoneinfo +from typing import TYPE_CHECKING, Any from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer @@ -41,7 +43,12 @@ from pcaspy.driver import Data, manager from server_common.channel_access import ChannelAccess from server_common.file_path_manager import FILEPATH_MANAGER -from server_common.helpers import BLOCK_PREFIX, CONTROL_SYSTEM_PREFIX, MACROS, PVPREFIX_MACRO +from server_common.helpers import ( + BLOCK_PREFIX, + CONTROL_SYSTEM_PREFIX, + MACROS, + PVPREFIX_MACRO, +) from server_common.pv_names import BlockserverPVNames, DatabasePVNames from server_common.utilities import ( char_waveform, @@ -131,7 +138,7 @@ def __init__(self, ca_server: CAServer) -> None: Args: ca_server (CAServer): The CA server used for generating PVs on the fly """ - super(BlockServer, self).__init__() + super().__init__() # Threading stuff self.monitor_lock = RLock() @@ -153,7 +160,7 @@ def __init__(self, ca_server: CAServer) -> None: self._run_control = None self._syn = None self._devices = None - self.on_the_fly_handlers = list() + self.on_the_fly_handlers = [] self._ioc_control = IocControl(self.instrument_prefix) self.block_rules = BlockRules(self) self.group_rules = GroupRules(self) @@ -169,13 +176,12 @@ def __init__(self, ca_server: CAServer) -> None: self._config_vc.setup() print_and_log("Config version control initialised correctly", "INFO") except NotUnderVersionControl as err: - print_and_log("Configurations not under version control: %s" % err, "MINOR") + print_and_log(f"Configurations not under version control: {err}", "MINOR") self._config_vc = MockVersionControl() except VersionControlException as err: - print_and_log("Unable to initialise config version control: %s" % err, "MINOR") - self._config_vc = MockVersionControl() - except Exception as err: - print_and_log("Unable to initialise config version control: %s" % err, "MINOR") + print_and_log( + f"Unable to initialise config version control: {err}", "MINOR" + ) self._config_vc = MockVersionControl() try: @@ -188,24 +194,27 @@ def __init__(self, ca_server: CAServer) -> None: self._script_vc.setup() print_and_log("Scripting version control initialised correctly", "INFO") except NotUnderVersionControl as err: - print_and_log("Scripts not under version control: %s" % err, "MINOR") + print_and_log(f"Scripts not under version control: {err}", "MINOR") self._script_vc = MockVersionControl() except VersionControlException as err: - print_and_log("Unable to initialise script version control: %s" % err, "MINOR") + print_and_log( + f"Unable to initialise script version control: {err}", "MINOR" + ) self._script_vc = MockVersionControl() - except Exception as err: - print_and_log("Unable to initialise script version control: %s" % err, "MINOR") + except Exception as err: # noqa: BLE001 deliberate fallback to MockVersionControl + print_and_log( + f"Unable to initialise script version control: {err}", "MINOR" + ) self._script_vc = MockVersionControl() # Import data about all configs try: self._config_list = ConfigListManager(self, ConfigurationFileManager()) - except Exception as err: + except OSError as err: print_and_log( "Error creating inactive config list. " "Configuration list changes will not be stored " - + "in version control: %s " - % str(err), + + f"in version control: {err!s} ", "MINOR", ) self._config_list = ConfigListManager(self, ConfigurationFileManager()) @@ -215,7 +224,9 @@ def __init__(self, ca_server: CAServer) -> None: write_thread.daemon = True # Daemonise thread write_thread.start() - self.write_queue.put((self.initialise_configserver, (FACILITY,), "INITIALISING")) + self.write_queue.put( + (self.initialise_configserver, (FACILITY,), "INITIALISING") + ) # Starts the Web Server self.server = Server() self.server.start() @@ -269,8 +280,10 @@ def initialise_configserver(self, facility: str) -> None: else: print_and_log("Could not connect to gateway - is it running?") self.load_last_config() - except Exception as err: - print_and_log("Could not load last configuration. Message was: %s" % err, "MAJOR") + except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions + print_and_log( + f"Could not load last configuration. Message was: {err}", "MAJOR" + ) self._active_configserver.clear_config() self._initialise_config() @@ -295,9 +308,13 @@ def read(self, reason: str) -> str: ) value = compress_and_hex(grps) elif reason == BlockserverPVNames.CONFIGS: - value = compress_and_hex(convert_to_json(self._config_list.get_configs())) + value = compress_and_hex( + convert_to_json(self._config_list.get_configs()) + ) elif reason == BlockserverPVNames.COMPS: - value = compress_and_hex(convert_to_json(self._config_list.get_components())) + value = compress_and_hex( + convert_to_json(self._config_list.get_components()) + ) elif reason == BlockserverPVNames.BLANK_CONFIG: js = convert_to_json(self.get_blank_config()) value = compress_and_hex(js) @@ -320,7 +337,7 @@ def read(self, reason: str) -> str: return handler.handle_pv_read(reason) value = self.getParam(reason) - except Exception as err: + except Exception as err: # noqa: BLE001 calls arbitrary on_the_fly_handlers and turns any error into a PV value value = compress_and_hex(convert_to_json("Error: " + str(err))) print_and_log(str(err), "MAJOR") return bytes.decode(value, "utf-8") if type(value) is bytes else str(value) @@ -341,16 +358,28 @@ def write(self, reason: str, value: str) -> bool: # type: ignore """ status = True try: - data = dehex_and_decompress(bytes(value, encoding="utf-8")).strip(b'"').decode("utf-8") + data = ( + dehex_and_decompress(bytes(value, encoding="utf-8")) + .strip(b'"') + .decode("utf-8") + ) if reason == BlockserverPVNames.LOAD_CONFIG: self.write_queue.put((self.load_config, (data,), "LOADING_CONFIG")) elif reason == BlockserverPVNames.RELOAD_CURRENT_CONFIG: - self.write_queue.put((self.reload_current_config, (), "RELOAD_CURRENT_CONFIG")) + self.write_queue.put( + (self.reload_current_config, (), "RELOAD_CURRENT_CONFIG") + ) elif reason == BlockserverPVNames.START_IOCS: - self.write_queue.put((self.start_iocs, (convert_from_json(data),), "START_IOCS")) + self.write_queue.put( + (self.start_iocs, (convert_from_json(data),), "START_IOCS") + ) elif reason == BlockserverPVNames.STOP_IOCS: self.write_queue.put( - (self._ioc_control.stop_iocs, (convert_from_json(data),), "STOP_IOCS") + ( + self._ioc_control.stop_iocs, + (convert_from_json(data),), + "STOP_IOCS", + ) ) elif reason == BlockserverPVNames.RESTART_IOCS: self.write_queue.put( @@ -373,10 +402,16 @@ def write(self, reason: str, value: str) -> bool: # type: ignore elif reason == BlockserverPVNames.SAVE_NEW_CONFIG: self.write_queue.put((self.save_config, (data,), "SAVING_NEW_CONFIG")) elif reason == BlockserverPVNames.SAVE_NEW_COMPONENT: - self.write_queue.put((self.save_config, (data, True), "SAVING_NEW_COMP")) + self.write_queue.put( + (self.save_config, (data, True), "SAVING_NEW_COMP") + ) elif reason == BlockserverPVNames.DELETE_CONFIGS: self.write_queue.put( - (self._config_list.delete_configs, (convert_from_json(data),), "DELETE_CONFIGS") + ( + self._config_list.delete_configs, + (convert_from_json(data),), + "DELETE_CONFIGS", + ) ) elif reason == BlockserverPVNames.DELETE_COMPONENTS: self.write_queue.put( @@ -397,7 +432,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore status = True break - except Exception as err: + except (ValueError, zlib.error) as err: value = str(compress_and_hex(convert_to_json("Error: " + str(err)))) print_and_log(str(err), "MAJOR") else: @@ -409,7 +444,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore self.setParam(reason, value) return status - def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> None: + def restart_service_ioc(self, iocs: list[str], reapply_auto: bool = False) -> None: """Restart service IOCs. Args: @@ -418,12 +453,11 @@ def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> No """ for ioc in iocs: - if not ioc.startswith(self._service_iocs): continue verify_manager_mode( - message=f"Attempting to restart a service IOC')", + message="Attempting to restart a service IOC')", ) match ioc: @@ -433,7 +467,8 @@ def restart_service_ioc(self, iocs: List[str], reapply_auto: bool = False) -> No self._gateway.reload() case "DBSVR": ChannelAccess.caput( - CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART, 1, + CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART, + 1, ) case _: self._ioc_control.restart_ioc(ioc, force=True) @@ -460,7 +495,7 @@ def load_last_config(self) -> None: ) self._active_configserver.clear_config() else: - print_and_log("Loaded last configuration: %s" % last) + print_and_log(f"Loaded last configuration: {last}") self._initialise_config() def _set_curr_config(self, details: str) -> None: @@ -497,7 +532,9 @@ def _initialise_config(self, full_init: bool = False) -> None: e.g. on loading a new configuration """ if self._active_configserver is not None: - new_iocs, changed_iocs, removed_iocs = self._active_configserver.iocs_changed() + new_iocs, changed_iocs, removed_iocs = ( + self._active_configserver.iocs_changed() + ) self._ioc_control.stop_iocs(list(removed_iocs)) self._start_config_iocs(list(new_iocs), list(changed_iocs)) if ( @@ -506,13 +543,18 @@ def _initialise_config(self, full_init: bool = False) -> None: ): # See https://github.com/ISISComputingGroup/IBEX/issues/5590 for # justification of why this ioc gets special treatment. - ioc = self._active_configserver.get_all_ioc_details()[CAEN_DISCRIMINATOR_IOC_NAME] + ioc = self._active_configserver.get_all_ioc_details()[ + CAEN_DISCRIMINATOR_IOC_NAME + ] if ioc.autostart: print_and_log( f"{CAEN_DISCRIMINATOR_IOC_NAME} present in configuration and set " f"to autostart - restarting it" ) - if self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) == "RUNNING": + if ( + self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) + == "RUNNING" + ): self._ioc_control.restart_iocs( [CAEN_DISCRIMINATOR_IOC_NAME], reapply_auto=True ) @@ -525,12 +567,18 @@ def _initialise_config(self, full_init: bool = False) -> None: self._active_configserver.get_block_details(), self._active_configserver.configures_block_gateway_and_archiver(), os.path.join( - CONFIG_DIR, "configurations", self._active_configserver.get_config_name() + CONFIG_DIR, + "configurations", + self._active_configserver.get_config_name(), ), ) - self._config_list.active_config_name = self._active_configserver.get_config_name() - self._config_list.active_components = self._active_configserver.get_component_names() + self._config_list.active_config_name = ( + self._active_configserver.get_config_name() + ) + self._config_list.active_components = ( + self._active_configserver.get_component_names() + ) self._config_list.update_monitors() self.update_blocks_monitors() @@ -543,10 +591,16 @@ def _initialise_config(self, full_init: bool = False) -> None: handler.on_config_change(full_init=full_init) # Update Web Server text - self.server.set_config(convert_to_json(self._active_configserver.get_config_details())) - self.write_queue.put((self.set_config_block_values, (), "LOADING_BLOCK_SETS")) + self.server.set_config( + convert_to_json(self._active_configserver.get_config_details()) + ) + self.write_queue.put( + (self.set_config_block_values, (), "LOADING_BLOCK_SETS") + ) - def _start_config_iocs(self, iocs_to_start: list[str], iocs_to_restart: list[str]) -> None: + def _start_config_iocs( + self, iocs_to_start: list[str], iocs_to_restart: list[str] + ) -> None: # Start the IOCs, if they are available and if they are flagged for autostart # Note: autostart means the IOC is started when the config is loaded, # restart means the IOC should automatically restart if it stops for some reason @@ -562,8 +616,12 @@ def _should_start(ioc_name: str) -> bool: ioc = _ioc_from_name(ioc_name) return ioc.autostart and not _is_remote(ioc_name) - self._ioc_control.start_iocs([ioc for ioc in iocs_to_start if _should_start(ioc)]) - self._ioc_control.restart_iocs([ioc for ioc in iocs_to_restart if _should_start(ioc)]) + self._ioc_control.start_iocs( + [ioc for ioc in iocs_to_start if _should_start(ioc)] + ) + self._ioc_control.restart_iocs( + [ioc for ioc in iocs_to_restart if _should_start(ioc)] + ) for ioc in iocs_to_start: self._ioc_control.waitfor_running(ioc) @@ -597,8 +655,10 @@ def load_config(self, config: str, full_init: bool = True) -> None: self._active_configserver.load_active(config) # If we get this far then assume the config is okay self._initialise_config(full_init=full_init) - except Exception as err: - print_and_log(f"Exception while loading configuration '{config}': {err}", "MAJOR") + except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions + print_and_log( + f"Exception while loading configuration '{config}': {err}", "MAJOR" + ) traceback.print_exc() def reload_current_config(self) -> None: @@ -608,9 +668,10 @@ def reload_current_config(self) -> None: print_and_log("Reloading current configuration") self._active_configserver.reload_current_config() self._initialise_config(full_init=True) - except Exception as err: + except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions print_and_log( - "Exception while reloading current configuration: {}".format(err), "MAJOR" + f"Exception while reloading current configuration: {err}", + "MAJOR", ) traceback.print_exc() @@ -646,7 +707,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: "component" if as_comp else "config", config_name ) ) - except IOError: + except OSError: pass # IOError thrown if config we're overwriting didn't exist, i.e. # this is a brand new config/component. @@ -670,14 +731,19 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: print_and_log(f"Finished saving ({config_name})") - except Exception: + except Exception: # noqa: BLE001 Call chain can cause many types of exceptions print_and_log( - f"Problem occurred saving configuration: {traceback.format_exc()}", "MAJOR" + f"Problem occurred saving configuration: {traceback.format_exc()}", + "MAJOR", ) if self._active_configserver is not None: # Reload configuration if a component has changed - if as_comp and new_details["name"] in self._active_configserver.get_component_names(): + if ( + as_comp + and new_details["name"] + in self._active_configserver.get_component_names() + ): self.load_last_config() # If the configuration we are trying to save is the currently active one, @@ -686,27 +752,35 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if config_name == self._active_configserver.get_config_name(): self.load_config(config_name, full_init=False) - def _get_inactive_history(self, name: str, is_component: bool = False) -> list[str | None]: + def _get_inactive_history( + self, name: str, is_component: bool = False + ) -> list[str | None]: # If it already exists load it try: inactive = InactiveConfigHolder(MACROS, ConfigurationFileManager()) inactive.load_inactive(name, is_component) # Get previous history history = inactive.get_history() - except IOError: + except OSError: # Config doesn't exist therefore start new history - history = list() + history = [] return history def _get_timestamp(self) -> str: - return datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S") + return datetime.datetime.strftime( + datetime.datetime.now(zoneinfo.ZoneInfo("localtime")), "%Y-%m-%d %H:%M:%S" + ) def update_blocks_monitors(self) -> None: """Updates the PV monitors for the blocks and groups, so the clients can see any changes.""" if self._active_configserver is not None: with self.monitor_lock: - block_names = convert_to_json(self._active_configserver.get_blocknames()) - self.setParam(BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names)) + block_names = convert_to_json( + self._active_configserver.get_blocknames() + ) + self.setParam( + BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names) + ) groups = ConfigurationJsonConverter.groups_to_json( self._active_configserver.get_group_details() @@ -755,7 +829,8 @@ def update_wd_details_monitors(self) -> None: } ) self.setParam( - BlockserverPVNames.WD_CONF_DETAILS, compress_and_hex(config_details_json) + BlockserverPVNames.WD_CONF_DETAILS, + compress_and_hex(config_details_json), ) self.updatePVs() @@ -765,9 +840,13 @@ def update_curr_config_name_monitors(self) -> None: if self._active_configserver is not None: with self.monitor_lock: self.setParam( - BlockserverPVNames.CURR_CONFIG_NAME, self._active_configserver.get_config_name() + BlockserverPVNames.CURR_CONFIG_NAME, + self._active_configserver.get_config_name(), + ) + self.setParam( + BlockserverPVNames.CURR_CONFIG_NAME_SEVR, + CURR_CONFIG_NAME_SEVR_VALUE, ) - self.setParam(BlockserverPVNames.CURR_CONFIG_NAME_SEVR, CURR_CONFIG_NAME_SEVR_VALUE) self.updatePVs() def consume_write_queue(self) -> None: @@ -787,7 +866,7 @@ def consume_write_queue(self) -> None: cmd(*arg) if arg is not None else cmd() except ManagerModeRequiredError as err: print_and_log(f"Error, operation requires manager mode: {err}", "MAJOR") - except Exception as err: + except Exception as err: # noqa: BLE001 A worker-thread catch-all that runs arbitrary commands. Narrowing it could kill the thread print_and_log( f"Error executing write queue command {cmd.__name__} for state {state}: {err}", "MAJOR", @@ -795,7 +874,7 @@ def consume_write_queue(self) -> None: traceback.print_exc() self.update_server_status("") - def get_blank_config(self) -> Dict[str, Any]: + def get_blank_config(self) -> dict[str, Any]: """Get a blank configuration which can be used to create a new configuration from scratch. Returns: @@ -880,7 +959,7 @@ def add_string_pv_to_db(self, name: str, count: int = 1000) -> None: data = Data() data.value = manager.pvs[self.port][name].info.value self.pvDB[name] = data - except Exception: + except Exception: # noqa: BLE001 Unclear what createPV raises print_and_log(f"Unable to add PV {name}", "MAJOR") @@ -956,7 +1035,11 @@ def add_string_pv_to_db(self, name: str, count: int = 1000) -> None: nargs=1, default=[ os.path.join( - MACROS["$(EPICS_KIT_ROOT)"], "CSS", "master", "ArchiveEngine", "block_config.xml" + MACROS["$(EPICS_KIT_ROOT)"], + "CSS", + "master", + "ArchiveEngine", + "block_config.xml", ) ], help="The XML file containing the new PV Archiver log settings", @@ -1021,6 +1104,6 @@ def add_string_pv_to_db(self, name: str, count: int = 1000) -> None: while True: try: SERVER.process(0.1) - except Exception as err: + except OSError as err: print_and_log(err, "MAJOR") break From 5892da8401a0205a0334a0e54fcd378b67502ff6 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 12 Nov 2026 14:25:29 +0000 Subject: [PATCH 07/12] ruff/pyright --- BlockServer/core/ioc_control.py | 16 +-- BlockServer/epics/gateway.py | 24 ++-- BlockServer/epics/procserv_utils.py | 16 +-- .../test_modules/test_epics_gateway.py | 18 +-- DatabaseServer/database_server.py | 48 +++----- DatabaseServer/exp_data.py | 8 +- block_server.py | 112 +++++------------- 7 files changed, 72 insertions(+), 170 deletions(-) diff --git a/BlockServer/core/ioc_control.py b/BlockServer/core/ioc_control.py index 20d18f38..80efc6f3 100644 --- a/BlockServer/core/ioc_control.py +++ b/BlockServer/core/ioc_control.py @@ -48,9 +48,7 @@ def start_ioc(self, ioc: str, restart_alarm_server: bool = True) -> None: except OSError as err: print_and_log(f"Could not start IOC {ioc}: {err}", "MAJOR") - def restart_ioc( - self, ioc: str, force: bool = False, restart_alarm_server: bool = True - ) -> None: + def restart_ioc(self, ioc: str, force: bool = False, restart_alarm_server: bool = True) -> None: """Restart an IOC. Note: restarting an IOC automatically sets the IOC to auto-restart, @@ -179,9 +177,7 @@ def set_autorestart(self, ioc: str, enable: bool) -> None: return print_and_log(f"Auto-restart for IOC {ioc} is already {enable}") else: - print_and_log( - f"Auto-restart for IOC {ioc} unchanged as IOC is not running" - ) + print_and_log(f"Auto-restart for IOC {ioc} unchanged as IOC is not running") except MaxAttemptsExceededException as err: print_and_log(f"Could not set auto-restart IOC {ioc}: {err}", "MAJOR") @@ -197,9 +193,7 @@ def get_autorestart(self, ioc: str) -> bool: try: return self._proc.get_autorestart(ioc) except MaxAttemptsExceededException as err: - print_and_log( - f"Could not get auto-restart setting for IOC {ioc}: {err}", "MAJOR" - ) + print_and_log(f"Could not get auto-restart setting for IOC {ioc}: {err}", "MAJOR") return False def waitfor_running(self, ioc: str, timeout: int = 15) -> None: @@ -214,9 +208,7 @@ def waitfor_running(self, ioc: str, timeout: int = 15) -> None: """ if self.ioc_exists(ioc): start = time() - while ( - self.ioc_restart_pending(ioc) or self.get_ioc_status(ioc) != "RUNNING" - ): + while self.ioc_restart_pending(ioc) or self.get_ioc_status(ioc) != "RUNNING": sleep(0.5) if time() - start >= timeout: print_and_log( diff --git a/BlockServer/epics/gateway.py b/BlockServer/epics/gateway.py index 24f668ad..f1d5113b 100644 --- a/BlockServer/epics/gateway.py +++ b/BlockServer/epics/gateway.py @@ -23,6 +23,8 @@ from server_common.helpers import CONTROL_SYSTEM_PREFIX from server_common.utilities import print_and_log +from BlockServer.config.block import Block + ALIAS_HEADER = """\ ## EVALUATION ORDER ALLOW, DENY @@ -30,11 +32,13 @@ """ ALIAS_FOOTER = r""" -## serve blockserver internal variables, including Flag variables needed by blockserver process to restart gateway +## serve blockserver internal variables, including Flag variables needed by +## blockserver process to restart gateway {0}CS:GATEWAY:BLOCKSERVER:.* ALLOW ANYBODY 1 ## allow anybody to generate gateway reports {0}CS:GATEWAY:BLOCKSERVER:report[1-9]Flag ALLOW ANYBODY 1 -## ignore any request related to run control/alerts etc. (RC,AC,DC) on blocks, these are handled by RUNCTRL ioc +## ignore any request related to run control/alerts etc. (RC,AC,DC) on blocks, +## these are handled by RUNCTRL ioc {0}CS:SB:[^:]*:[ADR]C:.* DENY ## ignore any request not related to local blocks or gateway itself !{0}CS:\(SB\|GATEWAY\):.* DENY @@ -137,7 +141,7 @@ def reload(self) -> None: except OSError as err: print_and_log(f"Problem with reloading the gateway {err}") - def _generate_alias_file(self, blocks=None) -> None: + def _generate_alias_file(self, blocks: dict[str, Block] | None = None) -> None: # Generate blocks.pvlist for gateway with open(self._pvlist_file, "w") as f: header = ALIAS_HEADER.format(self._inst_prefix) @@ -150,9 +154,7 @@ def _generate_alias_file(self, blocks=None) -> None: # Add a blank line at the end! f.write("\n") - def generate_alias( - self, block_name: str, underlying_pv: str, local: bool - ) -> list[str]: + def generate_alias(self, block_name: str, underlying_pv: str, local: bool) -> list[str]: print_and_log(f"Creating block: {block_name} for {underlying_pv}") underlying_pv = underlying_pv.replace(".VAL", "") @@ -172,20 +174,16 @@ def generate_alias( # Create a case insensitive alias so clients don't have to worry about getting case right if full_block_pv != full_block_pv.upper(): - lines.append( - "## Add full caps equivalent so clients need not be case sensitive" - ) + lines.append("## Add full caps equivalent so clients need not be case sensitive") lines.extend( - build_block_alias_lines( - full_block_pv.upper(), pv_suffix, underlying_pv, False - ) + build_block_alias_lines(full_block_pv.upper(), pv_suffix, underlying_pv, False) ) lines.append("") # New line to seperate out each block return lines def set_new_aliases( - self, blocks, configures_block_gateway: bool, config_dir: str + self, blocks: dict[str, Block] | None, configures_block_gateway: bool, config_dir: str ) -> None: """Creates the aliases for the blocks and restarts the gateway. diff --git a/BlockServer/epics/procserv_utils.py b/BlockServer/epics/procserv_utils.py index 81beed65..97df02c1 100644 --- a/BlockServer/epics/procserv_utils.py +++ b/BlockServer/epics/procserv_utils.py @@ -22,14 +22,14 @@ class ProcServWrapper: """A wrapper for accessing some of the functionality of ProcServ.""" - def __init__(self, prefix: str): + def __init__(self, prefix: str) -> None: """Constructor. Args: prefix (string): The prefix for the instrument """ self.procserv_prefix = f"{prefix}CS:PS:" - def start_ioc(self, ioc: str): + def start_ioc(self, ioc: str) -> None: """Starts the specified IOC. Args: @@ -38,7 +38,7 @@ def start_ioc(self, ioc: str): print_and_log(f"Starting IOC {ioc}") ChannelAccess.caput(f"{self.procserv_prefix}{ioc}:START", 1) - def stop_ioc(self, ioc: str): + def stop_ioc(self, ioc: str) -> None: """Stops the specified IOC. Args: @@ -47,7 +47,7 @@ def stop_ioc(self, ioc: str): print_and_log(f"Stopping IOC {ioc}") ChannelAccess.caput(f"{self.procserv_prefix}{ioc}:STOP", 1, wait=True) - def restart_ioc(self, ioc: str): + def restart_ioc(self, ioc: str) -> None: """Restarts the specified IOC. Args: @@ -56,7 +56,7 @@ def restart_ioc(self, ioc: str): print_and_log(f"Restarting IOC {ioc}") ChannelAccess.caput(f"{self.procserv_prefix}{ioc}:RESTART", 1) - def ioc_restart_pending(self, ioc: str): + def ioc_restart_pending(self, ioc: str) -> bool: """Tests to see if an IOC restart is pending Args: @@ -67,7 +67,7 @@ def ioc_restart_pending(self, ioc: str): """ return ioc_restart_pending(f"{self.procserv_prefix}{ioc}", ChannelAccess) - def get_ioc_status(self, ioc: str): + def get_ioc_status(self, ioc: str) -> str: """Gets the status of the specified IOC. Args: @@ -82,7 +82,7 @@ def get_ioc_status(self, ioc: str): raise OSError(f"Could not find IOC {ioc} (using pv {pv_name})") return ans.upper() - def toggle_autorestart(self, ioc: str): + def toggle_autorestart(self, ioc: str) -> None: """Toggles the auto-restart property. Args: @@ -93,7 +93,7 @@ def toggle_autorestart(self, ioc: str): ChannelAccess.caput(f"{self.procserv_prefix}{ioc}:TOGGLE", 1) @retry(50, 0.1, ValueError) # Retry for 5 seconds to get a valid value on failure - def get_autorestart(self, ioc: str): + def get_autorestart(self, ioc: str) -> bool: """Gets the current auto-restart setting of the specified IOC. Args: diff --git a/BlockServer/test_modules/test_epics_gateway.py b/BlockServer/test_modules/test_epics_gateway.py index ce62b826..4ac97cbf 100644 --- a/BlockServer/test_modules/test_epics_gateway.py +++ b/BlockServer/test_modules/test_epics_gateway.py @@ -32,9 +32,7 @@ def setUp(self): self.prefix = "INST:" self.block_prefix = self.prefix + "BLOCK:" self.control_sys_prefix = self.prefix + "CONTROL:" - self.config_dir = os.path.join( - os.path.dirname(os.path.realpath(__file__)), "settings" - ) + self.config_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "settings") self.gateway = Gateway( self.gateway_prefix, @@ -53,9 +51,7 @@ def test_GIVEN_gateway_pv_doesnt_exist_WHEN_exist_called_THEN_returns_false( channel_access.caget.assert_called_with(self.gateway_prefix + "pvtotal") @patch("BlockServer.epics.gateway.ChannelAccess") - def test_GIVEN_gateway_pv_exists_WHEN_exist_called_THEN_returns_true( - self, channel_access - ): + def test_GIVEN_gateway_pv_exists_WHEN_exist_called_THEN_returns_true(self, channel_access): channel_access.caget = MagicMock(return_value="Hi") self.assertTrue(self.gateway.exists()) channel_access.caget.assert_called_with(self.gateway_prefix + "pvtotal") @@ -188,15 +184,15 @@ def test_GIVEN_configuration_has_pvlist_WHEN_set_new_aliases_THEN_pvlist_copied( config_dir = os.path.join(self.config_dir, "non_empty") expected_name = "block" expected_pv = "pv" - blocks = [ - Block( + blocks = { + expected_name: Block( expected_name, expected_pv, log_periodic=True, log_rate=0, log_deadband=1, ) - ] + } self.gateway.set_new_aliases(blocks, True, config_dir) @@ -254,6 +250,4 @@ def test_GIVEN_configuration_claims_has_pvlist_BUT_does_not_WHEN_set_new_aliases print("Expected Lines") for line in expected_lines: print(line) - self._assert_lines_correct( - mock_file.file_contents[self.gateway_file_path], expected_lines - ) + self._assert_lines_correct(mock_file.file_contents[self.gateway_file_path], expected_lines) diff --git a/DatabaseServer/database_server.py b/DatabaseServer/database_server.py index b82a8bae..80cc9613 100644 --- a/DatabaseServer/database_server.py +++ b/DatabaseServer/database_server.py @@ -128,24 +128,14 @@ def _generate_pv_acquisition_info(self) -> dict: """ enhanced_info = DatabaseServer.generate_pv_info() - def add_get_method( - pv: str, get_function: Callable[[], list | str | dict] - ) -> None: + def add_get_method(pv: str, get_function: Callable[[], list | str | dict]) -> None: enhanced_info[pv]["get"] = get_function add_get_method(DbPVNames.IOCS, self._get_iocs_info) - add_get_method( - DbPVNames.HIGH_INTEREST, partial(self._get_interesting_pvs, "HIGH") - ) - add_get_method( - DbPVNames.MEDIUM_INTEREST, partial(self._get_interesting_pvs, "MEDIUM") - ) - add_get_method( - DbPVNames.LOW_INTEREST, partial(self._get_interesting_pvs, "LOW") - ) - add_get_method( - DbPVNames.FACILITY, partial(self._get_interesting_pvs, "FACILITY") - ) + add_get_method(DbPVNames.HIGH_INTEREST, partial(self._get_interesting_pvs, "HIGH")) + add_get_method(DbPVNames.MEDIUM_INTEREST, partial(self._get_interesting_pvs, "MEDIUM")) + add_get_method(DbPVNames.LOW_INTEREST, partial(self._get_interesting_pvs, "LOW")) + add_get_method(DbPVNames.FACILITY, partial(self._get_interesting_pvs, "FACILITY")) add_get_method(DbPVNames.ACTIVE_PVS, self._get_active_pvs) add_get_method(DbPVNames.ALL_PVS, partial(self._get_interesting_pvs, "")) add_get_method(DbPVNames.SAMPLE_PARS, self._get_sample_par_names) @@ -219,11 +209,7 @@ def read(self, reason: str) -> bytes: A compressed and hexed JSON formatted string that gives the desired information based on reason. """ - return ( - self.get_data_for_pv(reason) - if reason in self._pv_info - else self.getParam(reason) - ) + return self.get_data_for_pv(reason) if reason in self._pv_info else self.getParam(reason) def write(self, reason: str, value: str) -> Literal[True]: """ @@ -306,7 +292,8 @@ def _check_pv_capacity(self, pv: str, size: int, prefix: str) -> None: """ if size > self._pv_info[pv]["count"]: print_and_log( - f"Too much data to encode PV {prefix + pv}. Current size is {self._pv_info[pv]['count']} characters " + f"Too much data to encode PV {prefix + pv}. " + f"Current size is {self._pv_info[pv]['count']} characters " f"but {size} are required", MAJOR_MSG, LOG_TARGET, @@ -457,9 +444,7 @@ def _get_iocs_not_to_stop() -> list: BLOCKSERVER_PREFIX = args.blockserver_prefix[0] if not BLOCKSERVER_PREFIX.endswith(":"): BLOCKSERVER_PREFIX += ":" - BLOCKSERVER_PREFIX = BLOCKSERVER_PREFIX.replace( - "%MYPVPREFIX%", MACROS["$(MYPVPREFIX)"] - ) + BLOCKSERVER_PREFIX = BLOCKSERVER_PREFIX.replace("%MYPVPREFIX%", MACROS["$(MYPVPREFIX)"]) print_and_log(f"BLOCKSERVER PREFIX = {BLOCKSERVER_PREFIX}", INFO_MSG, LOG_TARGET) OPTIONS_DIR = os.path.abspath(args.options_dir[0]) @@ -499,12 +484,11 @@ def _get_iocs_not_to_stop() -> list: if exp_data is None: try: exp_data = ExpData(MACROS["$(MYPVPREFIX)"], ExpDataSource()) - print_and_log( - "Connected to experimental details database", INFO_MSG, LOG_TARGET - ) + print_and_log("Connected to experimental details database", INFO_MSG, LOG_TARGET) except (OSError, mysql.connector.Error): print_and_log( - f"Problem connecting to experimental details database: {traceback.format_exc()}", + f"Problem connecting to experimental details database: " + f"{traceback.format_exc()}", MAJOR_MSG, LOG_TARGET, ) @@ -515,9 +499,7 @@ def _get_iocs_not_to_stop() -> list: MoxaDataSource(SQLAbstraction("moxa_details", "moxas", "$moxas")), MACROS["$(MYPVPREFIX)"], ) - print_and_log( - "Connected to moxa details database", INFO_MSG, LOG_TARGET - ) + print_and_log("Connected to moxa details database", INFO_MSG, LOG_TARGET) except (OSError, mysql.connector.Error): print_and_log( f"Problem connecting to moxa details database: {traceback.format_exc()}", @@ -531,9 +513,7 @@ def _get_iocs_not_to_stop() -> list: else: sleep(15) - DRIVER = DatabaseServer( - SERVER, ioc_data, exp_data, moxa_data, OPTIONS_DIR, BLOCKSERVER_PREFIX - ) + DRIVER = DatabaseServer(SERVER, ioc_data, exp_data, moxa_data, OPTIONS_DIR, BLOCKSERVER_PREFIX) # Process CA transactions while True: diff --git a/DatabaseServer/exp_data.py b/DatabaseServer/exp_data.py index 8dd17e6a..838f2b27 100644 --- a/DatabaseServer/exp_data.py +++ b/DatabaseServer/exp_data.py @@ -72,9 +72,7 @@ def get_team(self, experiment_id: str) -> list: return [] team = [list(element) for element in result] if len(team) == 0: - raise ValueError( - f"unable to find team details for experiment ID {experiment_id}" - ) + raise ValueError(f"unable to find team details for experiment ID {experiment_id}") else: return team except (OSError, ValueError, AssertionError): @@ -279,6 +277,4 @@ def make_name_list_ascii(names: list) -> bytes: nlist = ",".join(names) nfkd_form = unicodedata.normalize("NFKD", nlist) nlist_no_sc = "".join([c for c in nfkd_form if not unicodedata.combining(c)]) - return nlist_no_sc.translate(ExpData._make_ascii_mappings()).encode( - "ascii", "ignore" - ) + return nlist_no_sc.translate(ExpData._make_ascii_mappings()).encode("ascii", "ignore") diff --git a/block_server.py b/block_server.py index 07250310..c5e4b77e 100644 --- a/block_server.py +++ b/block_server.py @@ -179,9 +179,7 @@ def __init__(self, ca_server: CAServer) -> None: print_and_log(f"Configurations not under version control: {err}", "MINOR") self._config_vc = MockVersionControl() except VersionControlException as err: - print_and_log( - f"Unable to initialise config version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise config version control: {err}", "MINOR") self._config_vc = MockVersionControl() try: @@ -197,14 +195,10 @@ def __init__(self, ca_server: CAServer) -> None: print_and_log(f"Scripts not under version control: {err}", "MINOR") self._script_vc = MockVersionControl() except VersionControlException as err: - print_and_log( - f"Unable to initialise script version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise script version control: {err}", "MINOR") self._script_vc = MockVersionControl() except Exception as err: # noqa: BLE001 deliberate fallback to MockVersionControl - print_and_log( - f"Unable to initialise script version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise script version control: {err}", "MINOR") self._script_vc = MockVersionControl() # Import data about all configs @@ -213,8 +207,7 @@ def __init__(self, ca_server: CAServer) -> None: except OSError as err: print_and_log( "Error creating inactive config list. " - "Configuration list changes will not be stored " - + f"in version control: {err!s} ", + "Configuration list changes will not be stored " + f"in version control: {err!s} ", "MINOR", ) self._config_list = ConfigListManager(self, ConfigurationFileManager()) @@ -224,9 +217,7 @@ def __init__(self, ca_server: CAServer) -> None: write_thread.daemon = True # Daemonise thread write_thread.start() - self.write_queue.put( - (self.initialise_configserver, (FACILITY,), "INITIALISING") - ) + self.write_queue.put((self.initialise_configserver, (FACILITY,), "INITIALISING")) # Starts the Web Server self.server = Server() self.server.start() @@ -281,9 +272,7 @@ def initialise_configserver(self, facility: str) -> None: print_and_log("Could not connect to gateway - is it running?") self.load_last_config() except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log( - f"Could not load last configuration. Message was: {err}", "MAJOR" - ) + print_and_log(f"Could not load last configuration. Message was: {err}", "MAJOR") self._active_configserver.clear_config() self._initialise_config() @@ -308,13 +297,9 @@ def read(self, reason: str) -> str: ) value = compress_and_hex(grps) elif reason == BlockserverPVNames.CONFIGS: - value = compress_and_hex( - convert_to_json(self._config_list.get_configs()) - ) + value = compress_and_hex(convert_to_json(self._config_list.get_configs())) elif reason == BlockserverPVNames.COMPS: - value = compress_and_hex( - convert_to_json(self._config_list.get_components()) - ) + value = compress_and_hex(convert_to_json(self._config_list.get_components())) elif reason == BlockserverPVNames.BLANK_CONFIG: js = convert_to_json(self.get_blank_config()) value = compress_and_hex(js) @@ -358,21 +343,13 @@ def write(self, reason: str, value: str) -> bool: # type: ignore """ status = True try: - data = ( - dehex_and_decompress(bytes(value, encoding="utf-8")) - .strip(b'"') - .decode("utf-8") - ) + data = dehex_and_decompress(bytes(value, encoding="utf-8")).strip(b'"').decode("utf-8") if reason == BlockserverPVNames.LOAD_CONFIG: self.write_queue.put((self.load_config, (data,), "LOADING_CONFIG")) elif reason == BlockserverPVNames.RELOAD_CURRENT_CONFIG: - self.write_queue.put( - (self.reload_current_config, (), "RELOAD_CURRENT_CONFIG") - ) + self.write_queue.put((self.reload_current_config, (), "RELOAD_CURRENT_CONFIG")) elif reason == BlockserverPVNames.START_IOCS: - self.write_queue.put( - (self.start_iocs, (convert_from_json(data),), "START_IOCS") - ) + self.write_queue.put((self.start_iocs, (convert_from_json(data),), "START_IOCS")) elif reason == BlockserverPVNames.STOP_IOCS: self.write_queue.put( ( @@ -402,9 +379,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore elif reason == BlockserverPVNames.SAVE_NEW_CONFIG: self.write_queue.put((self.save_config, (data,), "SAVING_NEW_CONFIG")) elif reason == BlockserverPVNames.SAVE_NEW_COMPONENT: - self.write_queue.put( - (self.save_config, (data, True), "SAVING_NEW_COMP") - ) + self.write_queue.put((self.save_config, (data, True), "SAVING_NEW_COMP")) elif reason == BlockserverPVNames.DELETE_CONFIGS: self.write_queue.put( ( @@ -532,9 +507,7 @@ def _initialise_config(self, full_init: bool = False) -> None: e.g. on loading a new configuration """ if self._active_configserver is not None: - new_iocs, changed_iocs, removed_iocs = ( - self._active_configserver.iocs_changed() - ) + new_iocs, changed_iocs, removed_iocs = self._active_configserver.iocs_changed() self._ioc_control.stop_iocs(list(removed_iocs)) self._start_config_iocs(list(new_iocs), list(changed_iocs)) if ( @@ -543,18 +516,13 @@ def _initialise_config(self, full_init: bool = False) -> None: ): # See https://github.com/ISISComputingGroup/IBEX/issues/5590 for # justification of why this ioc gets special treatment. - ioc = self._active_configserver.get_all_ioc_details()[ - CAEN_DISCRIMINATOR_IOC_NAME - ] + ioc = self._active_configserver.get_all_ioc_details()[CAEN_DISCRIMINATOR_IOC_NAME] if ioc.autostart: print_and_log( f"{CAEN_DISCRIMINATOR_IOC_NAME} present in configuration and set " f"to autostart - restarting it" ) - if ( - self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) - == "RUNNING" - ): + if self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) == "RUNNING": self._ioc_control.restart_iocs( [CAEN_DISCRIMINATOR_IOC_NAME], reapply_auto=True ) @@ -573,12 +541,8 @@ def _initialise_config(self, full_init: bool = False) -> None: ), ) - self._config_list.active_config_name = ( - self._active_configserver.get_config_name() - ) - self._config_list.active_components = ( - self._active_configserver.get_component_names() - ) + self._config_list.active_config_name = self._active_configserver.get_config_name() + self._config_list.active_components = self._active_configserver.get_component_names() self._config_list.update_monitors() self.update_blocks_monitors() @@ -591,16 +555,10 @@ def _initialise_config(self, full_init: bool = False) -> None: handler.on_config_change(full_init=full_init) # Update Web Server text - self.server.set_config( - convert_to_json(self._active_configserver.get_config_details()) - ) - self.write_queue.put( - (self.set_config_block_values, (), "LOADING_BLOCK_SETS") - ) + self.server.set_config(convert_to_json(self._active_configserver.get_config_details())) + self.write_queue.put((self.set_config_block_values, (), "LOADING_BLOCK_SETS")) - def _start_config_iocs( - self, iocs_to_start: list[str], iocs_to_restart: list[str] - ) -> None: + def _start_config_iocs(self, iocs_to_start: list[str], iocs_to_restart: list[str]) -> None: # Start the IOCs, if they are available and if they are flagged for autostart # Note: autostart means the IOC is started when the config is loaded, # restart means the IOC should automatically restart if it stops for some reason @@ -616,12 +574,8 @@ def _should_start(ioc_name: str) -> bool: ioc = _ioc_from_name(ioc_name) return ioc.autostart and not _is_remote(ioc_name) - self._ioc_control.start_iocs( - [ioc for ioc in iocs_to_start if _should_start(ioc)] - ) - self._ioc_control.restart_iocs( - [ioc for ioc in iocs_to_restart if _should_start(ioc)] - ) + self._ioc_control.start_iocs([ioc for ioc in iocs_to_start if _should_start(ioc)]) + self._ioc_control.restart_iocs([ioc for ioc in iocs_to_restart if _should_start(ioc)]) for ioc in iocs_to_start: self._ioc_control.waitfor_running(ioc) @@ -656,9 +610,7 @@ def load_config(self, config: str, full_init: bool = True) -> None: # If we get this far then assume the config is okay self._initialise_config(full_init=full_init) except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log( - f"Exception while loading configuration '{config}': {err}", "MAJOR" - ) + print_and_log(f"Exception while loading configuration '{config}': {err}", "MAJOR") traceback.print_exc() def reload_current_config(self) -> None: @@ -739,11 +691,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if self._active_configserver is not None: # Reload configuration if a component has changed - if ( - as_comp - and new_details["name"] - in self._active_configserver.get_component_names() - ): + if as_comp and new_details["name"] in self._active_configserver.get_component_names(): self.load_last_config() # If the configuration we are trying to save is the currently active one, @@ -752,9 +700,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if config_name == self._active_configserver.get_config_name(): self.load_config(config_name, full_init=False) - def _get_inactive_history( - self, name: str, is_component: bool = False - ) -> list[str | None]: + def _get_inactive_history(self, name: str, is_component: bool = False) -> list[str | None]: # If it already exists load it try: inactive = InactiveConfigHolder(MACROS, ConfigurationFileManager()) @@ -775,12 +721,8 @@ def update_blocks_monitors(self) -> None: """Updates the PV monitors for the blocks and groups, so the clients can see any changes.""" if self._active_configserver is not None: with self.monitor_lock: - block_names = convert_to_json( - self._active_configserver.get_blocknames() - ) - self.setParam( - BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names) - ) + block_names = convert_to_json(self._active_configserver.get_blocknames()) + self.setParam(BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names)) groups = ConfigurationJsonConverter.groups_to_json( self._active_configserver.get_group_details() From 1c82a3b12f754e3e22e87531bdd46ae5d9f3529a Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 12 Nov 2026 14:30:10 +0000 Subject: [PATCH 08/12] ruff/pyright --- DatabaseServer/test_modules/test_exp_data.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DatabaseServer/test_modules/test_exp_data.py b/DatabaseServer/test_modules/test_exp_data.py index c135b184..7fa5674d 100644 --- a/DatabaseServer/test_modules/test_exp_data.py +++ b/DatabaseServer/test_modules/test_exp_data.py @@ -1,4 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals import json @@ -20,12 +19,13 @@ import typing import unittest -from DatabaseServer.exp_data import ExpData from server_common.mocks.mock_ca import MockChannelAccess from server_common.utilities import dehex_and_decompress +from DatabaseServer.exp_data import ExpData + -class MockExpDataSource(object): +class MockExpDataSource: def __init__(self): self.valid_experiments = { "123456": [["Matt", "ESS", "PI"], ["Dom", "ISIS", ""], ["Jack", "ISIS", "Contact"]] From a5191d3402f36971dde14c7d138698b1a4b5a744 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 12 Nov 2026 14:40:47 +0000 Subject: [PATCH 09/12] ruff --- DatabaseServer/test_modules/test_exp_data.py | 1 - 1 file changed, 1 deletion(-) diff --git a/DatabaseServer/test_modules/test_exp_data.py b/DatabaseServer/test_modules/test_exp_data.py index 7fa5674d..e14ee5a8 100644 --- a/DatabaseServer/test_modules/test_exp_data.py +++ b/DatabaseServer/test_modules/test_exp_data.py @@ -1,4 +1,3 @@ - import json # This file is part of the ISIS IBEX application. From fc94441ca31106b74ce1f5cae6f535b74c90b3d6 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Thu, 12 Nov 2026 16:04:33 +0000 Subject: [PATCH 10/12] adds dbserver tests --- .../test_block_server_restart_services.py | 49 +++++++++++++++++++ .../test_modules/test_database_server.py | 33 ++++++++++++- block_server.py | 4 +- 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 BlockServer/test_modules/test_block_server_restart_services.py diff --git a/BlockServer/test_modules/test_block_server_restart_services.py b/BlockServer/test_modules/test_block_server_restart_services.py new file mode 100644 index 00000000..da76b95a --- /dev/null +++ b/BlockServer/test_modules/test_block_server_restart_services.py @@ -0,0 +1,49 @@ +# This file is part of the ISIS IBEX application. +# Copyright (C) 2012-2026 Science & Technology Facilities Council. +# All rights reserved. +# +# This program is distributed in the hope that it will be useful. +# This program and the accompanying materials are made available under the +# terms of the Eclipse Public License v1.0 which accompanies this distribution. +# EXCEPT AS EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM +# AND ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES +# OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more details. +# +# You should have received a copy of the Eclipse Public License v1.0 +# along with this program; if not, you can obtain a copy from +# https://www.eclipse.org/org/documents/epl-v10.php or +# http://opensource.org/licenses/eclipse-1.0.php + +import unittest + + +@unittest.skip("Not yet implemented") +class TestBlockServerRestartServiceIoc(unittest.TestCase): + def test_WHEN_restart_service_ioc_pv_written_THEN_restart_queued_with_iocs(self): + """Writing a list like ["DBSVR"] queues restart_service_ioc with that list.""" + + def test_GIVEN_manager_mode_WHEN_blocksvr_restarted_THEN_blockserver_restarts(self): + """"BLOCKSVR" calls self._restart().""" + + def test_GIVEN_manager_mode_WHEN_gwblock_restarted_THEN_gateway_reloaded(self): + """"GWBLOCK" calls self._gateway.reload().""" + + def test_GIVEN_manager_mode_WHEN_dbsvr_restarted_THEN_database_server_restart_pv_written( + self, + ): + """"DBSVR" caputs 1 to CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART.""" + + def test_GIVEN_manager_mode_WHEN_other_service_ioc_restarted_THEN_restarted_via_ioc_control( + self, + ): + """e.g. "INSTETC" calls ioc_control.restart_ioc("INSTETC", force=True).""" + + def test_GIVEN_ioc_not_in_service_list_WHEN_restarted_THEN_nothing_restarted(self): + """A normal IOC is ignored: no restart, reload or caput.""" + + def test_GIVEN_not_manager_mode_WHEN_service_ioc_restarted_THEN_nothing_restarted(self): + """ManagerModeRequiredError is raised and no restart happens.""" + + +if __name__ == "__main__": + unittest.main() diff --git a/DatabaseServer/test_modules/test_database_server.py b/DatabaseServer/test_modules/test_database_server.py index 38730c37..f9cd5444 100644 --- a/DatabaseServer/test_modules/test_database_server.py +++ b/DatabaseServer/test_modules/test_database_server.py @@ -17,8 +17,9 @@ # http://opensource.org/licenses/eclipse-1.0.php import json import os - +from unittest.mock import patch import mock +from server_common import channel_access os.environ["MYDIRBLOCK"] = os.path.abspath("..") os.environ["MYPVPREFIX"] = "" @@ -123,3 +124,33 @@ def test_iocs_pvs_correct(self): self.assertTrue( name in pv_data, msg="{name} in {pv_names}".format(name=name, pv_names=pv_data) ) + @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") + def test_GIVEN_manager_mode_WHEN_restart_pv_written_THEN_process_restarts(self): + """Writing to DatabasePVNames.RESTART calls os._exit(0) (patched).""" + + with ( + patch( + "DatabaseServer.database_server.verify_manager_mode", + ) as mock_verify_manager_mode, + patch.object(os, "_exit") as mock_exit, + ): + self.db_server.write(reason=DatabasePVNames.RESTART, value="1") + + mock_verify_manager_mode.assert_called_once() + mock_exit.assert_called_with(0) + + @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") + def test_GIVEN_not_manager_mode_WHEN_restart_pv_written_THEN_process_does_not_restart(self): + """ManagerModeRequiredError is raised and os._exit is not called.""" + with ( + patch( + "DatabaseServer.database_server.verify_manager_mode", + side_effect=channel_access.ManagerModeRequiredError("not manager"), + ) as mock_verify_manager_mode, + patch.object(os, "_exit") as mock_exit, + ): + with self.assertRaises(channel_access.ManagerModeRequiredError): + self.db_server.write(reason=DatabasePVNames.RESTART, value="1") + + mock_verify_manager_mode.assert_called_once() + mock_exit.assert_not_called() diff --git a/block_server.py b/block_server.py index c5e4b77e..7cbd4d84 100644 --- a/block_server.py +++ b/block_server.py @@ -659,7 +659,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: "component" if as_comp else "config", config_name ) ) - except OSError: + except IOError: pass # IOError thrown if config we're overwriting didn't exist, i.e. # this is a brand new config/component. @@ -707,7 +707,7 @@ def _get_inactive_history(self, name: str, is_component: bool = False) -> list[s inactive.load_inactive(name, is_component) # Get previous history history = inactive.get_history() - except OSError: + except IOError: # Config doesn't exist therefore start new history history = [] return history From 30f9e86bf511c4d2c49ee612fe64b13fe0368ae2 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Fri, 13 Nov 2026 11:01:25 +0000 Subject: [PATCH 11/12] add tests --- .../test_block_server_restart_services.py | 92 ++++++++++-- block_server.py | 141 ++++++++++++++---- 2 files changed, 191 insertions(+), 42 deletions(-) diff --git a/BlockServer/test_modules/test_block_server_restart_services.py b/BlockServer/test_modules/test_block_server_restart_services.py index da76b95a..fc4c09db 100644 --- a/BlockServer/test_modules/test_block_server_restart_services.py +++ b/BlockServer/test_modules/test_block_server_restart_services.py @@ -15,34 +15,108 @@ # http://opensource.org/licenses/eclipse-1.0.php import unittest +from unittest.mock import patch + +from ibex_non_ca_helpers.compress_hex import compress_and_hex +from server_common.channel_access import ManagerModeRequiredError +from server_common.helpers import CONTROL_SYSTEM_PREFIX +from server_common.mocks.mock_ca_server import MockCAServer +from server_common.pv_names import BlockserverPVNames, DatabasePVNames + +from block_server import BlockServer -@unittest.skip("Not yet implemented") class TestBlockServerRestartServiceIoc(unittest.TestCase): + def setUp(self): + self.block_server = BlockServer(MockCAServer(), test_mode=True) + + gateway_patcher = patch.object(self.block_server, "_gateway", create=True) + self.mock_gateway = gateway_patcher.start() + self.addCleanup(gateway_patcher.stop) + + ioc_control_patcher = patch.object( + self.block_server, "_ioc_control", create=True + ) + self.mock_ioc_control = ioc_control_patcher.start() + self.addCleanup(ioc_control_patcher.stop) + + verify_patcher = patch("block_server.verify_manager_mode") + self.mock_verify_manager_mode = verify_patcher.start() + self.addCleanup(verify_patcher.stop) + + ca_patcher = patch("block_server.ChannelAccess") + self.mock_channel_access = ca_patcher.start() + self.addCleanup(ca_patcher.stop) + + exit_patcher = patch("block_server.os._exit") + self.mock_exit = exit_patcher.start() + self.addCleanup(exit_patcher.stop) + def test_WHEN_restart_service_ioc_pv_written_THEN_restart_queued_with_iocs(self): - """Writing a list like ["DBSVR"] queues restart_service_ioc with that list.""" + value = compress_and_hex('["DBSVR"]').decode("utf-8") + + status = self.block_server.write(BlockserverPVNames.RESTART_SERVICE_IOC, value) + + self.assertTrue(status) + self.assertEqual( + self.block_server.write_queue.get_nowait(), + ( + self.block_server.restart_service_ioc, + (["DBSVR"], True), + "RESTART_SERVICE_IOC", + ), + ) def test_GIVEN_manager_mode_WHEN_blocksvr_restarted_THEN_blockserver_restarts(self): - """"BLOCKSVR" calls self._restart().""" + self.block_server.restart_service_ioc(["BLOCKSVR"]) + + self.mock_verify_manager_mode.assert_called_once() + self.mock_exit.assert_called_once_with(0) def test_GIVEN_manager_mode_WHEN_gwblock_restarted_THEN_gateway_reloaded(self): - """"GWBLOCK" calls self._gateway.reload().""" + self.block_server.restart_service_ioc(["GWBLOCK"]) + + self.mock_verify_manager_mode.assert_called_once() + self.mock_gateway.reload.assert_called_once() def test_GIVEN_manager_mode_WHEN_dbsvr_restarted_THEN_database_server_restart_pv_written( self, ): - """"DBSVR" caputs 1 to CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART.""" + self.block_server.restart_service_ioc(["DBSVR"]) + + self.mock_verify_manager_mode.assert_called_once() + self.mock_channel_access.caput.assert_called_once_with( + CONTROL_SYSTEM_PREFIX + DatabasePVNames.RESTART, 1 + ) def test_GIVEN_manager_mode_WHEN_other_service_ioc_restarted_THEN_restarted_via_ioc_control( self, ): - """e.g. "INSTETC" calls ioc_control.restart_ioc("INSTETC", force=True).""" + self.block_server.restart_service_ioc(["INSTETC"]) + + self.mock_verify_manager_mode.assert_called_once() + self.mock_ioc_control.restart_ioc.assert_called_once_with("INSTETC", force=True) def test_GIVEN_ioc_not_in_service_list_WHEN_restarted_THEN_nothing_restarted(self): - """A normal IOC is ignored: no restart, reload or caput.""" + self.block_server.restart_service_ioc(["SIMPLE"]) + + self.mock_verify_manager_mode.assert_not_called() + self.mock_exit.assert_not_called() + self.mock_channel_access.caput.assert_not_called() + self.mock_gateway.reload.assert_not_called() + self.mock_ioc_control.restart_ioc.assert_not_called() + + def test_GIVEN_not_manager_mode_WHEN_service_ioc_restarted_THEN_nothing_restarted( + self, + ): + self.mock_verify_manager_mode.side_effect = ManagerModeRequiredError( + "not manager" + ) + + with self.assertRaises(ManagerModeRequiredError): + self.block_server.restart_service_ioc(["INSTETC"]) - def test_GIVEN_not_manager_mode_WHEN_service_ioc_restarted_THEN_nothing_restarted(self): - """ManagerModeRequiredError is raised and no restart happens.""" + self.mock_ioc_control.restart_ioc.assert_not_called() if __name__ == "__main__": diff --git a/block_server.py b/block_server.py index 7cbd4d84..a6e71d76 100644 --- a/block_server.py +++ b/block_server.py @@ -22,11 +22,13 @@ import traceback import zlib import zoneinfo +from pathlib import Path from typing import TYPE_CHECKING, Any from server_common.channel_access import ManagerModeRequiredError, verify_manager_mode from server_common.channel_access_server import CAServer from server_common.constants import SERVICE_IOCS +from server_common.mocks.mock_ca_server import MockCAServer sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) @@ -89,6 +91,9 @@ CURR_CONFIG_NAME_SEVR_VALUE = 0 CONFIG_PUSH_TIME = 300 # 5 minutes INST_SCRIPT_PUSH_TIME = 604800 # 7 days +CONFIG_DIR = "" +SCRIPT_DIR = "" +SCHEMA_DIR = "" # This IOC gets special treatment as it needs to be reloaded on every single config change, # regardless of whether it's macros have changed or not. @@ -132,12 +137,22 @@ class BlockServer(Driver): """The class for handling all the static PV access and monitors etc.""" - def __init__(self, ca_server: CAServer) -> None: + def __init__( + self, ca_server: CAServer | MockCAServer, test_mode: bool = False + ) -> None: """Constructor. Args: ca_server (CAServer): The CA server used for generating PVs on the fly """ + + self._test_mode = test_mode + if self._test_mode: + self.write_queue = Queue() + self._service_iocs = SERVICE_IOCS + self.on_the_fly_handlers = [] + return + super().__init__() # Threading stuff @@ -179,7 +194,9 @@ def __init__(self, ca_server: CAServer) -> None: print_and_log(f"Configurations not under version control: {err}", "MINOR") self._config_vc = MockVersionControl() except VersionControlException as err: - print_and_log(f"Unable to initialise config version control: {err}", "MINOR") + print_and_log( + f"Unable to initialise config version control: {err}", "MINOR" + ) self._config_vc = MockVersionControl() try: @@ -195,10 +212,14 @@ def __init__(self, ca_server: CAServer) -> None: print_and_log(f"Scripts not under version control: {err}", "MINOR") self._script_vc = MockVersionControl() except VersionControlException as err: - print_and_log(f"Unable to initialise script version control: {err}", "MINOR") + print_and_log( + f"Unable to initialise script version control: {err}", "MINOR" + ) self._script_vc = MockVersionControl() except Exception as err: # noqa: BLE001 deliberate fallback to MockVersionControl - print_and_log(f"Unable to initialise script version control: {err}", "MINOR") + print_and_log( + f"Unable to initialise script version control: {err}", "MINOR" + ) self._script_vc = MockVersionControl() # Import data about all configs @@ -207,7 +228,8 @@ def __init__(self, ca_server: CAServer) -> None: except OSError as err: print_and_log( "Error creating inactive config list. " - "Configuration list changes will not be stored " + f"in version control: {err!s} ", + "Configuration list changes will not be stored " + + f"in version control: {err!s} ", "MINOR", ) self._config_list = ConfigListManager(self, ConfigurationFileManager()) @@ -217,7 +239,9 @@ def __init__(self, ca_server: CAServer) -> None: write_thread.daemon = True # Daemonise thread write_thread.start() - self.write_queue.put((self.initialise_configserver, (FACILITY,), "INITIALISING")) + self.write_queue.put( + (self.initialise_configserver, (FACILITY,), "INITIALISING") + ) # Starts the Web Server self.server = Server() self.server.start() @@ -254,13 +278,15 @@ def initialise_configserver(self, facility: str) -> None: # Import all the synoptic data and create PVs print_and_log("Creating synoptic manager...") if SCHEMA_DIR is not None: - self._syn = SynopticManager(self, SCHEMA_DIR, self._active_configserver) + self._syn = SynopticManager( + self, Path(SCHEMA_DIR), self._active_configserver + ) self.on_the_fly_handlers.append(self._syn) print_and_log("Finished creating synoptic manager") # Import all the devices data and create PVs print_and_log("Creating devices manager...") - self._devices = DevicesManager(self, SCHEMA_DIR) + self._devices = DevicesManager(self, Path(SCHEMA_DIR)) self.on_the_fly_handlers.append(self._devices) print_and_log("Finished creating devices manager") @@ -272,7 +298,9 @@ def initialise_configserver(self, facility: str) -> None: print_and_log("Could not connect to gateway - is it running?") self.load_last_config() except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log(f"Could not load last configuration. Message was: {err}", "MAJOR") + print_and_log( + f"Could not load last configuration. Message was: {err}", "MAJOR" + ) self._active_configserver.clear_config() self._initialise_config() @@ -297,9 +325,13 @@ def read(self, reason: str) -> str: ) value = compress_and_hex(grps) elif reason == BlockserverPVNames.CONFIGS: - value = compress_and_hex(convert_to_json(self._config_list.get_configs())) + value = compress_and_hex( + convert_to_json(self._config_list.get_configs()) + ) elif reason == BlockserverPVNames.COMPS: - value = compress_and_hex(convert_to_json(self._config_list.get_components())) + value = compress_and_hex( + convert_to_json(self._config_list.get_components()) + ) elif reason == BlockserverPVNames.BLANK_CONFIG: js = convert_to_json(self.get_blank_config()) value = compress_and_hex(js) @@ -343,13 +375,21 @@ def write(self, reason: str, value: str) -> bool: # type: ignore """ status = True try: - data = dehex_and_decompress(bytes(value, encoding="utf-8")).strip(b'"').decode("utf-8") + data = ( + dehex_and_decompress(bytes(value, encoding="utf-8")) + .strip(b'"') + .decode("utf-8") + ) if reason == BlockserverPVNames.LOAD_CONFIG: self.write_queue.put((self.load_config, (data,), "LOADING_CONFIG")) elif reason == BlockserverPVNames.RELOAD_CURRENT_CONFIG: - self.write_queue.put((self.reload_current_config, (), "RELOAD_CURRENT_CONFIG")) + self.write_queue.put( + (self.reload_current_config, (), "RELOAD_CURRENT_CONFIG") + ) elif reason == BlockserverPVNames.START_IOCS: - self.write_queue.put((self.start_iocs, (convert_from_json(data),), "START_IOCS")) + self.write_queue.put( + (self.start_iocs, (convert_from_json(data),), "START_IOCS") + ) elif reason == BlockserverPVNames.STOP_IOCS: self.write_queue.put( ( @@ -379,7 +419,9 @@ def write(self, reason: str, value: str) -> bool: # type: ignore elif reason == BlockserverPVNames.SAVE_NEW_CONFIG: self.write_queue.put((self.save_config, (data,), "SAVING_NEW_CONFIG")) elif reason == BlockserverPVNames.SAVE_NEW_COMPONENT: - self.write_queue.put((self.save_config, (data, True), "SAVING_NEW_COMP")) + self.write_queue.put( + (self.save_config, (data, True), "SAVING_NEW_COMP") + ) elif reason == BlockserverPVNames.DELETE_CONFIGS: self.write_queue.put( ( @@ -415,7 +457,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore value = str(compress_and_hex(convert_to_json("OK"))) # store the values - if status: + if status and not self._test_mode: self.setParam(reason, value) return status @@ -507,7 +549,9 @@ def _initialise_config(self, full_init: bool = False) -> None: e.g. on loading a new configuration """ if self._active_configserver is not None: - new_iocs, changed_iocs, removed_iocs = self._active_configserver.iocs_changed() + new_iocs, changed_iocs, removed_iocs = ( + self._active_configserver.iocs_changed() + ) self._ioc_control.stop_iocs(list(removed_iocs)) self._start_config_iocs(list(new_iocs), list(changed_iocs)) if ( @@ -516,13 +560,18 @@ def _initialise_config(self, full_init: bool = False) -> None: ): # See https://github.com/ISISComputingGroup/IBEX/issues/5590 for # justification of why this ioc gets special treatment. - ioc = self._active_configserver.get_all_ioc_details()[CAEN_DISCRIMINATOR_IOC_NAME] + ioc = self._active_configserver.get_all_ioc_details()[ + CAEN_DISCRIMINATOR_IOC_NAME + ] if ioc.autostart: print_and_log( f"{CAEN_DISCRIMINATOR_IOC_NAME} present in configuration and set " f"to autostart - restarting it" ) - if self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) == "RUNNING": + if ( + self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) + == "RUNNING" + ): self._ioc_control.restart_iocs( [CAEN_DISCRIMINATOR_IOC_NAME], reapply_auto=True ) @@ -541,8 +590,12 @@ def _initialise_config(self, full_init: bool = False) -> None: ), ) - self._config_list.active_config_name = self._active_configserver.get_config_name() - self._config_list.active_components = self._active_configserver.get_component_names() + self._config_list.active_config_name = ( + self._active_configserver.get_config_name() + ) + self._config_list.active_components = ( + self._active_configserver.get_component_names() + ) self._config_list.update_monitors() self.update_blocks_monitors() @@ -555,10 +608,16 @@ def _initialise_config(self, full_init: bool = False) -> None: handler.on_config_change(full_init=full_init) # Update Web Server text - self.server.set_config(convert_to_json(self._active_configserver.get_config_details())) - self.write_queue.put((self.set_config_block_values, (), "LOADING_BLOCK_SETS")) + self.server.set_config( + convert_to_json(self._active_configserver.get_config_details()) + ) + self.write_queue.put( + (self.set_config_block_values, (), "LOADING_BLOCK_SETS") + ) - def _start_config_iocs(self, iocs_to_start: list[str], iocs_to_restart: list[str]) -> None: + def _start_config_iocs( + self, iocs_to_start: list[str], iocs_to_restart: list[str] + ) -> None: # Start the IOCs, if they are available and if they are flagged for autostart # Note: autostart means the IOC is started when the config is loaded, # restart means the IOC should automatically restart if it stops for some reason @@ -574,8 +633,12 @@ def _should_start(ioc_name: str) -> bool: ioc = _ioc_from_name(ioc_name) return ioc.autostart and not _is_remote(ioc_name) - self._ioc_control.start_iocs([ioc for ioc in iocs_to_start if _should_start(ioc)]) - self._ioc_control.restart_iocs([ioc for ioc in iocs_to_restart if _should_start(ioc)]) + self._ioc_control.start_iocs( + [ioc for ioc in iocs_to_start if _should_start(ioc)] + ) + self._ioc_control.restart_iocs( + [ioc for ioc in iocs_to_restart if _should_start(ioc)] + ) for ioc in iocs_to_start: self._ioc_control.waitfor_running(ioc) @@ -610,7 +673,9 @@ def load_config(self, config: str, full_init: bool = True) -> None: # If we get this far then assume the config is okay self._initialise_config(full_init=full_init) except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log(f"Exception while loading configuration '{config}': {err}", "MAJOR") + print_and_log( + f"Exception while loading configuration '{config}': {err}", "MAJOR" + ) traceback.print_exc() def reload_current_config(self) -> None: @@ -659,7 +724,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: "component" if as_comp else "config", config_name ) ) - except IOError: + except OSError: pass # IOError thrown if config we're overwriting didn't exist, i.e. # this is a brand new config/component. @@ -691,7 +756,11 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if self._active_configserver is not None: # Reload configuration if a component has changed - if as_comp and new_details["name"] in self._active_configserver.get_component_names(): + if ( + as_comp + and new_details["name"] + in self._active_configserver.get_component_names() + ): self.load_last_config() # If the configuration we are trying to save is the currently active one, @@ -700,14 +769,16 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if config_name == self._active_configserver.get_config_name(): self.load_config(config_name, full_init=False) - def _get_inactive_history(self, name: str, is_component: bool = False) -> list[str | None]: + def _get_inactive_history( + self, name: str, is_component: bool = False + ) -> list[str | None]: # If it already exists load it try: inactive = InactiveConfigHolder(MACROS, ConfigurationFileManager()) inactive.load_inactive(name, is_component) # Get previous history history = inactive.get_history() - except IOError: + except OSError: # Config doesn't exist therefore start new history history = [] return history @@ -721,8 +792,12 @@ def update_blocks_monitors(self) -> None: """Updates the PV monitors for the blocks and groups, so the clients can see any changes.""" if self._active_configserver is not None: with self.monitor_lock: - block_names = convert_to_json(self._active_configserver.get_blocknames()) - self.setParam(BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names)) + block_names = convert_to_json( + self._active_configserver.get_blocknames() + ) + self.setParam( + BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names) + ) groups = ConfigurationJsonConverter.groups_to_json( self._active_configserver.get_group_details() From 383b248b017acd5f0a459dc0828d324616934077 Mon Sep 17 00:00:00 2001 From: Jack Doughty Date: Fri, 13 Nov 2026 11:56:18 +0000 Subject: [PATCH 12/12] ruff/pyright --- .../test_block_server_restart_services.py | 8 +- .../test_modules/test_database_server.py | 31 ++--- block_server.py | 120 +++++------------- 3 files changed, 42 insertions(+), 117 deletions(-) diff --git a/BlockServer/test_modules/test_block_server_restart_services.py b/BlockServer/test_modules/test_block_server_restart_services.py index fc4c09db..dc2dc82f 100644 --- a/BlockServer/test_modules/test_block_server_restart_services.py +++ b/BlockServer/test_modules/test_block_server_restart_services.py @@ -34,9 +34,7 @@ def setUp(self): self.mock_gateway = gateway_patcher.start() self.addCleanup(gateway_patcher.stop) - ioc_control_patcher = patch.object( - self.block_server, "_ioc_control", create=True - ) + ioc_control_patcher = patch.object(self.block_server, "_ioc_control", create=True) self.mock_ioc_control = ioc_control_patcher.start() self.addCleanup(ioc_control_patcher.stop) @@ -109,9 +107,7 @@ def test_GIVEN_ioc_not_in_service_list_WHEN_restarted_THEN_nothing_restarted(sel def test_GIVEN_not_manager_mode_WHEN_service_ioc_restarted_THEN_nothing_restarted( self, ): - self.mock_verify_manager_mode.side_effect = ManagerModeRequiredError( - "not manager" - ) + self.mock_verify_manager_mode.side_effect = ManagerModeRequiredError("not manager") with self.assertRaises(ManagerModeRequiredError): self.block_server.restart_service_ioc(["INSTETC"]) diff --git a/DatabaseServer/test_modules/test_database_server.py b/DatabaseServer/test_modules/test_database_server.py index f9cd5444..eee00b38 100644 --- a/DatabaseServer/test_modules/test_database_server.py +++ b/DatabaseServer/test_modules/test_database_server.py @@ -1,5 +1,3 @@ -from __future__ import absolute_import, division, print_function, unicode_literals - # This file is part of the ISIS IBEX application. # Copyright (C) 2012-2016 Science & Technology Facilities Council. # All rights reserved. @@ -17,8 +15,9 @@ # http://opensource.org/licenses/eclipse-1.0.php import json import os +from unittest import mock from unittest.mock import patch -import mock + from server_common import channel_access os.environ["MYDIRBLOCK"] = os.path.abspath("..") @@ -27,8 +26,6 @@ os.environ["ICPCONFIGROOT"] = "" import unittest -from DatabaseServer.database_server import DatabaseServer -from DatabaseServer.mocks.mock_exp_data import MockExpData from server_common.constants import IS_LINUX from server_common.ioc_data import IOCData from server_common.loggers.logger import Logger @@ -44,6 +41,9 @@ ) from server_common.utilities import dehex_and_decompress, set_logger +from DatabaseServer.database_server import DatabaseServer +from DatabaseServer.mocks.mock_exp_data import MockExpData + # Use a dummy logger during tests as real logger requires log server set_logger(Logger()) @@ -79,9 +79,7 @@ def test_interest_high_pvs_correct(self): pv_names = [item[0] for item in pv_data if len(item) > 0] for name in HIGH_PV_NAMES: - self.assertTrue( - name in pv_names, msg="{name} in {pv_names}".format(name=name, pv_names=pv_names) - ) + self.assertTrue(name in pv_names, msg=f"{name} in {pv_names}") @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") def test_interest_medium_pvs_correct(self): @@ -91,9 +89,7 @@ def test_interest_medium_pvs_correct(self): pv_names = [item[0] for item in pv_data if len(item) > 0] for name in MEDIUM_PV_NAMES: - self.assertTrue( - name in pv_names, msg="{name} in {pv_names}".format(name=name, pv_names=pv_names) - ) + self.assertTrue(name in pv_names, msg=f"{name} in {pv_names}") @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") def test_WHEN_asked_for_low_THEN_get_low_pvs(self): @@ -103,9 +99,7 @@ def test_WHEN_asked_for_low_THEN_get_low_pvs(self): pv_names = [item[0] for item in pv_data if len(item) > 0] for name in LOW_PV_NAMES: - self.assertTrue( - name in pv_names, msg="{name} in {pv_names}".format(name=name, pv_names=pv_names) - ) + self.assertTrue(name in pv_names, msg=f"{name} in {pv_names}") @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") def test_interest_facility_pvs_correct(self): @@ -113,17 +107,14 @@ def test_interest_facility_pvs_correct(self): pv_names = [item[0] for item in pv_data if len(item) > 0] for name in FACILITY_PV_NAMES: - self.assertTrue( - name in pv_names, msg="{name} in {pv_names}".format(name=name, pv_names=pv_names) - ) + self.assertTrue(name in pv_names, msg=f"{name} in {pv_names}") @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") def test_iocs_pvs_correct(self): pv_data = json.loads(dehex_and_decompress(self.db_server.read(DatabasePVNames.IOCS))) for name in IOCS: - self.assertTrue( - name in pv_data, msg="{name} in {pv_names}".format(name=name, pv_names=pv_data) - ) + self.assertTrue(name in pv_data, msg=f"{name} in {pv_data}") + @unittest.skipIf(IS_LINUX, "DB server not configured to run properly on Linux build") def test_GIVEN_manager_mode_WHEN_restart_pv_written_THEN_process_restarts(self): """Writing to DatabasePVNames.RESTART calls os._exit(0) (patched).""" diff --git a/block_server.py b/block_server.py index a6e71d76..49c6351e 100644 --- a/block_server.py +++ b/block_server.py @@ -137,9 +137,7 @@ class BlockServer(Driver): """The class for handling all the static PV access and monitors etc.""" - def __init__( - self, ca_server: CAServer | MockCAServer, test_mode: bool = False - ) -> None: + def __init__(self, ca_server: CAServer | MockCAServer, test_mode: bool = False) -> None: """Constructor. Args: @@ -194,9 +192,7 @@ def __init__( print_and_log(f"Configurations not under version control: {err}", "MINOR") self._config_vc = MockVersionControl() except VersionControlException as err: - print_and_log( - f"Unable to initialise config version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise config version control: {err}", "MINOR") self._config_vc = MockVersionControl() try: @@ -212,14 +208,10 @@ def __init__( print_and_log(f"Scripts not under version control: {err}", "MINOR") self._script_vc = MockVersionControl() except VersionControlException as err: - print_and_log( - f"Unable to initialise script version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise script version control: {err}", "MINOR") self._script_vc = MockVersionControl() except Exception as err: # noqa: BLE001 deliberate fallback to MockVersionControl - print_and_log( - f"Unable to initialise script version control: {err}", "MINOR" - ) + print_and_log(f"Unable to initialise script version control: {err}", "MINOR") self._script_vc = MockVersionControl() # Import data about all configs @@ -228,8 +220,7 @@ def __init__( except OSError as err: print_and_log( "Error creating inactive config list. " - "Configuration list changes will not be stored " - + f"in version control: {err!s} ", + "Configuration list changes will not be stored " + f"in version control: {err!s} ", "MINOR", ) self._config_list = ConfigListManager(self, ConfigurationFileManager()) @@ -239,9 +230,7 @@ def __init__( write_thread.daemon = True # Daemonise thread write_thread.start() - self.write_queue.put( - (self.initialise_configserver, (FACILITY,), "INITIALISING") - ) + self.write_queue.put((self.initialise_configserver, (FACILITY,), "INITIALISING")) # Starts the Web Server self.server = Server() self.server.start() @@ -278,9 +267,7 @@ def initialise_configserver(self, facility: str) -> None: # Import all the synoptic data and create PVs print_and_log("Creating synoptic manager...") if SCHEMA_DIR is not None: - self._syn = SynopticManager( - self, Path(SCHEMA_DIR), self._active_configserver - ) + self._syn = SynopticManager(self, Path(SCHEMA_DIR), self._active_configserver) self.on_the_fly_handlers.append(self._syn) print_and_log("Finished creating synoptic manager") @@ -298,9 +285,7 @@ def initialise_configserver(self, facility: str) -> None: print_and_log("Could not connect to gateway - is it running?") self.load_last_config() except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log( - f"Could not load last configuration. Message was: {err}", "MAJOR" - ) + print_and_log(f"Could not load last configuration. Message was: {err}", "MAJOR") self._active_configserver.clear_config() self._initialise_config() @@ -325,13 +310,9 @@ def read(self, reason: str) -> str: ) value = compress_and_hex(grps) elif reason == BlockserverPVNames.CONFIGS: - value = compress_and_hex( - convert_to_json(self._config_list.get_configs()) - ) + value = compress_and_hex(convert_to_json(self._config_list.get_configs())) elif reason == BlockserverPVNames.COMPS: - value = compress_and_hex( - convert_to_json(self._config_list.get_components()) - ) + value = compress_and_hex(convert_to_json(self._config_list.get_components())) elif reason == BlockserverPVNames.BLANK_CONFIG: js = convert_to_json(self.get_blank_config()) value = compress_and_hex(js) @@ -375,21 +356,13 @@ def write(self, reason: str, value: str) -> bool: # type: ignore """ status = True try: - data = ( - dehex_and_decompress(bytes(value, encoding="utf-8")) - .strip(b'"') - .decode("utf-8") - ) + data = dehex_and_decompress(bytes(value, encoding="utf-8")).strip(b'"').decode("utf-8") if reason == BlockserverPVNames.LOAD_CONFIG: self.write_queue.put((self.load_config, (data,), "LOADING_CONFIG")) elif reason == BlockserverPVNames.RELOAD_CURRENT_CONFIG: - self.write_queue.put( - (self.reload_current_config, (), "RELOAD_CURRENT_CONFIG") - ) + self.write_queue.put((self.reload_current_config, (), "RELOAD_CURRENT_CONFIG")) elif reason == BlockserverPVNames.START_IOCS: - self.write_queue.put( - (self.start_iocs, (convert_from_json(data),), "START_IOCS") - ) + self.write_queue.put((self.start_iocs, (convert_from_json(data),), "START_IOCS")) elif reason == BlockserverPVNames.STOP_IOCS: self.write_queue.put( ( @@ -419,9 +392,7 @@ def write(self, reason: str, value: str) -> bool: # type: ignore elif reason == BlockserverPVNames.SAVE_NEW_CONFIG: self.write_queue.put((self.save_config, (data,), "SAVING_NEW_CONFIG")) elif reason == BlockserverPVNames.SAVE_NEW_COMPONENT: - self.write_queue.put( - (self.save_config, (data, True), "SAVING_NEW_COMP") - ) + self.write_queue.put((self.save_config, (data, True), "SAVING_NEW_COMP")) elif reason == BlockserverPVNames.DELETE_CONFIGS: self.write_queue.put( ( @@ -549,9 +520,7 @@ def _initialise_config(self, full_init: bool = False) -> None: e.g. on loading a new configuration """ if self._active_configserver is not None: - new_iocs, changed_iocs, removed_iocs = ( - self._active_configserver.iocs_changed() - ) + new_iocs, changed_iocs, removed_iocs = self._active_configserver.iocs_changed() self._ioc_control.stop_iocs(list(removed_iocs)) self._start_config_iocs(list(new_iocs), list(changed_iocs)) if ( @@ -560,18 +529,13 @@ def _initialise_config(self, full_init: bool = False) -> None: ): # See https://github.com/ISISComputingGroup/IBEX/issues/5590 for # justification of why this ioc gets special treatment. - ioc = self._active_configserver.get_all_ioc_details()[ - CAEN_DISCRIMINATOR_IOC_NAME - ] + ioc = self._active_configserver.get_all_ioc_details()[CAEN_DISCRIMINATOR_IOC_NAME] if ioc.autostart: print_and_log( f"{CAEN_DISCRIMINATOR_IOC_NAME} present in configuration and set " f"to autostart - restarting it" ) - if ( - self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) - == "RUNNING" - ): + if self._ioc_control.get_ioc_status(CAEN_DISCRIMINATOR_IOC_NAME) == "RUNNING": self._ioc_control.restart_iocs( [CAEN_DISCRIMINATOR_IOC_NAME], reapply_auto=True ) @@ -590,12 +554,8 @@ def _initialise_config(self, full_init: bool = False) -> None: ), ) - self._config_list.active_config_name = ( - self._active_configserver.get_config_name() - ) - self._config_list.active_components = ( - self._active_configserver.get_component_names() - ) + self._config_list.active_config_name = self._active_configserver.get_config_name() + self._config_list.active_components = self._active_configserver.get_component_names() self._config_list.update_monitors() self.update_blocks_monitors() @@ -608,16 +568,10 @@ def _initialise_config(self, full_init: bool = False) -> None: handler.on_config_change(full_init=full_init) # Update Web Server text - self.server.set_config( - convert_to_json(self._active_configserver.get_config_details()) - ) - self.write_queue.put( - (self.set_config_block_values, (), "LOADING_BLOCK_SETS") - ) + self.server.set_config(convert_to_json(self._active_configserver.get_config_details())) + self.write_queue.put((self.set_config_block_values, (), "LOADING_BLOCK_SETS")) - def _start_config_iocs( - self, iocs_to_start: list[str], iocs_to_restart: list[str] - ) -> None: + def _start_config_iocs(self, iocs_to_start: list[str], iocs_to_restart: list[str]) -> None: # Start the IOCs, if they are available and if they are flagged for autostart # Note: autostart means the IOC is started when the config is loaded, # restart means the IOC should automatically restart if it stops for some reason @@ -633,12 +587,8 @@ def _should_start(ioc_name: str) -> bool: ioc = _ioc_from_name(ioc_name) return ioc.autostart and not _is_remote(ioc_name) - self._ioc_control.start_iocs( - [ioc for ioc in iocs_to_start if _should_start(ioc)] - ) - self._ioc_control.restart_iocs( - [ioc for ioc in iocs_to_restart if _should_start(ioc)] - ) + self._ioc_control.start_iocs([ioc for ioc in iocs_to_start if _should_start(ioc)]) + self._ioc_control.restart_iocs([ioc for ioc in iocs_to_restart if _should_start(ioc)]) for ioc in iocs_to_start: self._ioc_control.waitfor_running(ioc) @@ -673,9 +623,7 @@ def load_config(self, config: str, full_init: bool = True) -> None: # If we get this far then assume the config is okay self._initialise_config(full_init=full_init) except Exception as err: # noqa: BLE001 Call chain can cause many types of exceptions - print_and_log( - f"Exception while loading configuration '{config}': {err}", "MAJOR" - ) + print_and_log(f"Exception while loading configuration '{config}': {err}", "MAJOR") traceback.print_exc() def reload_current_config(self) -> None: @@ -756,11 +704,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if self._active_configserver is not None: # Reload configuration if a component has changed - if ( - as_comp - and new_details["name"] - in self._active_configserver.get_component_names() - ): + if as_comp and new_details["name"] in self._active_configserver.get_component_names(): self.load_last_config() # If the configuration we are trying to save is the currently active one, @@ -769,9 +713,7 @@ def save_config(self, json_data: str, as_comp: bool = False) -> None: if config_name == self._active_configserver.get_config_name(): self.load_config(config_name, full_init=False) - def _get_inactive_history( - self, name: str, is_component: bool = False - ) -> list[str | None]: + def _get_inactive_history(self, name: str, is_component: bool = False) -> list[str | None]: # If it already exists load it try: inactive = InactiveConfigHolder(MACROS, ConfigurationFileManager()) @@ -792,12 +734,8 @@ def update_blocks_monitors(self) -> None: """Updates the PV monitors for the blocks and groups, so the clients can see any changes.""" if self._active_configserver is not None: with self.monitor_lock: - block_names = convert_to_json( - self._active_configserver.get_blocknames() - ) - self.setParam( - BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names) - ) + block_names = convert_to_json(self._active_configserver.get_blocknames()) + self.setParam(BlockserverPVNames.BLOCKNAMES, compress_and_hex(block_names)) groups = ConfigurationJsonConverter.groups_to_json( self._active_configserver.get_group_details()