From b27337db875937fa3255d19ab135a79b9d25d19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pit-Claudel?= Date: Mon, 19 Sep 2022 00:16:04 -0700 Subject: [PATCH] Config: Make CoreConfig a singleton object 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. --- GTG/core/config.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/GTG/core/config.py b/GTG/core/config.py index 9d3138e2c6..70215ca904 100644 --- a/GTG/core/config.py +++ b/GTG/core/config.py @@ -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) @@ -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')) @@ -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()