Skip to content

Commit 83f3128

Browse files
committed
more ruff
1 parent 467d10b commit 83f3128

File tree

3 files changed

+42
-35
lines changed

3 files changed

+42
-35
lines changed

installation_and_upgrade/calibration_files_updater.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import os
99
import subprocess
10+
from typing import Any, List
1011

1112
import git
1213
from epics import caget
@@ -25,7 +26,7 @@ class CalibrationsFolder:
2526
DRIVE_LETTER = "q"
2627

2728
@staticmethod
28-
def disconnect_from_drive():
29+
def disconnect_from_drive() -> bool:
2930
"""
3031
Returns: True if disconnect is successful, else False.
3132
"""
@@ -38,7 +39,7 @@ def disconnect_from_drive():
3839
== 0
3940
)
4041

41-
def connect_to_drive(self):
42+
def connect_to_drive(self) -> bool:
4243
"""
4344
Returns: True if the connection is successful, else False
4445
"""
@@ -58,23 +59,24 @@ def connect_to_drive(self):
5859
== 0
5960
)
6061

61-
def __init__(self, instrument_host, username, password):
62+
def __init__(self, instrument_host: str, username: str, password: str) -> None:
6263
self.username_with_domain = f"{instrument_host}\\{username}"
6364
self.network_location = r"\\{}\c$\Instrument\Settings\config\common".format(instrument_host)
6465
self.password = password
6566

66-
def __enter__(self):
67+
def __enter__(self) -> git.Repo | None:
6768
"""
68-
Returns: A git repository for the remote calibration folder if connection is successful, else None.
69+
Returns: A git repository for the
70+
remote calibration folder if connection is successful, else None.
6971
"""
7072
self.disconnect_from_drive()
7173
return git.Repo(self.network_location) if self.connect_to_drive() else None
7274

73-
def __exit__(self, *args):
75+
def __exit__(self, *args: Any) -> None: # noqa: ANN401
7476
self.disconnect_from_drive()
7577

7678

77-
def get_instrument_hosts():
79+
def get_instrument_hosts() -> List[str]:
7880
"""
7981
Returns: A collection of instrument host names
8082
"""
@@ -86,7 +88,9 @@ def get_instrument_hosts():
8688
)
8789

8890

89-
def update_instrument(host, username, password, logger, dry_run=False):
91+
def update_instrument(
92+
host: str, username: str, password: str, logger: logging.Logger, dry_run: bool = False
93+
) -> bool:
9094
"""
9195
Updates the calibration files on the named host.
9296

installation_and_upgrade/ibex_install_utils/ca_utils.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import zlib
33
from ast import literal_eval
44
from socket import gethostname
5-
from typing import Any
5+
from typing import Any, List
66

77
import epicscorelibs.path.pyepics # noqa: F401
8+
import numpy as np
89
from epics import caget, caput
910

1011
from ibex_install_utils.file_utils import FileUtils
@@ -59,7 +60,7 @@ def dehex_and_decompress(value: bytes | str) -> str:
5960
return zlib.decompress(bytes.fromhex(value)).decode("utf-8")
6061

6162

62-
def dehex_decompress_and_dejson(value: str | bytes) -> Any: # No known type
63+
def dehex_decompress_and_dejson(value: str | bytes) -> Any: # noqa: ANN401
6364
"""
6465
Convert string from zipped hexed json to a python representation
6566
:param value: value to convert
@@ -125,14 +126,15 @@ def get_machine_details_from_identifier(
125126
# then find the first match where pvPrefix equals the machine identifier
126127
# that's been passed to this function if it is not found instrument_details will be None
127128
instrument_details = None
128-
try:
129+
instlist_raw = caget("CS:INSTLIST")
130+
if instlist_raw is not None and isinstance(instlist_raw, np.ndarray):
129131
instrument_list = dehex_decompress_and_dejson(
130-
caget("CS:INSTLIST").tobytes().decode().rstrip("\x00")
132+
instlist_raw.tobytes().decode().rstrip("\x00")
131133
)
132134
instrument_details = next(
133135
(inst for inst in instrument_list if inst["pvPrefix"] == machine_identifier), None
134136
)
135-
except AttributeError:
137+
else:
136138
print("Error getting instlist, \nContinuing execution...")
137139

138140
if instrument_details is not None:
@@ -171,14 +173,13 @@ class CaWrapper:
171173
Wrapper around genie python's channel access class providing some useful abstractions.
172174
"""
173175

174-
def __init__(self):
176+
def __init__(self) -> None:
175177
"""
176-
Setting instrument is necessary because genie_python is being run from a network drive so it may not know
177-
where it is.
178+
Get the current PV prefix.
178179
"""
179180
_, _, self.prefix = get_machine_details_from_identifier()
180181

181-
def get_local_pv(self, name):
182+
def get_local_pv(self, name: str) -> str | int | float | None:
182183
"""
183184
Get PV with the local PV prefix appended
184185
@@ -188,9 +189,9 @@ def get_local_pv(self, name):
188189
Returns:
189190
None if the PV was not connected
190191
"""
191-
return caget(f"{self.prefix}{name}")
192+
return caget(f"{self.prefix}{name}") # type: ignore
192193

193-
def get_object_from_compressed_hexed_json(self, name):
194+
def get_object_from_compressed_hexed_json(self, name: str) -> bytes | None:
194195
"""
195196
Gets an object from a compressed hexed json PV
196197
@@ -206,26 +207,24 @@ def get_object_from_compressed_hexed_json(self, name):
206207
else:
207208
return FileUtils.dehex_and_decompress(data)
208209

209-
def get_blocks(self):
210+
def get_blocks(self) -> List[str]:
210211
"""
211212
Returns:
212213
A collection of blocks, or None if the PV was not connected
213214
"""
214-
try:
215-
blocks_hexed = caget(f"{self.prefix}CS:BLOCKSERVER:BLOCKNAMES").tobytes()
216-
except AttributeError as e:
217-
print("Error getting blocks.")
218-
raise e
219-
return literal_eval(FileUtils.dehex_and_decompress(blocks_hexed).decode())
220-
221-
def cget(self, block: str) -> Any:
215+
blocks_hexed = caget(f"{self.prefix}CS:BLOCKSERVER:BLOCKNAMES")
216+
if blocks_hexed is None:
217+
raise Exception("Error getting blocks from blockserver PV.")
218+
return literal_eval(FileUtils.dehex_and_decompress(blocks_hexed.tobytes()).decode())
219+
220+
def cget(self, block: str) -> str | int | float | None:
222221
"""
223222
Returns:
224223
A collection of blocks, or None if the PV was not connected.
225224
"""
226-
return caget(f"{self.prefix}CS:SB:{block}")
225+
return caget(f"{self.prefix}CS:SB:{block}") # type: ignore
227226

228-
def set_pv(self, pv, value, is_local: bool = False):
227+
def set_pv(self, pv: str, value: str | float | int, is_local: bool = False) -> None:
229228
"""
230229
Sets the value of a PV.
231230
"""

installation_and_upgrade/ibex_install_utils/motor_params.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
"""
2-
Script to extract information from motors to be consumed by the motion controls team. Exports data as CSV. To run,
3-
load the script into a genie_python console and run as a standard user script.
2+
Script to extract information from motors to be consumed by the motion controls team.
3+
Exports data as CSV.
4+
To run, load the script into a genie_python console and run as a standard user script.
45
"""
56

67
import csv
8+
from typing import BinaryIO
79

810
from aioca import CANothing, caget
911

@@ -24,7 +26,7 @@
2426
"Encoder Step Size (step per EGU)",
2527
)
2628
DECEL_DIST = "Max Deceleration Distance (EGU)"
27-
P, I, D = "P", "I", "D"
29+
P, I, D = "P", "I", "D" # noqa: E741
2830

2931
K1, K2, K3 = "K1", "K2", "K3"
3032
MTR_TYPE, ENC_TYPE, AUX_ENC_TYPE = "Motor Type", "Encoder Type", "Aux Encoder Type"
@@ -110,13 +112,15 @@
110112
}
111113

112114

113-
async def get_params_and_save_to_file(file_reference, num_of_controllers=8):
115+
async def get_params_and_save_to_file(
116+
file_reference: BinaryIO, num_of_controllers: int = 8
117+
) -> None:
114118
"""
115119
Gets all the motor parameters and saves them to an open file reference as a csv.
116120
117121
Args:
118122
file_reference (BinaryIO): The csv file to save the data to.
119-
num_of_controllers (int, optional): The number of motor controllers on the instrument (default is 8)
123+
num_of_controllers (int, optional): The number of motor controllers on the instrument
120124
"""
121125
list_of_axis_pvs = []
122126
_, _, pv_prefix = get_machine_details_from_identifier()

0 commit comments

Comments
 (0)