diff libervia/backend/__init__.py @ 4362:d34b17bce612

core: Add a "Global" variable with a `LiberviaBackend` singleton to `libervia.backend`.
author Goffi <goffi@goffi.org>
date Tue, 06 May 2025 00:16:15 +0200
parents 10b6ad569157
children
line wrap: on
line diff
--- a/libervia/backend/__init__.py	Fri Apr 11 21:49:46 2025 +0200
+++ b/libervia/backend/__init__.py	Tue May 06 00:16:15 2025 +0200
@@ -15,9 +15,36 @@
 
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+from typing import TYPE_CHECKING
 from sat_tmp import wokkel
 
 __version__ = "0.9.0.dev0"
 
 if not wokkel.installed:
     wokkel.install()
+
+if TYPE_CHECKING:
+    from libervia.backend.core.main import LiberviaBackend
+    from libervia.backend.memory.sqla import Storage
+
+
+class Global:
+
+    def __init__(self):
+        self._host: "LiberviaBackend|None" = None
+
+    def set_host(self, host: "LiberviaBackend") -> None:
+        """This method set the backend, and must be called once."""
+        assert self._host is None
+        self._host = host
+
+    @property
+    def host(self) -> "LiberviaBackend":
+        assert self._host is not None
+        return self._host
+
+    @property
+    def storage(self) -> "Storage":
+        return self.host.memory.storage
+
+G = Global()