Don't purge children on purge_context() in EventHook, make event_context
str(uuid) instead of just uuid in ModuleManager
This commit is contained in:
parent
ef645c338a
commit
2206502cca
2 changed files with 11 additions and 18 deletions
|
@ -86,14 +86,13 @@ class EventHook(object):
|
||||||
self._hooks = []
|
self._hooks = []
|
||||||
self._stored_events = []
|
self._stored_events = []
|
||||||
self._context_hooks = {}
|
self._context_hooks = {}
|
||||||
self._current_context = None
|
|
||||||
|
|
||||||
def _make_event(self, kwargs):
|
def _make_event(self, kwargs):
|
||||||
return Event(self.bot, self.name, **kwargs)
|
return Event(self.bot, self.name, **kwargs)
|
||||||
|
|
||||||
def _get_path(self):
|
def _get_path(self):
|
||||||
path = [self.name]
|
path = []
|
||||||
parent = self.parent
|
parent = self
|
||||||
while not parent == None and not parent.name == None:
|
while not parent == None and not parent.name == None:
|
||||||
path.append(parent.name)
|
path.append(parent.name)
|
||||||
parent = parent.parent
|
parent = parent.parent
|
||||||
|
@ -134,7 +133,7 @@ class EventHook(object):
|
||||||
for event_name in event_chain:
|
for event_name in event_chain:
|
||||||
event_obj = event_obj.get_child(event_name)
|
event_obj = event_obj.get_child(event_name)
|
||||||
if not context == None:
|
if not context == None:
|
||||||
event_obj = event_obj.new_context(context)
|
return event_obj.new_context(context)
|
||||||
return event_obj
|
return event_obj
|
||||||
|
|
||||||
if extra_subevents:
|
if extra_subevents:
|
||||||
|
@ -170,10 +169,9 @@ class EventHook(object):
|
||||||
start = time.monotonic()
|
start = time.monotonic()
|
||||||
|
|
||||||
event = self._make_event(kwargs)
|
event = self._make_event(kwargs)
|
||||||
called = 0
|
|
||||||
returns = []
|
returns = []
|
||||||
for hook in self.get_hooks():
|
for hook in self.get_hooks()[:maximum]:
|
||||||
if (maximum and called == maximum) or event.eaten:
|
if event.eaten:
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
returns.append(hook.call(event))
|
returns.append(hook.call(event))
|
||||||
|
@ -181,10 +179,8 @@ class EventHook(object):
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
self.bot.log.error("failed to call event \"%s\"", [
|
self.bot.log.error("failed to call event \"%s\"", [
|
||||||
event_path], exc_info=True)
|
event_path], exc_info=True)
|
||||||
called += 1
|
|
||||||
|
|
||||||
end = time.monotonic()
|
total_milliseconds = (time.monotonic() - start) * 1000
|
||||||
total_milliseconds = (end - start) * 1000
|
|
||||||
self.bot.log.debug("event \"%s\" called in %fms", [
|
self.bot.log.debug("event \"%s\" called in %fms", [
|
||||||
event_path, total_milliseconds])
|
event_path, total_milliseconds])
|
||||||
|
|
||||||
|
@ -204,8 +200,7 @@ class EventHook(object):
|
||||||
del self._children[child_name_lower]
|
del self._children[child_name_lower]
|
||||||
|
|
||||||
def check_purge(self):
|
def check_purge(self):
|
||||||
if len(self.get_hooks()) == 0 and len(self.get_children()
|
if self.is_empty() and not self.parent == None:
|
||||||
) == 0 and not self.parent == None:
|
|
||||||
self.parent.remove_child(self.name)
|
self.parent.remove_child(self.name)
|
||||||
self.parent.check_purge()
|
self.parent.check_purge()
|
||||||
|
|
||||||
|
@ -216,15 +211,14 @@ class EventHook(object):
|
||||||
def purge_context(self, context):
|
def purge_context(self, context):
|
||||||
if self.has_context(context):
|
if self.has_context(context):
|
||||||
self.remove_context(context)
|
self.remove_context(context)
|
||||||
|
|
||||||
for child_name in self.get_children()[:]:
|
for child_name in self.get_children()[:]:
|
||||||
child = self.get_child(child_name)
|
child = self.get_child(child_name)
|
||||||
child.purge_context(context)
|
child.purge_context(context)
|
||||||
if child.is_empty():
|
|
||||||
self.remove_child(child_name)
|
|
||||||
|
|
||||||
def get_hooks(self):
|
def get_hooks(self):
|
||||||
return sorted(self._hooks + list(itertools.chain.from_iterable(
|
return sorted(self._hooks + sum(self._context_hooks.values(), []),
|
||||||
self._context_hooks.values())), key=lambda e: e.priority)
|
key=lambda e: e.priority)
|
||||||
def get_children(self):
|
def get_children(self):
|
||||||
return list(self._children.keys())
|
return list(self._children.keys())
|
||||||
def is_empty(self):
|
def is_empty(self):
|
||||||
|
|
|
@ -49,13 +49,12 @@ class ModuleManager(object):
|
||||||
if not inspect.isclass(module.Module):
|
if not inspect.isclass(module.Module):
|
||||||
raise ImportError("module '%s' has a Module attribute but it is not a class.")
|
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(
|
module_object = module.Module(self.bot, self.events.new_context(
|
||||||
event_context))
|
event_context))
|
||||||
if not hasattr(module_object, "_name"):
|
if not hasattr(module_object, "_name"):
|
||||||
module_object._name = name.title()
|
module_object._name = name.title()
|
||||||
module_object._event_context = event_context
|
module_object._event_context = event_context
|
||||||
module_object._is_unloaded = False
|
|
||||||
module_object._import_name = name
|
module_object._import_name = name
|
||||||
|
|
||||||
assert not module_object._name in self.modules, (
|
assert not module_object._name in self.modules, (
|
||||||
|
|
Loading…
Reference in a new issue