# HG changeset patch # User Goffi # Date 1746483375 -7200 # Node ID d34b17bce6123c8f925641b761985c267cf8417a # Parent 676a320415b975f20c6063e6b9486c0d72924c13 core: Add a "Global" variable with a `LiberviaBackend` singleton to `libervia.backend`. diff -r 676a320415b9 -r d34b17bce612 libervia/backend/__init__.py --- 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 . +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() diff -r 676a320415b9 -r d34b17bce612 libervia/backend/core/main.py --- a/libervia/backend/core/main.py Fri Apr 11 21:49:46 2025 +0200 +++ b/libervia/backend/core/main.py Tue May 06 00:16:15 2025 +0200 @@ -36,6 +36,7 @@ from twisted.words.protocols.jabber import jid from twisted.internet import reactor from wokkel.xmppim import RosterItem +from libervia.backend import G from libervia.backend.core import xmpp from libervia.backend.core import exceptions from libervia.backend.core.core_types import SatXMPPEntity @@ -65,6 +66,7 @@ # we don't use __init__ to avoid doule initialisation with twistd # this _init is called in startService log.info(f"{C.APP_NAME} {self.full_version}") + G.set_host(self) self._cb_map = {} # map from callback_id to callbacks # dynamic menus. key: callback_id, value: menu data (dictionnary) self._menus = {}