Skip to content

Commit

Permalink
correct bugs derived from refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlonsoDRDLV committed Aug 25, 2023
1 parent 3950838 commit e972b59
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 65 deletions.
51 changes: 24 additions & 27 deletions src/hvps/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
from hvps import __version__ as hvps_version
from hvps import Caen, Iseg
from hvps.commands.caen.module import (
_MON_MODULE_COMMANDS as CAEN_MON_MODULE_COMMANDS,
_SET_MODULE_COMMANDS as CAEN_SET_MODULE_COMMANDS,
)
from hvps.commands.caen.channel import (
_MON_CHANNEL_COMMANDS as CAEN_MON_CHANNEL_COMMANDS,
_SET_CHANNEL_COMMANDS as CAEN_SET_CHANNEL_COMMANDS,
)
from hvps.commands.iseg.module import (
_MON_MODULE_COMMANDS as ISEG_MON_MODULE_COMMANDS,
_SET_MODULE_COMMANDS as ISEG_SET_MODULE_COMMANDS,
)
from hvps.commands.iseg.channel import (
_MON_CHANNEL_COMMANDS as ISEG_MON_CHANNEL_COMMANDS,
_SET_CHANNEL_COMMANDS as ISEG_SET_CHANNEL_COMMANDS,
)

Expand Down Expand Up @@ -106,12 +110,11 @@ def _call_setter_method(
getattr(o, method)(value)
except (serial.SerialException, serial.serialutil.PortNotOpenError) as e:
if dry_run:
print(f"setter {method} called")
logger.info(f"setter {method} called")
else:
raise e

result = f"{method} {value}: ok"
print(result)
logger.info(result)


Expand All @@ -137,11 +140,10 @@ def _call_monitor_method(
"""
try:
result = f"{method}: {getattr(o, method)}"
print(result)
logger.info(result)
except (serial.SerialException, serial.serialutil.PortNotOpenError) as e:
if dry_run:
print(f"monitor {method} called")
logger.info(f"monitor {method} called")
else:
raise e

Expand Down Expand Up @@ -175,7 +177,10 @@ def main():
"--channel", default=None, type=int, help="HV PS channel"
) # Required argument
parser.add_argument(
"--dry-run", action="store_true", help="Dry run mode. Do not run commands"
"--dry-run",
dest="dry_run",
action="store_true",
help="Dry run mode. Do not run commands",
)

# Subparsers
Expand Down Expand Up @@ -205,7 +210,7 @@ def main():
# validate args
args = parser.parse_args()
logging.basicConfig(level=args.log.upper())
dry_run = True if args.test else False
dry_run = True if args.dry_run else False

if args.ports:
ports = [port.device for port in list_ports.comports()]
Expand Down Expand Up @@ -233,8 +238,8 @@ def main():
setter_mode = _is_setter_mode(
method,
value,
caen_mon_module_methods.keys(),
caen_set_module_methods.keys(),
CAEN_MON_MODULE_COMMANDS.keys(),
CAEN_SET_MODULE_COMMANDS.keys(),
)

if setter_mode:
Expand All @@ -247,17 +252,15 @@ def main():
caen._logger,
)
else:
_call_monitor_method(
method, module, caen_mon_module_methods, dry_run, caen._logger
)
_call_monitor_method(method, module, dry_run, caen._logger)
else:
# method is caen at channel level
channel = module.channel(args.channel)
setter_mode = _is_setter_mode(
method,
value,
caen_mon_channel_methods.keys(),
caen_set_channel_methods.keys(),
CAEN_MON_CHANNEL_COMMANDS.keys(),
CAEN_SET_CHANNEL_COMMANDS.keys(),
)

if setter_mode:
Expand All @@ -270,14 +273,12 @@ def main():
caen._logger,
)
else:
_call_monitor_method(
method, channel, caen_mon_channel_methods, dry_run, caen._logger
)
_call_monitor_method(method, channel, dry_run, caen._logger)

caen.close()

elif args.brand == "iseg":
iseg = Iseg(port=args.port, baudrate=args.baud, connect=not dry_run)
iseg = Iseg(port=args.port, baudrate=args.baud)
if not dry_run:
iseg.open()
module = iseg.module()
Expand All @@ -287,8 +288,8 @@ def main():
setter_mode = _is_setter_mode(
method,
value,
iseg_mon_module_methods.keys(),
iseg_set_module_methods.keys(),
ISEG_MON_MODULE_COMMANDS.keys(),
ISEG_SET_MODULE_COMMANDS.keys(),
)
if setter_mode:
_call_setter_method(
Expand All @@ -300,17 +301,15 @@ def main():
iseg._logger,
)
else:
_call_monitor_method(
method, module, iseg_mon_module_methods, dry_run, iseg._logger
)
_call_monitor_method(method, module, dry_run, iseg._logger)
else:
# method is iseg at channel level
channel = module.channel(args.channel)
setter_mode = _is_setter_mode(
method,
value,
iseg_mon_channel_methods.keys(),
iseg_set_channel_methods.keys(),
ISEG_MON_CHANNEL_COMMANDS.keys(),
ISEG_SET_CHANNEL_COMMANDS.keys(),
)
if setter_mode:
_call_setter_method(
Expand All @@ -322,9 +321,7 @@ def main():
iseg._logger,
)
else:
_call_monitor_method(
method, channel, iseg_mon_channel_methods, dry_run, iseg._logger
)
_call_monitor_method(method, channel, dry_run, iseg._logger)
iseg.close()
else:
raise ValueError(f"Brand {args.brand} not supported")
Expand Down
32 changes: 32 additions & 0 deletions src/hvps/commands/caen/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@
"possible_output_values": ["HIGH", "LOW"],
"description": "Read out IMON RANGE value (HIGH / LOW)",
},
"imrange_high": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out if IMON RANGE value is HIGH)",
},
"imrange_low": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out if IMON RANGE value is HIGH)",
},
"imdec": {
"command": "IMDEC",
"input_type": None,
Expand Down Expand Up @@ -243,6 +259,22 @@
"possible_output_values": ["+", "-"],
"description": "Read out POLARITY value (+ / -)",
},
"polarity_positive": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out POLARITY value is +",
},
"polarity_negative": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out POLARITY value is -",
},
"stat": {
"command": "STAT",
"input_type": None,
Expand Down
16 changes: 16 additions & 0 deletions src/hvps/commands/caen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@
"possible_output_values": [],
"description": "Set Interlock Mode",
},
"open_interlock": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": None,
"possible_output_values": [],
"description": "Set INTERLOCK mode to OPEN)",
},
"close_interlock": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": None,
"possible_output_values": [],
"description": "Set INTERLOCK mode to CLOSE)",
},
"clear_alarm_signal": {
"command": "BDCLR",
"input_type": None,
Expand Down
32 changes: 32 additions & 0 deletions src/hvps/commands/iseg/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@
"possible_output_values": [],
"description": "Check if serial echo is enabled or disabled.",
},
"serial_echo_enabled": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out if serial echo is enabled",
},
"serial_echo_disabled": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": bool,
"possible_output_values": [],
"description": "Check out if serial echo is disabled",
},
"module_current_limit": {
"command": ":READ:CURR:LIM",
"input_type": None,
Expand Down Expand Up @@ -310,6 +326,22 @@
"possible_output_values": [],
"description": "Enable or disable serial echo.",
},
"set_serial_echo_enabled": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": None,
"possible_output_values": [],
"description": "Enable serial echo.",
},
"set_serial_echo_disabled": {
"command": "",
"input_type": None,
"allowed_input_values": [],
"output_type": None,
"possible_output_values": [],
"description": "disable serial echo.",
},
"module_event_mask_register": {
"command": ":CONF:EVENT:MASK",
"input_type": int,
Expand Down
6 changes: 4 additions & 2 deletions src/hvps/devices/caen/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _write_command_read_response_channel_mon(
),
)
return check_command_output_and_convert(
command, None, response, _MON_CHANNEL_COMMANDS
method_name, None, response, _MON_CHANNEL_COMMANDS
)

def _write_command_read_response_channel_set(
Expand Down Expand Up @@ -277,7 +277,9 @@ def polarity_negative(self) -> bool:

@property
def stat(self) -> dict:
command = _MON_CHANNEL_COMMANDS[inspect.currentframe().f_code.co_name]
command = _MON_CHANNEL_COMMANDS[inspect.currentframe().f_code.co_name][
"command"
]
response = self._write_command_read_response(
bd=self.bd,
command=_get_mon_channel_command(
Expand Down
2 changes: 1 addition & 1 deletion src/hvps/devices/caen/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _write_command_read_response_module_mon(
command=_get_mon_module_command(bd=self.bd, command=command),
)
return check_command_output_and_convert(
command, None, response, _MON_MODULE_COMMANDS
method_name, None, response, _MON_MODULE_COMMANDS
)

def _write_command_read_response_module_set(
Expand Down
2 changes: 1 addition & 1 deletion src/hvps/devices/iseg/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _write_command_read_response_channel_mon(
expected_response_type=expected_response_type,
)
return check_command_output_and_convert(
command, None, response, _MON_CHANNEL_COMMANDS
method_name, None, response, _MON_CHANNEL_COMMANDS
)

def _write_command_read_response_channel_set(
Expand Down
2 changes: 1 addition & 1 deletion src/hvps/devices/iseg/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _write_command_read_response_module_mon(
expected_response_type=expected_response_type,
)
return check_command_output_and_convert(
command, None, response, _MON_MODULE_COMMANDS
method_name, None, response, _MON_MODULE_COMMANDS
)

def _write_command_read_response_module_set(
Expand Down
28 changes: 9 additions & 19 deletions src/hvps/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_command_input(
method (str): The method.
input_value (int | float | str | None): The input value.
command_dict (Dict): The command dictionary, Must be of the form:
"method_name": {"command": command associated,
"method_name": {"command": associated command,
"input_type": input_type,
"allowed_input_values": allowed_input_values,
"output_type": output_type,
Expand All @@ -70,28 +70,18 @@ def check_command_input(
ValueError: If the input value is not of the correct type, is not in the allowed values list or
if the command is not in the command dictionary.
"""
try:
command = command_dict[method]["command"].upper()
except KeyError:
command = None
valid_commands = [
entry_value["command"]
for entry_key, entry_value in command_dict.items()
if "command" in entry_value
]
print(valid_commands)
if command is None or command not in valid_commands:
valid_commands_string = ", ".join(valid_commands)
if method not in command_dict.keys():
valid_methods_string = ", ".join(command_dict.keys())
raise ValueError(
f"Invalid command '{command}'. Valid commands are: {valid_commands_string}"
f"Invalid method '{method}'. Valid methods are: {valid_methods_string}"
)

input_type = command_dict[method]["input_type"]
allowed_input_values = command_dict[method]["allowed_input_values"]

if input_type is None:
if input_value is not None:
raise ValueError(f"Command {command} does not take an input value.")
raise ValueError(f"Method {method} does not take an input value.")
else:
return
if input_type == str:
Expand All @@ -112,7 +102,7 @@ def check_command_input(


def check_command_output_and_convert(
command: str,
method: str,
input_value: int | float | str | None | List[int] | List[float] | List[str],
response: str | List[str] | None,
command_dict: Dict,
Expand All @@ -121,7 +111,7 @@ def check_command_output_and_convert(
Check output types and convert the response if necessary.
Args:
command (str): The command.
method (str): The method's name.
input_value (int | float | str | None | List[int] | List[float] | List[str]): The input value.
response (str | List[str] | None): The response.
command_dict (Dict): The command dictionary, Must be of the form:
Expand All @@ -138,8 +128,8 @@ def check_command_output_and_convert(
Returns:
"""
output_type = command_dict[command]["output_type"]
possible_output_values = command_dict[command]["possible_output_values"]
output_type = command_dict[method]["output_type"]
possible_output_values = command_dict[method]["possible_output_values"]

if output_type is None:
if response is not None:
Expand Down
Loading

0 comments on commit e972b59

Please sign in to comment.