From d6c0c64cbe80b0104a112a45d4559bb4ffda76d4 Mon Sep 17 00:00:00 2001 From: jesopo Date: Sat, 1 Sep 2018 18:27:10 +0100 Subject: [PATCH 1/2] Don't purge children on purge_context() in EventHook, make event_context str(uuid) instead of just uuid in ModuleManager --- EventManager.py | 26 ++++++++++---------------- ModuleManager.py | 3 +-- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/EventManager.py b/EventManager.py index 326192ad..67cd1f76 100644 --- a/EventManager.py +++ b/EventManager.py @@ -86,14 +86,13 @@ class EventHook(object): self._hooks = [] self._stored_events = [] self._context_hooks = {} - self._current_context = None def _make_event(self, kwargs): return Event(self.bot, self.name, **kwargs) def _get_path(self): - path = [self.name] - parent = self.parent + path = [] + parent = self while not parent == None and not parent.name == None: path.append(parent.name) parent = parent.parent @@ -134,7 +133,7 @@ class EventHook(object): for event_name in event_chain: event_obj = event_obj.get_child(event_name) if not context == None: - event_obj = event_obj.new_context(context) + return event_obj.new_context(context) return event_obj if extra_subevents: @@ -170,10 +169,9 @@ class EventHook(object): start = time.monotonic() event = self._make_event(kwargs) - called = 0 returns = [] - for hook in self.get_hooks(): - if (maximum and called == maximum) or event.eaten: + for hook in self.get_hooks()[:maximum]: + if event.eaten: break try: returns.append(hook.call(event)) @@ -181,10 +179,8 @@ class EventHook(object): traceback.print_exc() self.bot.log.error("failed to call event \"%s\"", [ event_path], exc_info=True) - called += 1 - end = time.monotonic() - total_milliseconds = (end - start) * 1000 + total_milliseconds = (time.monotonic() - start) * 1000 self.bot.log.debug("event \"%s\" called in %fms", [ event_path, total_milliseconds]) @@ -204,8 +200,7 @@ class EventHook(object): del self._children[child_name_lower] def check_purge(self): - if len(self.get_hooks()) == 0 and len(self.get_children() - ) == 0 and not self.parent == None: + if self.is_empty() and not self.parent == None: self.parent.remove_child(self.name) self.parent.check_purge() @@ -216,15 +211,14 @@ class EventHook(object): def purge_context(self, context): if self.has_context(context): self.remove_context(context) + for child_name in self.get_children()[:]: child = self.get_child(child_name) child.purge_context(context) - if child.is_empty(): - self.remove_child(child_name) def get_hooks(self): - return sorted(self._hooks + list(itertools.chain.from_iterable( - self._context_hooks.values())), key=lambda e: e.priority) + return sorted(self._hooks + sum(self._context_hooks.values(), []), + key=lambda e: e.priority) def get_children(self): return list(self._children.keys()) def is_empty(self): diff --git a/ModuleManager.py b/ModuleManager.py index 404502e8..d2457ae5 100644 --- a/ModuleManager.py +++ b/ModuleManager.py @@ -49,13 +49,12 @@ class ModuleManager(object): if not inspect.isclass(module.Module): raise ImportError("module '%s' has a Module attribute but it is not a class.") - event_context = uuid.uuid4() + event_context = str(uuid.uuid4()) module_object = module.Module(self.bot, self.events.new_context( event_context)) if not hasattr(module_object, "_name"): module_object._name = name.title() module_object._event_context = event_context - module_object._is_unloaded = False module_object._import_name = name assert not module_object._name in self.modules, ( From a0a29eacd07b75b81ff0643908e7c5736e1fe96c Mon Sep 17 00:00:00 2001 From: dngfx <294904+dngfx@users.noreply.github.com> Date: Sat, 1 Sep 2018 18:45:01 +0100 Subject: [PATCH 2/2] Convert to booleans and validation. --- modules/ducks.py | 52 ++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/modules/ducks.py b/modules/ducks.py index 8ca562c7..7adb3b9b 100644 --- a/modules/ducks.py +++ b/modules/ducks.py @@ -38,15 +38,20 @@ class Module(object): events.on("postboot").on("configure").on( "channelset").assure_call(setting="ducks-enabled", - help="Toggles ducks! (1 or 0)") + help="Toggles ducks!", + validate=Utils.bool_or_none) events.on("postboot").on("configure").on( "channelset").assure_call(setting="min-duck-time", - help="Minimum seconds before a duck is summoned") + help="Minimum seconds before a duck is " + "summoned", + validate=Utils.int_or_none) events.on("postboot").on("configure").on( "channelset").assure_call(setting="max-duck-time", - help="Max seconds before a duck is summoned") + help="Max seconds before a duck is " + "summoned", + validate=Utils.int_or_none) events.on("received.numeric.366").hook(self.bootstrap) @@ -64,9 +69,7 @@ class Module(object): def bootstrap(self, event): for server in self.bot.servers.values(): for channel in server.channels.values(): - ducks_enabled = channel.get_setting("ducks-enabled", 0) - ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, - str) else ducks_enabled + ducks_enabled = channel.get_setting("ducks-enabled", False) min_time = "min-duck-time-%s" % channel.name max_time = "max-duck-time-%s" % channel.name @@ -82,8 +85,8 @@ class Module(object): self.duck_times[min_time] = min_duck_time self.duck_times[max_time] = max_duck_time - if ducks_enabled == 1: - channel.set_setting("active-duck", 0) + if ducks_enabled == True: + channel.set_setting("active-duck", False) def duck_time(self, channel): if isinstance(channel, str): @@ -173,11 +176,9 @@ class Module(object): target = user.nickname id = user.id - active_duck = event["target"].get_setting("active-duck", 0) - active_duck = int(active_duck) if isinstance(active_duck, - str) else active_duck + active_duck = event["target"].get_setting("active-duck", False) - if active_duck == 0: + if active_duck == False: if event["server"].has_user(target): if not event["server"].is_own_nickname(target): event["target"].send_kick(target, @@ -193,7 +194,7 @@ class Module(object): event["target"].set_user_setting(id, "ducks-befriended", befriended_ducks + 1) - event["target"].set_setting("active-duck", 0) + event["target"].set_setting("active-duck", False) grammar = "" if befriended_ducks == 0 else "s" @@ -209,7 +210,7 @@ class Module(object): user = event["user"] target = user.nickname id = user.id - if event["target"].get_setting("active-duck", 0) == 0: + if not event["target"].get_setting("active-duck", False): event["stderr"].set_prefix("Kick") if event["server"].has_user(target): if not event["server"].is_own_nickname(target): @@ -222,7 +223,7 @@ class Module(object): else: shot_ducks = event["target"].get_user_setting(id, "ducks-shot", 0) event["target"].set_user_setting(id, "ducks-shot", shot_ducks + 1) - event["target"].set_setting("active-duck", 0) + event["target"].set_setting("active-duck", False) grammar = "" if shot_ducks == 0 else "s" @@ -240,28 +241,23 @@ class Module(object): def show_duck(self, event): for server in self.bot.servers.values(): for channel in server.channels.values(): - ducks_enabled = channel.get_setting("ducks-enabled", 0) - ducks_enabled = int(ducks_enabled) if isinstance(ducks_enabled, - str) else ducks_enabled - if ducks_enabled == 0: + ducks_enabled = channel.get_setting("ducks-enabled", False) + + if ducks_enabled == False: continue - self.bot.log.info("Ducks enabled for %s: %s", - [str(channel.name), str(ducks_enabled)]) - active_duck = channel.get_setting("active-duck", 0) - active_duck = int(active_duck) if isinstance(active_duck, - str) else active_duck + active_duck = channel.get_setting("active-duck", False) - if ducks_enabled == 1 and active_duck == 0: + if ducks_enabled == True and active_duck == False: channel.send_message(random.choice(DUCK_LIST)) - channel.set_setting("active-duck", 1) + channel.set_setting("active-duck", True) - elif ducks_enabled == 1 and active_duck == 1: + elif ducks_enabled == True and active_duck == True: pass else: - channel.set_setting("active-duck", 0) + channel.set_setting("active-duck", False) def duck_decoy(self, event): event["stdout"].write(random.choice(DUCK_LIST))