22import zlib
33from ast import literal_eval
44from socket import gethostname
5- from typing import Any
5+ from typing import Any , List
66
77import epicscorelibs .path .pyepics # noqa: F401
8+ import numpy as np
89from epics import caget , caput
910
1011from 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, \n Continuing 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 """
0 commit comments