modulecontext: use inheritance instead of conditional init
This commit is contained in:
parent
b809451be2
commit
91230ac101
@ -17,9 +17,9 @@
|
|||||||
__version__ = '4.0.dev3'
|
__version__ = '4.0.dev3'
|
||||||
__author__ = 'nemunaire'
|
__author__ = 'nemunaire'
|
||||||
|
|
||||||
from nemubot.modulecontext import ModuleContext
|
from nemubot.modulecontext import _ModuleContext
|
||||||
|
|
||||||
context = ModuleContext(None, None)
|
context = _ModuleContext()
|
||||||
|
|
||||||
|
|
||||||
def requires_version(min=None, max=None):
|
def requires_version(min=None, max=None):
|
||||||
|
@ -478,7 +478,7 @@ class Bot(threading.Thread):
|
|||||||
module.print = prnt
|
module.print = prnt
|
||||||
|
|
||||||
# Create module context
|
# Create module context
|
||||||
from nemubot.modulecontext import ModuleContext
|
from nemubot.modulecontext import _ModuleContext, ModuleContext
|
||||||
module.__nemubot_context__ = ModuleContext(self, module)
|
module.__nemubot_context__ = ModuleContext(self, module)
|
||||||
|
|
||||||
if not hasattr(module, "logger"):
|
if not hasattr(module, "logger"):
|
||||||
@ -486,7 +486,7 @@ class Bot(threading.Thread):
|
|||||||
|
|
||||||
# Replace imported context by real one
|
# Replace imported context by real one
|
||||||
for attr in module.__dict__:
|
for attr in module.__dict__:
|
||||||
if attr != "__nemubot_context__" and type(module.__dict__[attr]) == ModuleContext:
|
if attr != "__nemubot_context__" and isinstance(module.__dict__[attr], _ModuleContext):
|
||||||
module.__dict__[attr] = module.__nemubot_context__
|
module.__dict__[attr] = module.__nemubot_context__
|
||||||
|
|
||||||
# Register decorated functions
|
# Register decorated functions
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# Nemubot is a smart and modulable IM bot.
|
# Nemubot is a smart and modulable IM bot.
|
||||||
# Copyright (C) 2012-2015 Mercier Pierre-Olivier
|
# Copyright (C) 2012-2017 Mercier Pierre-Olivier
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
# it under the terms of the GNU Affero General Public License as published by
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
@ -14,105 +14,61 @@
|
|||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
class ModuleContext:
|
class _ModuleContext:
|
||||||
|
|
||||||
def __init__(self, context, module):
|
def __init__(self, module=None):
|
||||||
"""Initialize the module context
|
self.module = module
|
||||||
|
|
||||||
arguments:
|
|
||||||
context -- the bot context
|
|
||||||
module -- the module
|
|
||||||
"""
|
|
||||||
|
|
||||||
if module is not None:
|
if module is not None:
|
||||||
module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__
|
self.module_name = module.__spec__.name if hasattr(module, "__spec__") else module.__name__
|
||||||
else:
|
else:
|
||||||
module_name = ""
|
self.module_name = ""
|
||||||
|
|
||||||
# Load module configuration if exists
|
|
||||||
if (context is not None and
|
|
||||||
module_name in context.modules_configuration):
|
|
||||||
self.config = context.modules_configuration[module_name]
|
|
||||||
else:
|
|
||||||
from nemubot.config.module import Module
|
|
||||||
self.config = Module(module_name)
|
|
||||||
|
|
||||||
self.hooks = list()
|
self.hooks = list()
|
||||||
self.events = list()
|
self.events = list()
|
||||||
self.debug = context.verbosity > 0 if context is not None else False
|
self.debug = False
|
||||||
|
|
||||||
|
from nemubot.config.module import Module
|
||||||
|
self.config = Module(self.module_name)
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(self):
|
||||||
|
from nemubot.tools.xmlparser import module_state
|
||||||
|
return module_state.ModuleState("nemubotstate")
|
||||||
|
|
||||||
|
def add_hook(self, hook, *triggers):
|
||||||
from nemubot.hooks import Abstract as AbstractHook
|
from nemubot.hooks import Abstract as AbstractHook
|
||||||
|
assert isinstance(hook, AbstractHook), hook
|
||||||
|
self.hooks.append((triggers, hook))
|
||||||
|
|
||||||
# Define some callbacks
|
def del_hook(self, hook, *triggers):
|
||||||
if context is not None:
|
from nemubot.hooks import Abstract as AbstractHook
|
||||||
def load_data():
|
assert isinstance(hook, AbstractHook), hook
|
||||||
return context.datastore.load(module_name)
|
self.hooks.remove((triggers, hook))
|
||||||
|
|
||||||
def add_hook(hook, *triggers):
|
def subtreat(self, msg):
|
||||||
assert isinstance(hook, AbstractHook), hook
|
return None
|
||||||
self.hooks.append((triggers, hook))
|
|
||||||
return context.treater.hm.add_hook(hook, *triggers)
|
|
||||||
|
|
||||||
def del_hook(hook, *triggers):
|
def add_event(self, evt, eid=None):
|
||||||
assert isinstance(hook, AbstractHook), hook
|
return self.events.append((evt, eid))
|
||||||
self.hooks.remove((triggers, hook))
|
|
||||||
return context.treater.hm.del_hooks(*triggers, hook=hook)
|
|
||||||
|
|
||||||
def subtreat(msg):
|
def del_event(self, evt):
|
||||||
yield from context.treater.treat_msg(msg)
|
for i in self.events:
|
||||||
def add_event(evt, eid=None):
|
e, eid = i
|
||||||
return context.add_event(evt, eid, module_src=module)
|
if e == evt:
|
||||||
def del_event(evt):
|
self.events.remove(i)
|
||||||
return context.del_event(evt, module_src=module)
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
def send_response(server, res):
|
def send_response(self, server, res):
|
||||||
if server in context.servers:
|
self.module.logger.info("Send response: %s", res)
|
||||||
if res.server is not None:
|
|
||||||
return context.servers[res.server].send_response(res)
|
|
||||||
else:
|
|
||||||
return context.servers[server].send_response(res)
|
|
||||||
else:
|
|
||||||
module.logger.error("Try to send a message to the unknown server: %s", server)
|
|
||||||
return False
|
|
||||||
|
|
||||||
else: # Used when using outside of nemubot
|
def save(self):
|
||||||
def load_data():
|
self.context.datastore.save(self.module_name, self.data)
|
||||||
from nemubot.tools.xmlparser import module_state
|
|
||||||
return module_state.ModuleState("nemubotstate")
|
|
||||||
|
|
||||||
def add_hook(hook, *triggers):
|
|
||||||
assert isinstance(hook, AbstractHook), hook
|
|
||||||
self.hooks.append((triggers, hook))
|
|
||||||
def del_hook(hook, *triggers):
|
|
||||||
assert isinstance(hook, AbstractHook), hook
|
|
||||||
self.hooks.remove((triggers, hook))
|
|
||||||
def subtreat(msg):
|
|
||||||
return None
|
|
||||||
def add_event(evt, eid=None):
|
|
||||||
return context.add_event(evt, eid, module_src=module)
|
|
||||||
def del_event(evt):
|
|
||||||
return context.del_event(evt, module_src=module)
|
|
||||||
|
|
||||||
def send_response(server, res):
|
|
||||||
module.logger.info("Send response: %s", res)
|
|
||||||
|
|
||||||
def save():
|
|
||||||
context.datastore.save(module_name, self.data)
|
|
||||||
|
|
||||||
def subparse(orig, cnt):
|
|
||||||
if orig.server in context.servers:
|
|
||||||
return context.servers[orig.server].subparse(orig, cnt)
|
|
||||||
|
|
||||||
self.load_data = load_data
|
|
||||||
self.add_hook = add_hook
|
|
||||||
self.del_hook = del_hook
|
|
||||||
self.add_event = add_event
|
|
||||||
self.del_event = del_event
|
|
||||||
self.save = save
|
|
||||||
self.send_response = send_response
|
|
||||||
self.subtreat = subtreat
|
|
||||||
self.subparse = subparse
|
|
||||||
|
|
||||||
|
def subparse(self, orig, cnt):
|
||||||
|
if orig.server in self.context.servers:
|
||||||
|
return self.context.servers[orig.server].subparse(orig, cnt)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def data(self):
|
def data(self):
|
||||||
@ -129,7 +85,62 @@ class ModuleContext:
|
|||||||
self.del_hook(h, *s)
|
self.del_hook(h, *s)
|
||||||
|
|
||||||
# Remove registered events
|
# Remove registered events
|
||||||
for e in self.events:
|
for evt, eid, module_src in self.events:
|
||||||
self.del_event(e)
|
self.del_event(evt)
|
||||||
|
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleContext(_ModuleContext):
|
||||||
|
|
||||||
|
def __init__(self, context, *args, **kwargs):
|
||||||
|
"""Initialize the module context
|
||||||
|
|
||||||
|
arguments:
|
||||||
|
context -- the bot context
|
||||||
|
module -- the module
|
||||||
|
"""
|
||||||
|
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# Load module configuration if exists
|
||||||
|
if self.module_name in context.modules_configuration:
|
||||||
|
self.config = context.modules_configuration[self.module_name]
|
||||||
|
|
||||||
|
self.context = context
|
||||||
|
self.debug = context.verbosity > 0
|
||||||
|
|
||||||
|
|
||||||
|
def load_data(self):
|
||||||
|
return self.context.datastore.load(self.module_name)
|
||||||
|
|
||||||
|
def add_hook(self, hook, *triggers):
|
||||||
|
from nemubot.hooks import Abstract as AbstractHook
|
||||||
|
assert isinstance(hook, AbstractHook), hook
|
||||||
|
self.hooks.append((triggers, hook))
|
||||||
|
return self.context.treater.hm.add_hook(hook, *triggers)
|
||||||
|
|
||||||
|
def del_hook(self, hook, *triggers):
|
||||||
|
from nemubot.hooks import Abstract as AbstractHook
|
||||||
|
assert isinstance(hook, AbstractHook), hook
|
||||||
|
self.hooks.remove((triggers, hook))
|
||||||
|
return self.context.treater.hm.del_hooks(*triggers, hook=hook)
|
||||||
|
|
||||||
|
def subtreat(self, msg):
|
||||||
|
yield from self.context.treater.treat_msg(msg)
|
||||||
|
|
||||||
|
def add_event(self, evt, eid=None):
|
||||||
|
return self.context.add_event(evt, eid, module_src=self.module)
|
||||||
|
|
||||||
|
def del_event(self, evt):
|
||||||
|
return self.context.del_event(evt, module_src=self.module)
|
||||||
|
|
||||||
|
def send_response(self, server, res):
|
||||||
|
if server in self.context.servers:
|
||||||
|
if res.server is not None:
|
||||||
|
return self.context.servers[res.server].send_response(res)
|
||||||
|
else:
|
||||||
|
return self.context.servers[server].send_response(res)
|
||||||
|
else:
|
||||||
|
self.module.logger.error("Try to send a message to the unknown server: %s", server)
|
||||||
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user