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