def test_halt(): o = options.Options() m = master.Master(o) a = addonmanager.AddonManager(m) halt = THalt() end = TAddon("end") a.add(halt) a.add(end) assert not end.running_called a.trigger(hooks.RunningHook()) assert not end.running_called a.remove(halt) a.trigger(hooks.RunningHook()) assert end.running_called
def test_nesting(): o = options.Options() m = master.Master(o) a = addonmanager.AddonManager(m) a.add( TAddon( "one", addons=[TAddon("two"), TAddon("three", addons=[TAddon("four")])])) assert len(a.chain) == 1 assert a.get("one") assert a.get("two") assert a.get("three") assert a.get("four") a.trigger(hooks.RunningHook()) assert a.get("one").running_called assert a.get("two").running_called assert a.get("three").running_called assert a.get("four").running_called a.remove(a.get("three")) assert not a.get("three") assert not a.get("four")
async def test_simple(): with taddons.context(loadcore=False) as tctx: a = tctx.master.addons assert len(a) == 0 a.add(TAddon("one")) assert a.get("one") assert not a.get("two") assert len(a) == 1 a.clear() assert len(a) == 0 assert not a.chain a.add(TAddon("one")) a.trigger("nonexistent") await tctx.master.await_log("AssertionError") f = tflow.tflow() a.trigger(hooks.RunningHook()) a.trigger(HttpResponseHook(f)) await tctx.master.await_log("not callable") tctx.master.clear() a.get("one").response = addons a.trigger(HttpResponseHook(f)) with pytest.raises(AssertionError): await tctx.master.await_log("not callable", timeout=0.01) a.remove(a.get("one")) assert not a.get("one") ta = TAddon("one") a.add(ta) a.trigger(hooks.RunningHook()) assert ta.running_called assert ta in a
def loadscript(self): ctx.log.info("Loading script %s" % self.path) if self.ns: ctx.master.addons.remove(self.ns) self.ns = None with addonmanager.safecall(): ns = load_script(self.fullpath) ctx.master.addons.register(ns) self.ns = ns if self.ns: # We're already running, so we have to explicitly register and # configure the addon ctx.master.addons.invoke_addon(self.ns, hooks.RunningHook()) try: ctx.master.addons.invoke_addon( self.ns, hooks.ConfigureHook(ctx.options.keys())) except exceptions.OptionsError as e: script_error_handler(self.fullpath, e, msg=str(e))
def script_run(self, flows: typing.Sequence[flow.Flow], path: mtypes.Path) -> None: """ Run a script on the specified flows. The script is configured with the current options and all lifecycle events for each flow are simulated. Note that the load event is not invoked. """ if not os.path.isfile(path): ctx.log.error('No such script: %s' % path) return mod = load_script(path) if mod: with addonmanager.safecall(): ctx.master.addons.invoke_addon(mod, hooks.RunningHook()) ctx.master.addons.invoke_addon( mod, hooks.ConfigureHook(ctx.options.keys()), ) for f in flows: for evt in eventsequence.iterate(f): ctx.master.addons.invoke_addon(mod, evt)
def configure(self, updated): if "scripts" in updated: for s in ctx.options.scripts: if ctx.options.scripts.count(s) > 1: raise exceptions.OptionsError("Duplicate script") for a in self.addons[:]: if a.path not in ctx.options.scripts: ctx.log.info("Un-loading script: %s" % a.path) ctx.master.addons.remove(a) self.addons.remove(a) # The machinations below are to ensure that: # - Scripts remain in the same order # - Scripts are not initialized un-necessarily. If only a # script's order in the script list has changed, it is just # moved. current = {} for a in self.addons: current[a.path] = a ordered = [] newscripts = [] for s in ctx.options.scripts: if s in current: ordered.append(current[s]) else: sc = Script(s, True) ordered.append(sc) newscripts.append(sc) self.addons = ordered for s in newscripts: ctx.master.addons.register(s) if self.is_running: # If we're already running, we configure and tell the addon # we're up and running. ctx.master.addons.invoke_addon(s, hooks.RunningHook())
async def running(self) -> None: await self.addons.trigger_event(hooks.RunningHook())
async def running(self): self.addons.trigger(hooks.RunningHook())