Hold context in EventCallback objects, add name<->context translation in
ModuleManager
This commit is contained in:
parent
4986c52d9b
commit
091818fa32
2 changed files with 15 additions and 2 deletions
|
@ -29,11 +29,12 @@ class Event(object):
|
||||||
|
|
||||||
class EventCallback(object):
|
class EventCallback(object):
|
||||||
def __init__(self, function: CALLBACK_TYPE, priority: int, kwargs: dict,
|
def __init__(self, function: CALLBACK_TYPE, priority: int, kwargs: dict,
|
||||||
one_shot: bool=False):
|
context: typing.Optional[str], one_shot: bool=False):
|
||||||
self.function = function
|
self.function = function
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
self.kwargs = kwargs
|
self.kwargs = kwargs
|
||||||
self.docstring = utils.parse.docstring(function.__doc__ or "")
|
self.docstring = utils.parse.docstring(function.__doc__ or "")
|
||||||
|
self.context = context
|
||||||
self._one_shot = one_shot
|
self._one_shot = one_shot
|
||||||
|
|
||||||
def call(self, event: Event) -> typing.Any:
|
def call(self, event: Event) -> typing.Any:
|
||||||
|
@ -83,7 +84,7 @@ class EventHook(object):
|
||||||
return self._hook(function, context, priority, replay, kwargs)
|
return self._hook(function, context, priority, replay, kwargs)
|
||||||
def _hook(self, function: CALLBACK_TYPE, context: typing.Optional[str],
|
def _hook(self, function: CALLBACK_TYPE, context: typing.Optional[str],
|
||||||
priority: int, replay: bool, kwargs: dict) -> EventCallback:
|
priority: int, replay: bool, kwargs: dict) -> EventCallback:
|
||||||
callback = EventCallback(function, priority, kwargs)
|
callback = EventCallback(function, priority, kwargs, context)
|
||||||
|
|
||||||
if context == None:
|
if context == None:
|
||||||
self._hooks.append(callback)
|
self._hooks.append(callback)
|
||||||
|
|
|
@ -91,6 +91,18 @@ class ModuleManager(object):
|
||||||
def _import_name(self, name: str) -> str:
|
def _import_name(self, name: str) -> str:
|
||||||
return "bitbot_%s" % name
|
return "bitbot_%s" % name
|
||||||
|
|
||||||
|
def from_context(self, context: str) -> typing.Optional[LoadedModule]:
|
||||||
|
for module in self.modules.values():
|
||||||
|
if module.context == context:
|
||||||
|
return module
|
||||||
|
return None
|
||||||
|
def from_name(self, name: str) -> typing.Optional[LoadedModule]:
|
||||||
|
name_lower = name.lower()
|
||||||
|
for module in self.modules.values():
|
||||||
|
if module.name.lower() == name_lower:
|
||||||
|
return module
|
||||||
|
return None
|
||||||
|
|
||||||
def _get_magic(self, obj: typing.Any, magic: str, default: typing.Any
|
def _get_magic(self, obj: typing.Any, magic: str, default: typing.Any
|
||||||
) -> typing.Any:
|
) -> typing.Any:
|
||||||
return getattr(obj, magic) if hasattr(obj, magic) else default
|
return getattr(obj, magic) if hasattr(obj, magic) else default
|
||||||
|
|
Loading…
Reference in a new issue