Browse Source

code review

Falko Schindler 1 year ago
parent
commit
ec56d8c084
4 changed files with 10 additions and 17 deletions
  1. 1 1
      nicegui/globals.py
  2. 4 4
      nicegui/run.py
  3. 2 2
      nicegui/run_with.py
  4. 3 10
      nicegui/storage.py

+ 1 - 1
nicegui/globals.py

@@ -41,7 +41,7 @@ reload: bool
 title: str
 viewport: str
 favicon: Optional[Union[str, Path]]
-storage_path: Optional[Union[str, Path]]
+storage_path: Path
 dark: Optional[bool]
 language: Language
 binding_refresh_interval: float

+ 4 - 4
nicegui/run.py

@@ -65,7 +65,7 @@ def run(*,
         prod_js: bool = True,
         endpoint_documentation: Literal['none', 'internal', 'page', 'all'] = 'none',
         storage_secret: Optional[str] = None,
-        storage_path: Optional[Union[str, Path]] = None,
+        storage_path: Union[str, Path] = Path('.nicegui'),
         **kwargs: Any,
         ) -> None:
     '''ui.run
@@ -94,8 +94,8 @@ def run(*,
     :param tailwind: whether to use Tailwind (experimental, default: `True`)
     :param prod_js: whether to use the production version of Vue and Quasar dependencies (default: `True`)
     :param endpoint_documentation: control what endpoints appear in the autogenerated OpenAPI docs (default: 'none', options: 'none', 'internal', 'page', 'all')
-    :param storage_secret: secret key for browser based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
-    :param storage_path: directory for local based storage (default: `None`, local directory is default, 'temp' for temp directory and for a custom path use the absolute path)
+    :param storage_secret: secret key for browser-based storage (default: `None`, a value is required to enable ui.storage.individual and ui.storage.browser)
+    :param storage_path: directory for local storage (default: `.nicegui` in the current working directory)
     :param kwargs: additional keyword arguments are passed to `uvicorn.run`    
     '''
     globals.ui_run_has_been_called = True
@@ -109,7 +109,7 @@ def run(*,
     globals.tailwind = tailwind
     globals.prod_js = prod_js
     globals.endpoint_documentation = endpoint_documentation
-    globals.storage_path = storage_path
+    globals.storage_path = Path(storage_path)
 
     for route in globals.app.routes:
         if not isinstance(route, Route):

+ 2 - 2
nicegui/run_with.py

@@ -21,7 +21,7 @@ def run_with(
     tailwind: bool = True,
     prod_js: bool = True,
     storage_secret: Optional[str] = None,
-    storage_path: Optional[Union[str, Path]] = None,
+    storage_path: Union[str, Path] = Path('.nicegui'),
 ) -> None:
     globals.ui_run_has_been_called = True
     globals.title = title
@@ -32,7 +32,7 @@ def run_with(
     globals.binding_refresh_interval = binding_refresh_interval
     globals.tailwind = tailwind
     globals.prod_js = prod_js
-    globals.storage_path = storage_path
+    globals.storage_path = Path(storage_path)
 
     set_storage_secret(storage_secret)
     app.on_event('startup')(lambda: handle_startup(with_welcome_message=False))

+ 3 - 10
nicegui/storage.py

@@ -75,14 +75,7 @@ class RequestTrackingMiddleware(BaseHTTPMiddleware):
 class Storage:
 
     def __init__(self) -> None:
-        if globals.storage_path == 'temp':
-            self.storage_dir = Path(gettempdir() / 'nicegui')
-        elif globals.storage_path is not None and globals.storage_path != 'temp':
-            self.storage_dir = Path(globals.storage_path)
-        else:
-            self.storage_dir = Path('.nicegui')
-
-        self._general = PersistentDict(self.storage_dir / 'storage_general.json')
+        self._general = PersistentDict(globals.storage_path / 'storage_general.json')
         self._users: Dict[str, PersistentDict] = {}
 
     @property
@@ -123,7 +116,7 @@ class Storage:
                 raise RuntimeError('app.storage.user needs a storage_secret passed in ui.run()')
         id = request.session['id']
         if id not in self._users:
-            self._users[id] = PersistentDict(self.storage_dir / f'storage_user_{id}.json')
+            self._users[id] = PersistentDict(globals.storage_path / f'storage_user_{id}.json')
         return self._users[id]
 
     @property
@@ -135,5 +128,5 @@ class Storage:
         """Clears all storage."""
         self._general.clear()
         self._users.clear()
-        for filepath in self.storage_dir.glob('storage_*.json'):
+        for filepath in globals.storage_path.glob('storage_*.json'):
             filepath.unlink()