|
19 | 19 | import tempfile |
20 | 20 | import typing |
21 | 21 | import zipfile |
22 | | -from collections.abc import Callable, Sequence |
| 22 | +from collections.abc import Sequence |
23 | 23 | from datetime import datetime |
24 | 24 | from pathlib import Path |
25 | 25 | from types import ModuleType |
26 | 26 | from typing import NamedTuple |
27 | 27 | from urllib.parse import urlparse |
28 | 28 |
|
29 | 29 | import click |
30 | | -import httpx |
31 | 30 | from alembic.util.exc import CommandError |
32 | 31 | from packaging import version |
33 | 32 | from redis import Redis as RedisSync |
|
39 | 38 | from reflex.config import Config, get_config |
40 | 39 | from reflex.environment import environment |
41 | 40 | from reflex.utils import console, net, path_ops, processes, redir |
| 41 | +from reflex.utils.decorator import cached_procedure |
42 | 42 | from reflex.utils.exceptions import SystemPackageMissingError |
43 | 43 | from reflex.utils.misc import get_module_path |
44 | 44 | from reflex.utils.registry import get_npm_registry |
@@ -1170,6 +1170,8 @@ def download_and_run(url: str, *args, show_status: bool = False, **env): |
1170 | 1170 | Raises: |
1171 | 1171 | Exit: If the script fails to download. |
1172 | 1172 | """ |
| 1173 | + import httpx |
| 1174 | + |
1173 | 1175 | # Download the script |
1174 | 1176 | console.debug(f"Downloading {url}") |
1175 | 1177 | try: |
@@ -1251,71 +1253,9 @@ def install_bun(): |
1251 | 1253 | ) |
1252 | 1254 |
|
1253 | 1255 |
|
1254 | | -def _write_cached_procedure_file(payload: str, cache_file: str | Path): |
1255 | | - cache_file = Path(cache_file) |
1256 | | - cache_file.write_text(payload) |
1257 | | - |
1258 | | - |
1259 | | -def _read_cached_procedure_file(cache_file: str | Path) -> str | None: |
1260 | | - cache_file = Path(cache_file) |
1261 | | - if cache_file.exists(): |
1262 | | - return cache_file.read_text() |
1263 | | - return None |
1264 | | - |
1265 | | - |
1266 | | -def _clear_cached_procedure_file(cache_file: str | Path): |
1267 | | - cache_file = Path(cache_file) |
1268 | | - if cache_file.exists(): |
1269 | | - cache_file.unlink() |
1270 | | - |
1271 | | - |
1272 | | -def cached_procedure( |
1273 | | - cache_file: str | None, |
1274 | | - payload_fn: Callable[..., str], |
1275 | | - cache_file_fn: Callable[[], str] | None = None, |
1276 | | -): |
1277 | | - """Decorator to cache the runs of a procedure on disk. Procedures should not have |
1278 | | - a return value. |
1279 | | -
|
1280 | | - Args: |
1281 | | - cache_file: The file to store the cache payload in. |
1282 | | - payload_fn: Function that computes cache payload from function args. |
1283 | | - cache_file_fn: Function that computes the cache file name at runtime. |
1284 | | -
|
1285 | | - Returns: |
1286 | | - The decorated function. |
1287 | | -
|
1288 | | - Raises: |
1289 | | - ValueError: If both cache_file and cache_file_fn are provided. |
1290 | | - """ |
1291 | | - if cache_file and cache_file_fn is not None: |
1292 | | - msg = "cache_file and cache_file_fn cannot both be provided." |
1293 | | - raise ValueError(msg) |
1294 | | - |
1295 | | - def _inner_decorator(func: Callable): |
1296 | | - def _inner(*args, **kwargs): |
1297 | | - _cache_file = cache_file_fn() if cache_file_fn is not None else cache_file |
1298 | | - if not _cache_file: |
1299 | | - msg = "Unknown cache file, cannot cache result." |
1300 | | - raise ValueError(msg) |
1301 | | - payload = _read_cached_procedure_file(_cache_file) |
1302 | | - new_payload = payload_fn(*args, **kwargs) |
1303 | | - if payload != new_payload: |
1304 | | - _clear_cached_procedure_file(_cache_file) |
1305 | | - func(*args, **kwargs) |
1306 | | - _write_cached_procedure_file(new_payload, _cache_file) |
1307 | | - |
1308 | | - return _inner |
1309 | | - |
1310 | | - return _inner_decorator |
1311 | | - |
1312 | | - |
1313 | 1256 | @cached_procedure( |
1314 | | - cache_file_fn=lambda: str( |
1315 | | - get_web_dir() / "reflex.install_frontend_packages.cached" |
1316 | | - ), |
1317 | | - payload_fn=lambda p, c: f"{sorted(p)!r},{c.json()}", |
1318 | | - cache_file=None, |
| 1257 | + cache_file_path=lambda: get_web_dir() / "reflex.install_frontend_packages.cached", |
| 1258 | + payload_fn=lambda packages, config: f"{sorted(packages)!r},{config.json()}", |
1319 | 1259 | ) |
1320 | 1260 | def install_frontend_packages(packages: set[str], config: Config): |
1321 | 1261 | """Installs the base and custom frontend packages. |
@@ -1725,6 +1665,8 @@ def create_config_init_app_from_remote_template(app_name: str, template_url: str |
1725 | 1665 | Exit: If any download, file operations fail or unexpected zip file format. |
1726 | 1666 |
|
1727 | 1667 | """ |
| 1668 | + import httpx |
| 1669 | + |
1728 | 1670 | # Create a temp directory for the zip download. |
1729 | 1671 | try: |
1730 | 1672 | temp_dir = tempfile.mkdtemp() |
|
0 commit comments