Refactor updating

This commit is contained in:
CPD 2025-03-13 15:44:14 +01:00
parent 3c754669c3
commit 75159e9dbd

View File

@ -26,12 +26,13 @@ class LedScriptUpdateHandler(FileSystemEventHandler):
super().__init__()
self.led_script = led_script
self.verbose = verbose
def on_modified(self, event):
if self.verbose:
print("File modified: ", event.src_path)
filename = os.path.basename(self.led_script.filepath)
# print(event.event_type, event.src_path, os.path.basename(event.src_path), filename)
if os.path.basename(event.src_path) == filename:
if self.verbose:
print("File modified: ", event.src_path)
try:
self.led_script.update_from_file()
except ValueError as e:
@ -43,7 +44,7 @@ class LedScript:
Class representing a script to control the state of a LED
"""
ARRAY_DTYPE = [("dt", "f8"), ("dtsum", "f8"), ("led", "i4"), ("line", "i4")]
def __init__(self, script:np.ndarray|str|int=0, auto_update=False, on_update_callback:Callable[[], None]|None=None, verbose=False):
def __init__(self, script:np.ndarray|str|int=0, auto_update=False, verbose=False):
"""
Parameters
----------
@ -81,11 +82,24 @@ class LedScript:
raise ValueError("Invalid value for script: Muse be int, np.ndarray or str")
self.observer = None
if self.auto_update:
self.start_updating()
self.start_updating()
self.current_dt = 0
assert(self.script.shape[0] > 0)
self.on_update_callback = on_update_callback
self.is_updated = False
def has_updated(self) -> True:
"""
This is a workaround for when the update needs to be propagated to a QT thread for cpdctrl-gui
Returns
-------
True if the script was updated since the last call to this method
"""
if self.is_updated:
self.is_updated = False
return True
return False
def __del__(self):
self.stop_updating()
@ -214,9 +228,9 @@ class LedScript:
"""
if self.observer is not None:
raise ValueError("Already watching for updates")
raise RuntimeError("Already watching for updates")
if self.filepath is None:
raise ValueError("Can not watch for updates if the LedScript was not initialized with a file path")
raise RuntimeError("Can not watch for updates if the LedScript was not initialized with a file path")
# event_handler = LoggingEventHandler()
event_handler = LedScriptUpdateHandler(self) #, verbose=True)
self.observer = Observer()
@ -255,8 +269,8 @@ class LedScript:
if idx != newidx:
raise ValueError(f"The duration of the current step {idx+1} in line {newscript['line'][idx]} in the new script is too short TODO")
self.script = newscript
if self.on_update_callback is not None: self.on_update_callback()
self.is_updated = True
@staticmethod
def parse_script(filepath: str, ignore_errors:bool=False) -> np.ndarray|tuple[np.ndarray, list[InvalidScript]]:
"""