Skip to content

Commit

Permalink
Config: Make CoreConfig a singleton object
Browse files Browse the repository at this point in the history
This prevents the configuration from being read and modified in two different
places (`CoreConfig` objects are created in `datastore.py`, `treefactory.py`,
and `backends/__init__.py`).

* GTG/core/config.py (CoreConfig_): Rename from `CoreConfig`.
(CoreConfig_.INSTANCE): New field (use it to store the unique config instance).
(CoreConfig_.__init__): Make sure that we are only ever initialized once.
(CoreConfig_.get_instance): New method.
(CoreConfig): New function to initialize and return the singleton config object.
  • Loading branch information
cpitclaudel committed Sep 19, 2022
1 parent f4d2fc8 commit b27337d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion GTG/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@ def save(self):
self._save_function()


class CoreConfig():
class CoreConfig_():
""" Class holding configuration to all systems and tasks """

_instance = None

def __init__(self):
assert self._instance is None

self._conf_path = os.path.join(CONFIG_DIR, 'gtg.conf')
self._conf = open_config_file(self._conf_path)

Expand All @@ -193,6 +197,11 @@ def __init__(self):
self._backends_conf_path = os.path.join(CONFIG_DIR, 'backends.conf')
self._backends_conf = open_config_file(self._backends_conf_path)

@classmethod
def get_instance(cls):
cls._instance = cls._instance or cls()
return cls._instance

def save_gtg_config(self):
self._conf.write(open(self._conf_path, 'w'))

Expand Down Expand Up @@ -231,3 +240,6 @@ def get_backend_config(self, backend):
self._backends_conf[backend],
DEFAULTS['backend'],
self.save_backends_config)

def CoreConfig():
return CoreConfig_.get_instance()

0 comments on commit b27337d

Please sign in to comment.