Load module data on first access
This commit is contained in:
parent
6fc6561186
commit
f47aa8c478
@ -45,8 +45,8 @@ class ModuleContext:
|
||||
|
||||
# Define some callbacks
|
||||
if context is not None:
|
||||
# Load module data
|
||||
self.data = context.datastore.load(module_name)
|
||||
def load_data():
|
||||
return context.datastore.load(module_name)
|
||||
|
||||
def add_hook(hook, *triggers):
|
||||
assert isinstance(hook, AbstractHook), hook
|
||||
@ -76,8 +76,9 @@ class ModuleContext:
|
||||
return False
|
||||
|
||||
else: # Used when using outside of nemubot
|
||||
def load_data():
|
||||
from nemubot.tools.xmlparser import module_state
|
||||
self.data = module_state.ModuleState("nemubotstate")
|
||||
return module_state.ModuleState("nemubotstate")
|
||||
|
||||
def add_hook(hook, *triggers):
|
||||
assert isinstance(hook, AbstractHook), hook
|
||||
@ -98,6 +99,7 @@ class ModuleContext:
|
||||
def save():
|
||||
context.datastore.save(module_name, self.data)
|
||||
|
||||
self.load_data = load_data
|
||||
self.add_hook = add_hook
|
||||
self.del_hook = del_hook
|
||||
self.add_event = add_event
|
||||
@ -107,6 +109,13 @@ class ModuleContext:
|
||||
self.subtreat = subtreat
|
||||
|
||||
|
||||
@property
|
||||
def data(self):
|
||||
if not hasattr(self, "_data"):
|
||||
self._data = self.load_data()
|
||||
return self._data
|
||||
|
||||
|
||||
def unload(self):
|
||||
"""Perform actions for unloading the module"""
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user