Commit 4c95c328 authored by Jerome Flesch's avatar Jerome Flesch Committed by Jerome Flesch

Core: pass the reference of the core to all plugins

Signed-off-by: 's avatarJerome Flesch <jflech@openpaper.work>
parent 74ebf4d9
......@@ -45,7 +45,7 @@ class Plugin(openpaperwork_core.PluginBase):
],
}
def init(self):
def init(self, core):
# all the dependnecies have loaded and initialized.
# we can safely rely on them here.
pass
......
......@@ -16,7 +16,7 @@ class PluginBase(object):
# to enable/disable this plugin in the UI.
USER_VISIBLE = False
def __init__(self):
def __init__(self, core):
"""
Called as soon as the module is loaded. Should be as minimal as
possible. Most of the work should be done in `init()`.
......@@ -43,7 +43,7 @@ class PluginBase(object):
'interfaces': [],
}
def init(self):
def init(self, core):
pass
......@@ -170,7 +170,7 @@ class Core(object):
self._init(dep_plugin)
LOGGER.info("Initializing plugin '%s' ...", type(plugin))
plugin.init()
plugin.init(self)
self._initialized.add(plugin)
......
......@@ -17,8 +17,8 @@ LOGGER = logging.getLogger(__name__)
class ConfigList(object):
SEPARATOR = ", "
def __init__(self, string=None):
self.elements = []
def __init__(self, string=None, elements=[]):
self.elements = elements
if string is not None:
elements = string.split(self.SEPARATOR, 1)
......@@ -67,6 +67,10 @@ class Plugin(PluginBase):
)
self.application_name = None
self.observers = collections.defaultdict(set)
self.core = None
def init(self, core):
self.core = core
def get_interfaces(self):
return ['configuration']
......@@ -96,18 +100,18 @@ class Plugin(PluginBase):
with open(config_path, 'w') as fd:
self.config.write(fd)
def config_load_plugins(self, core):
def config_load_plugins(self, default=[]):
"""
Load and init the plugin list from the configuration.
"""
modules = self.config_get("plugins", "modules", ConfigList())
modules = self.config_get("plugins", "modules", ConfigList(default))
LOGGER.info(
"Loading plugins from configuration: %s",
"Loading and initializing plugins from configuration: %s",
str(modules)
)
for module in modules:
core.load(module)
core.init()
self.core.load(module)
self.core.init()
def config_add_plugin(self, module_name):
LOGGER.info("Adding plugin '%s' to configuration", module_name)
......
......@@ -63,7 +63,7 @@ class TestReadWrite(unittest.TestCase):
def __init__(self):
self.initialized = False
def init(self):
def init(self, core):
self.initialized = True
core.call_all('config_add_plugin', 'some_test_module')
......@@ -71,7 +71,7 @@ class TestReadWrite(unittest.TestCase):
import_module.reset_mock()
import_module.side_effect = [TestModule(), TestModule()]
core.call_all('config_load_plugins', core)
core.call_all('config_load_plugins')
import_module.assert_called_with('some_test_module_2')
self.assertEqual(core.get('some_test_module').initialized, True)
......
......@@ -13,7 +13,7 @@ class TestLoading(unittest.TestCase):
self.init_called = False
self.test_method_called = False
def init(self):
def init(self, core):
self.init_called = True
def test_method(self):
......@@ -42,7 +42,7 @@ class TestLoading(unittest.TestCase):
def get_interfaces(self):
return ["test_interface"]
def init(self):
def init(self, core):
self.init_called = True
def test_method(self):
......@@ -61,7 +61,7 @@ class TestLoading(unittest.TestCase):
],
}
def init(self):
def init(self, core):
self.init_called = True
core = openpaperwork_core.Core()
......@@ -92,7 +92,7 @@ class TestInit(unittest.TestCase):
def __init__(self):
self.init_called_a = -1
def init(self):
def init(self, core):
global g_idx
self.init_called_a = g_idx
g_idx += 1
......@@ -107,7 +107,7 @@ class TestInit(unittest.TestCase):
'plugins': ['module_a'],
}
def init(self):
def init(self, core):
global g_idx
self.init_called_b = g_idx
g_idx += 1
......@@ -122,7 +122,7 @@ class TestInit(unittest.TestCase):
'plugins': ['module_b'],
}
def init(self):
def init(self, core):
global g_idx
self.init_called_c = g_idx
g_idx += 1
......@@ -160,7 +160,7 @@ class TestCall(unittest.TestCase):
def get_interfaces(self):
return ["test_interface"]
def init(self):
def init(self, core):
self.init_called_b = True
def test_method(self):
......@@ -183,7 +183,7 @@ class TestCall(unittest.TestCase):
],
}
def init(self):
def init(self, core):
self.init_called_c = True
def test_method(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment