Commit 74ebf4d9 authored by Jerome Flesch's avatar Jerome Flesch Committed by Jerome Flesch

Core: Config: Add observers: Make it possible to watch configuration sections

Signed-off-by: 's avatarJerome Flesch <jflech@openpaper.work>
parent daae49f0
......@@ -2,6 +2,7 @@
Manages a configuration file using configparser.
"""
import collections
import configparser
import logging
import os
......@@ -64,11 +65,14 @@ class Plugin(PluginBase):
self.config_file_path_fmt = os.path.join(
"{directory}", "{app_name}.conf"
)
self.application_name = None
self.observers = collections.defaultdict(set)
def get_interfaces(self):
return ['configuration']
def config_load(self, application_name):
self.application_name = application_name
config_path = self.config_file_path_fmt.format(
directory=self.base_path,
app_name=application_name,
......@@ -77,11 +81,16 @@ class Plugin(PluginBase):
LOGGER.info("Loading configuration '%s' ...", config_path)
with open(config_path, 'r') as fd:
self.config.read_file(fd)
for observers in self.observers.values():
for observer in observers:
observer()
def config_save(self, application_name):
def config_save(self, application_name=None):
if application_name is not None:
self.application_name = application_name
config_path = self.config_file_path_fmt.format(
directory=self.base_path,
app_name=application_name,
app_name=self.application_name,
)
LOGGER.info("Writing configuration '%s' ...", config_path)
with open(config_path, 'w') as fd:
......@@ -125,6 +134,9 @@ class Plugin(PluginBase):
else:
self.config[section][key] = value
for observer in self.observers[section]:
observer()
def config_get(self, section, key, default=None):
try:
value = self.config[section][key]
......@@ -142,3 +154,9 @@ class Plugin(PluginBase):
section, key, str(default)
)
return default
def config_add_observer(self, section, callback):
self.observers[section].add(callback)
def config_remove_observer(self, section, callback):
self.observers[section].remove(callback)
......@@ -28,8 +28,8 @@ class TestReadWrite(unittest.TestCase):
core = openpaperwork_core.Core()
core.load('openpaperwork_core.config_file')
core.get('openpaperwork_core.config_file').base_path = tempfile.mkdtemp(
prefix='openpaperwork_core_config_tests'
core.get('openpaperwork_core.config_file').base_path = (
tempfile.mkdtemp(prefix='openpaperwork_core_config_tests')
)
core.init()
......@@ -76,3 +76,38 @@ class TestReadWrite(unittest.TestCase):
self.assertEqual(core.get('some_test_module').initialized, True)
self.assertEqual(core.get('some_test_module_2').initialized, True)
def test_observers(self):
core = openpaperwork_core.Core()
core.load('openpaperwork_core.config_file')
core.get('openpaperwork_core.config_file').base_path = (
tempfile.mkdtemp(prefix='openpaperwork_core_config_tests')
)
core.init()
class Observer(object):
def __init__(self):
self.count = 0
def obs(self):
self.count += 1
obs = Observer()
core.call_all('config_add_observer', 'test_section', obs.obs)
core.call_all('config_put', 'other_section', 'test_key', 'test_value')
self.assertEqual(obs.count, 0)
core.call_all('config_put', 'test_section', 'test_key', 'test_value')
self.assertEqual(obs.count, 1)
core.call_all('config_add_plugin', 'some_test_module')
self.assertEqual(obs.count, 1)
core.call_all('config_save', 'openpaperwork_test')
self.assertEqual(obs.count, 1)
core.call_all('config_load', 'openpaperwork_test')
self.assertEqual(obs.count, 2)
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