added pyepics; added option to load /remove single script files

This commit is contained in:
wakonig_k 2022-10-26 22:10:21 +02:00
parent 69f58d7db1
commit 3a05e6582e
2 changed files with 36 additions and 20 deletions

View File

@ -56,7 +56,7 @@ class BECClient(BECService):
self._start_scan_queue()
self._start_alarm_handler()
self._configure_logger()
self.load_user_scripts()
self.load_all_user_scripts()
def alarms(self, severity=Alarms.WARNING):
"""get the next alarm with at least the specified severity"""
@ -85,11 +85,25 @@ class BECClient(BECService):
for hook in hooks:
self.producer.lpush(MessageEndpoints.pre_scan_macros(), hook)
def load_user_scripts(self):
def load_all_user_scripts(self) -> None:
"""Load all scripts from the `scripts` directory."""
current_path = pathlib.Path(__file__).parent.resolve()
script_files = glob.glob(os.path.join(current_path, "../scripts/*.py"))
for file in script_files:
self.load_user_script(file)
builtins.__dict__.update(self._scripts)
def forget_all_user_scripts(self) -> None:
"""unload / remove loaded user scripts from builtins. The files will remain on disk though!"""
for name in self._scripts:
self.forget_user_script(name)
def load_user_script(self, file: str) -> None:
"""load a user script file and import all its definitions
Args:
file (str): Full path to the script file.
"""
module_spec = importlib.util.spec_from_file_location("scripts", file)
plugin_module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(plugin_module)
@ -104,13 +118,14 @@ class BECClient(BECService):
logger.warning(f"Conflicting definitions for {name}.")
logger.info(f"Importing {name}")
self._scripts[name] = cls
builtins.__dict__.update(self._scripts)
def forget_user_scripts(self):
"""remove loaded user scripts from builtins. The files will remain on disk though!"""
for name in self._scripts:
def forget_user_script(self, name: str) -> None:
"""unload / remove a user scripts. The file will remain on disk."""
if not name in self._scripts:
logger.error(f"{name} is not a known user script.")
return
builtins.__dict__.pop(name)
self._scripts = {}
self._scripts.pop(name)
def _load_scans(self):
self.scans = Scans(self)

View File

@ -19,6 +19,7 @@ if __name__ == "__main__":
"ipython",
"cytoolz",
"rich",
"pyepics",
]
)
local_deps = [utils]