Jelajahi Sumber

move storage path into storage.py

Falko Schindler 1 tahun lalu
induk
melakukan
6d9d03b171
2 mengubah file dengan 5 tambahan dan 5 penghapusan
  1. 0 2
      nicegui/globals.py
  2. 5 3
      nicegui/storage.py

+ 0 - 2
nicegui/globals.py

@@ -1,7 +1,6 @@
 from __future__ import annotations
 
 import asyncio
-import os
 from pathlib import Path
 from typing import TYPE_CHECKING, Dict, List, Literal, Optional, Set, Union
 
@@ -32,7 +31,6 @@ reconnect_timeout: float
 tailwind: bool
 prod_js: bool
 endpoint_documentation: Literal['none', 'internal', 'page', 'all'] = 'none'
-storage_path: Path = Path(os.environ.get('NICEGUI_STORAGE_PATH', '.nicegui')).resolve()
 socket_io_js_query_params: Dict = {}
 socket_io_js_extra_headers: Dict = {}
 socket_io_js_transports: List[Literal['websocket', 'polling']] = ['websocket', 'polling']  # NOTE: we favor websocket

+ 5 - 3
nicegui/storage.py

@@ -1,4 +1,5 @@
 import contextvars
+import os
 import uuid
 from collections.abc import MutableMapping
 from pathlib import Path
@@ -86,7 +87,8 @@ def set_storage_secret(storage_secret: Optional[str] = None) -> None:
 class Storage:
 
     def __init__(self) -> None:
-        self._general = PersistentDict(globals.storage_path / 'storage_general.json')
+        self.path = Path(os.environ.get('NICEGUI_STORAGE_PATH', '.nicegui')).resolve()
+        self._general = PersistentDict(self.path / 'storage_general.json')
         self._users: Dict[str, PersistentDict] = {}
 
     @property
@@ -125,7 +127,7 @@ class Storage:
             raise RuntimeError('app.storage.user needs a storage_secret passed in ui.run()')
         session_id = request.session['id']
         if session_id not in self._users:
-            self._users[session_id] = PersistentDict(globals.storage_path / f'storage_user_{session_id}.json')
+            self._users[session_id] = PersistentDict(self.path / f'storage_user_{session_id}.json')
         return self._users[session_id]
 
     @property
@@ -137,5 +139,5 @@ class Storage:
         """Clears all storage."""
         self._general.clear()
         self._users.clear()
-        for filepath in globals.storage_path.glob('storage_*.json'):
+        for filepath in self.path.glob('storage_*.json'):
             filepath.unlink()