Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
4361:676a320415b9 | 4362:d34b17bce612 |
---|---|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 # GNU Affero General Public License for more details. | 14 # GNU Affero General Public License for more details. |
15 | 15 |
16 # You should have received a copy of the GNU Affero General Public License | 16 # You should have received a copy of the GNU Affero General Public License |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 from typing import TYPE_CHECKING | |
18 from sat_tmp import wokkel | 19 from sat_tmp import wokkel |
19 | 20 |
20 __version__ = "0.9.0.dev0" | 21 __version__ = "0.9.0.dev0" |
21 | 22 |
22 if not wokkel.installed: | 23 if not wokkel.installed: |
23 wokkel.install() | 24 wokkel.install() |
25 | |
26 if TYPE_CHECKING: | |
27 from libervia.backend.core.main import LiberviaBackend | |
28 from libervia.backend.memory.sqla import Storage | |
29 | |
30 | |
31 class Global: | |
32 | |
33 def __init__(self): | |
34 self._host: "LiberviaBackend|None" = None | |
35 | |
36 def set_host(self, host: "LiberviaBackend") -> None: | |
37 """This method set the backend, and must be called once.""" | |
38 assert self._host is None | |
39 self._host = host | |
40 | |
41 @property | |
42 def host(self) -> "LiberviaBackend": | |
43 assert self._host is not None | |
44 return self._host | |
45 | |
46 @property | |
47 def storage(self) -> "Storage": | |
48 return self.host.memory.storage | |
49 | |
50 G = Global() |