An addon that manages a single script.
| 75 | |
| 76 | |
| 77 | class Script: |
| 78 | """ |
| 79 | An addon that manages a single script. |
| 80 | """ |
| 81 | |
| 82 | def __init__(self, path: str, reload: bool) -> None: |
| 83 | self.name = "scriptmanager:" + path |
| 84 | self.path = path |
| 85 | self.fullpath = os.path.expanduser(path.strip("'\" ")) |
| 86 | self.ns: types.ModuleType | None = None |
| 87 | self.is_running = False |
| 88 | |
| 89 | if not os.path.isfile(self.fullpath): |
| 90 | raise exceptions.OptionsError(f"No such script: {self.fullpath}") |
| 91 | |
| 92 | self.reloadtask = None |
| 93 | if reload: |
| 94 | self.reloadtask = asyncio_utils.create_task( |
| 95 | self.watcher(), |
| 96 | name=f"script watcher for {path}", |
| 97 | keep_ref=False, |
| 98 | ) |
| 99 | else: |
| 100 | self.loadscript() |
| 101 | |
| 102 | def running(self): |
| 103 | self.is_running = True |
| 104 | |
| 105 | def done(self): |
| 106 | if self.reloadtask: |
| 107 | self.reloadtask.cancel() |
| 108 | |
| 109 | @property |
| 110 | def addons(self): |
| 111 | return [self.ns] if self.ns else [] |
| 112 | |
| 113 | def loadscript(self): |
| 114 | logger.info("Loading script %s" % self.path) |
| 115 | if self.ns: |
| 116 | ctx.master.addons.remove(self.ns) |
| 117 | self.ns = None |
| 118 | with addonmanager.safecall(): |
| 119 | ns = load_script(self.fullpath) |
| 120 | ctx.master.addons.register(ns) |
| 121 | self.ns = ns |
| 122 | if self.ns: |
| 123 | try: |
| 124 | ctx.master.addons.invoke_addon_sync( |
| 125 | self.ns, hooks.ConfigureHook(ctx.options.keys()) |
| 126 | ) |
| 127 | except Exception as e: |
| 128 | script_error_handler(self.fullpath, e) |
| 129 | if self.is_running: |
| 130 | # We're already running, so we call that on the addon now. |
| 131 | ctx.master.addons.invoke_addon_sync(self.ns, hooks.RunningHook()) |
| 132 | |
| 133 | async def watcher(self): |
| 134 | # Script loading is terminally confused at the moment. |
no outgoing calls
no test coverage detected
searching dependent graphs…