-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
400 lines (306 loc) · 11.3 KB
/
storage.py
File metadata and controls
400 lines (306 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""Cross-platform key/value persistence (``AsyncStorage``).
Mirrors React Native's ``AsyncStorage`` API but with native ``async``
coroutines and a JSON convenience layer. Values are persisted using
the platform-appropriate key/value store:
- **iOS**: ``NSUserDefaults`` (standard user defaults).
- **Android**: ``SharedPreferences`` (file ``"pn_async_storage"``).
- **Desktop / tests**: an in-memory dict optionally backed by a JSON
file under
[`FileSystem.app_dir`][pythonnative.native_modules.file_system.FileSystem.app_dir]
for inter-run persistence during local development.
All operations are coroutines so they don't block the framework loop;
the underlying native calls are dispatched via
:func:`asyncio.to_thread`.
Example:
```python
import pythonnative as pn
async def remember_user(user):
await pn.AsyncStorage.set_json("current_user", user.to_dict())
async def restore_session():
user_dict = await pn.AsyncStorage.get_json("current_user")
if user_dict is None:
return None
return User.from_dict(user_dict)
```
"""
from __future__ import annotations
import asyncio
import json
import os
import threading
from typing import Any, Callable, List, Optional, Tuple, TypeVar
from .utils import IS_ANDROID, IS_IOS
T = TypeVar("T")
# ======================================================================
# Backend selection
# ======================================================================
_DEFAULTS_SUITE = "pn_async_storage"
def _ios_set(key: str, value: str) -> None:
from rubicon.objc import ObjCClass
defaults = ObjCClass("NSUserDefaults").standardUserDefaults
defaults.setObject_forKey_(value, key)
defaults.synchronize()
def _ios_get(key: str) -> Optional[str]:
from rubicon.objc import ObjCClass
defaults = ObjCClass("NSUserDefaults").standardUserDefaults
val = defaults.stringForKey_(key)
if val is None:
return None
try:
return str(val)
except Exception:
return None
def _ios_delete(key: str) -> None:
from rubicon.objc import ObjCClass
defaults = ObjCClass("NSUserDefaults").standardUserDefaults
defaults.removeObjectForKey_(key)
defaults.synchronize()
def _ios_all_keys() -> List[str]:
from rubicon.objc import ObjCClass
defaults = ObjCClass("NSUserDefaults").standardUserDefaults
rep = defaults.dictionaryRepresentation()
if rep is None:
return []
try:
keys = rep.allKeys
return [str(keys.objectAtIndex_(i)) for i in range(keys.count)]
except Exception:
return []
def _ios_clear() -> None:
for key in _ios_all_keys():
_ios_delete(key)
def _android_prefs() -> Any:
from java import jclass
from .utils import get_android_context
Context = jclass("android.content.Context")
ctx = get_android_context()
return ctx.getSharedPreferences(_DEFAULTS_SUITE, Context.MODE_PRIVATE)
def _android_set(key: str, value: str) -> None:
prefs = _android_prefs()
editor = prefs.edit()
editor.putString(key, value)
editor.apply()
def _android_get(key: str) -> Optional[str]:
prefs = _android_prefs()
if not prefs.contains(key):
return None
try:
return str(prefs.getString(key, None))
except Exception:
return None
def _android_delete(key: str) -> None:
prefs = _android_prefs()
editor = prefs.edit()
editor.remove(key)
editor.apply()
def _android_all_keys() -> List[str]:
prefs = _android_prefs()
all_map = prefs.getAll()
keys = all_map.keySet()
it = keys.iterator()
out: List[str] = []
while it.hasNext():
out.append(str(it.next()))
return out
def _android_clear() -> None:
prefs = _android_prefs()
editor = prefs.edit()
editor.clear()
editor.apply()
# ======================================================================
# Desktop / test backend
# ======================================================================
_desktop_lock = threading.Lock()
_desktop_store: dict = {}
_desktop_loaded = False
def _desktop_path() -> Optional[str]:
# Tests can opt out of disk persistence by leaving the env var unset.
base = os.environ.get("PN_STORAGE_DIR")
if not base:
return None
try:
os.makedirs(base, exist_ok=True)
except OSError:
return None
return os.path.join(base, "pn_async_storage.json")
def _desktop_load() -> None:
global _desktop_loaded
if _desktop_loaded:
return
_desktop_loaded = True
path = _desktop_path()
if path is None or not os.path.exists(path):
return
try:
with open(path, encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
_desktop_store.update({str(k): str(v) for k, v in data.items()})
except OSError:
return
except json.JSONDecodeError:
return
def _desktop_persist() -> None:
path = _desktop_path()
if path is None:
return
try:
with open(path, "w", encoding="utf-8") as f:
json.dump(_desktop_store, f)
except OSError:
pass
def _desktop_set(key: str, value: str) -> None:
with _desktop_lock:
_desktop_load()
_desktop_store[key] = value
_desktop_persist()
def _desktop_get(key: str) -> Optional[str]:
with _desktop_lock:
_desktop_load()
return _desktop_store.get(key)
def _desktop_delete(key: str) -> None:
with _desktop_lock:
_desktop_load()
_desktop_store.pop(key, None)
_desktop_persist()
def _desktop_all_keys() -> List[str]:
with _desktop_lock:
_desktop_load()
return list(_desktop_store.keys())
def _desktop_clear() -> None:
with _desktop_lock:
_desktop_store.clear()
_desktop_persist()
# ======================================================================
# Public API
# ======================================================================
class AsyncStorage:
"""Async key/value persistence layered on platform-native stores.
Every method is a coroutine that returns when the underlying
native operation completes. Strings round-trip directly via
[`get`][pythonnative.storage.AsyncStorage.get] /
[`set`][pythonnative.storage.AsyncStorage.set]; richer values can
use [`get_json`][pythonnative.storage.AsyncStorage.get_json] /
[`set_json`][pythonnative.storage.AsyncStorage.set_json] which
add a JSON encode/decode step.
"""
@staticmethod
async def get(key: str) -> Optional[str]:
"""Return the string stored at ``key``, or ``None`` if missing."""
if IS_IOS:
return await asyncio.to_thread(_ios_get, key)
if IS_ANDROID:
return await asyncio.to_thread(_android_get, key)
return await asyncio.to_thread(_desktop_get, key)
@staticmethod
async def set(key: str, value: str) -> None:
"""Persist ``value`` under ``key``.
``value`` must be a ``str``. For non-string values, use
[`set_json`][pythonnative.storage.AsyncStorage.set_json].
"""
if not isinstance(value, str):
raise TypeError("AsyncStorage.set requires a str value; use set_json for richer types")
if IS_IOS:
await asyncio.to_thread(_ios_set, key, value)
elif IS_ANDROID:
await asyncio.to_thread(_android_set, key, value)
else:
await asyncio.to_thread(_desktop_set, key, value)
@staticmethod
async def delete(key: str) -> None:
"""Remove the entry at ``key`` if present (no-op otherwise)."""
if IS_IOS:
await asyncio.to_thread(_ios_delete, key)
elif IS_ANDROID:
await asyncio.to_thread(_android_delete, key)
else:
await asyncio.to_thread(_desktop_delete, key)
@staticmethod
async def all_keys() -> List[str]:
"""Return every persisted key (order is platform-dependent)."""
if IS_IOS:
return await asyncio.to_thread(_ios_all_keys)
if IS_ANDROID:
return await asyncio.to_thread(_android_all_keys)
return await asyncio.to_thread(_desktop_all_keys)
@staticmethod
async def clear() -> None:
"""Remove every entry written through ``AsyncStorage``."""
if IS_IOS:
await asyncio.to_thread(_ios_clear)
elif IS_ANDROID:
await asyncio.to_thread(_android_clear)
else:
await asyncio.to_thread(_desktop_clear)
@staticmethod
async def get_json(key: str) -> Any:
"""Return the JSON-decoded value stored at ``key``, or ``None``.
If the stored value isn't valid JSON, returns ``None`` rather
than raising — assume the entry was written by another
process or an older version of the app.
"""
raw = await AsyncStorage.get(key)
if raw is None:
return None
try:
return json.loads(raw)
except (json.JSONDecodeError, TypeError):
return None
@staticmethod
async def set_json(key: str, value: Any) -> None:
"""JSON-encode ``value`` and persist it under ``key``."""
await AsyncStorage.set(key, json.dumps(value, default=str))
def use_persisted_state(
key: str,
initial: T,
) -> Tuple[T, Callable[[Any], None]]:
"""Persisted [`use_state`][pythonnative.hooks.use_state] variant.
Backed by [`AsyncStorage`][pythonnative.storage.AsyncStorage]:
behaves like ``use_state`` but loads the prior value (if any) on
mount and persists every subsequent update. Until the load
completes the value is ``initial`` — the same fallback React
Native users get with ``AsyncStorage.getItem``.
The setter accepts either a value or a ``current -> new``
callable, matching
[`use_state`][pythonnative.use_state]. Writes are
fire-and-forget; failures are silently absorbed (storage is
best-effort by design).
Args:
key: Storage key. Pass a stable, namespaced string
(e.g. ``"settings.theme"``).
initial: Value used before the first load completes.
Returns:
``(value, setter)`` — same shape as
[`use_state`][pythonnative.use_state].
Example:
```python
import pythonnative as pn
@pn.component
def ThemeToggle():
theme, set_theme = pn.use_persisted_state("settings.theme", "light")
return pn.Button(
f"Theme: {theme}",
on_click=lambda: set_theme("dark" if theme == "light" else "light"),
)
```
"""
from .hooks import use_async_effect, use_callback, use_ref, use_state
from .runtime import run_async
state, set_state = use_state(initial)
loaded = use_ref(False)
async def _load() -> None:
stored = await AsyncStorage.get_json(key)
if stored is not None:
set_state(stored)
loaded["current"] = True
use_async_effect(_load, [key])
def setter(value_or_updater: Any) -> None:
def _reducer(current: Any) -> Any:
new_value = value_or_updater(current) if callable(value_or_updater) else value_or_updater
if loaded["current"] is True:
run_async(AsyncStorage.set_json(key, new_value))
return new_value
set_state(_reducer)
stable_setter = use_callback(setter, [key])
return state, stable_setter
__all__ = ["AsyncStorage", "use_persisted_state"]