-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathutils.py
546 lines (438 loc) ยท 15.8 KB
/
utils.py
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
"""Common utility functions used in the compiler."""
from __future__ import annotations
import asyncio
import concurrent.futures
import traceback
from datetime import datetime
from pathlib import Path
from typing import Any, Callable, Type
from urllib.parse import urlparse
from pydantic.v1.fields import ModelField
from reflex import constants
from reflex.components.base import (
Body,
Description,
DocumentHead,
Head,
Html,
Image,
Main,
Meta,
NextScript,
Title,
)
from reflex.components.component import Component, ComponentStyle, CustomComponent
from reflex.istate.storage import Cookie, LocalStorage, SessionStorage
from reflex.state import BaseState, StateDelta, _resolve_delta
from reflex.style import Style
from reflex.utils import console, format, imports, path_ops
from reflex.utils.exec import is_in_app_harness
from reflex.utils.imports import ImportVar, ParsedImportDict
from reflex.utils.prerequisites import get_web_dir
from reflex.vars.base import Var
# To re-export this function.
merge_imports = imports.merge_imports
def compile_import_statement(fields: list[ImportVar]) -> tuple[str, list[str]]:
"""Compile an import statement.
Args:
fields: The set of fields to import from the library.
Raises:
ValueError: If there is more than one default import.
Returns:
The libraries for default and rest.
default: default library. When install "import def from library".
rest: rest of libraries. When install "import {rest1, rest2} from library"
"""
# ignore the ImportVar fields with render=False during compilation
fields_set = {field for field in fields if field.render}
# Check for default imports.
defaults = {field for field in fields_set if field.is_default}
if len(defaults) >= 2:
raise ValueError("Only one default import is allowed.")
# Get the default import, and the specific imports.
default = next(iter({field.name for field in defaults}), "")
rest = {field.name for field in fields_set - defaults}
return default, list(rest)
def validate_imports(import_dict: ParsedImportDict):
"""Verify that the same Tag is not used in multiple import.
Args:
import_dict: The dict of imports to validate
Raises:
ValueError: if a conflict on "tag/alias" is detected for an import.
"""
used_tags = {}
for lib, _imports in import_dict.items():
for _import in _imports:
import_name = (
f"{_import.tag}/{_import.alias}" if _import.alias else _import.tag
)
if import_name in used_tags:
already_imported = used_tags[import_name]
if (already_imported[0] == "$" and already_imported[1:] == lib) or (
lib[0] == "$" and lib[1:] == already_imported
):
used_tags[import_name] = lib if lib[0] == "$" else already_imported
continue
raise ValueError(
f"Can not compile, the tag {import_name} is used multiple time from {lib} and {used_tags[import_name]}"
)
if import_name is not None:
used_tags[import_name] = lib
def compile_imports(import_dict: ParsedImportDict) -> list[dict]:
"""Compile an import dict.
Args:
import_dict: The import dict to compile.
Raises:
ValueError: If an import in the dict is invalid.
Returns:
The list of import dict.
"""
collapsed_import_dict: ParsedImportDict = imports.collapse_imports(import_dict)
validate_imports(collapsed_import_dict)
import_dicts = []
for lib, fields in collapsed_import_dict.items():
# prevent lib from being rendered on the page if all imports are non rendered kind
if not any(f.render for f in fields):
continue
lib_paths: dict[str, list[ImportVar]] = {}
for field in fields:
lib_paths.setdefault(field.package_path, []).append(field)
compiled = {
path: compile_import_statement(fields) for path, fields in lib_paths.items()
}
for path, (default, rest) in compiled.items():
if not lib:
if default:
raise ValueError("No default field allowed for empty library.")
if rest is None or len(rest) == 0:
raise ValueError("No fields to import.")
import_dicts.extend(get_import_dict(module) for module in sorted(rest))
continue
# remove the version before rendering the package imports
formatted_lib = format.format_library_name(lib) + (
path if path != "/" else ""
)
import_dicts.append(get_import_dict(formatted_lib, default, rest))
return import_dicts
def get_import_dict(lib: str, default: str = "", rest: list[str] | None = None) -> dict:
"""Get dictionary for import template.
Args:
lib: The importing react library.
default: The default module to import.
rest: The rest module to import.
Returns:
A dictionary for import template.
"""
return {
"lib": lib,
"default": default,
"rest": rest if rest else [],
}
def save_error(error: Exception) -> str:
"""Save the error to a file.
Args:
error: The error to save.
Returns:
The path of the saved error.
"""
timestamp = datetime.now().strftime("%Y-%m-%d__%H-%M-%S")
constants.Reflex.LOGS_DIR.mkdir(parents=True, exist_ok=True)
log_path = constants.Reflex.LOGS_DIR / f"error_{timestamp}.log"
traceback.TracebackException.from_exception(error).print(file=log_path.open("w+"))
return str(log_path)
def compile_state(state: Type[BaseState]) -> dict:
"""Compile the state of the app.
Args:
state: The app state object.
Returns:
A dictionary of the compiled state.
"""
initial_state = StateDelta(state(_reflex_internal_init=True).dict(initial=True))
try:
_ = asyncio.get_running_loop()
except RuntimeError:
pass
else:
if is_in_app_harness():
# Playwright tests already have an event loop running, so we can't use asyncio.run.
with concurrent.futures.ThreadPoolExecutor() as pool:
resolved_initial_state = pool.submit(
asyncio.run, _resolve_delta(initial_state)
).result()
console.warn(
f"Had to get initial state in a thread ๐คฎ {resolved_initial_state}",
)
return dict(**resolved_initial_state.data)
# Normally the compile runs before any event loop starts, we asyncio.run is available for calling.
return dict(**asyncio.run(_resolve_delta(initial_state)).data)
def _compile_client_storage_field(
field: ModelField,
) -> tuple[
Type[Cookie] | Type[LocalStorage] | Type[SessionStorage] | None,
dict[str, Any] | None,
]:
"""Compile the given cookie, local_storage or session_storage field.
Args:
field: The possible cookie field to compile.
Returns:
A dictionary of the compiled cookie or None if the field is not cookie-like.
"""
for field_type in (Cookie, LocalStorage, SessionStorage):
if isinstance(field.default, field_type):
cs_obj = field.default
elif isinstance(field.type_, type) and issubclass(field.type_, field_type):
cs_obj = field.type_()
else:
continue
return field_type, cs_obj.options()
return None, None
def _compile_client_storage_recursive(
state: Type[BaseState],
) -> tuple[dict[str, dict], dict[str, dict], dict[str, dict]]:
"""Compile the client-side storage for the given state recursively.
Args:
state: The app state object.
Returns:
A tuple of the compiled client-side storage info:
(
cookies: dict[str, dict],
local_storage: dict[str, dict[str, str]]
session_storage: dict[str, dict[str, str]]
).
"""
cookies = {}
local_storage = {}
session_storage = {}
state_name = state.get_full_name()
for name, field in state.__fields__.items():
if name in state.inherited_vars:
# only include vars defined in this state
continue
state_key = f"{state_name}.{name}"
field_type, options = _compile_client_storage_field(field)
if field_type is Cookie:
cookies[state_key] = options
elif field_type is LocalStorage:
local_storage[state_key] = options
elif field_type is SessionStorage:
session_storage[state_key] = options
else:
continue
for substate in state.get_substates():
(
substate_cookies,
substate_local_storage,
substate_session_storage,
) = _compile_client_storage_recursive(substate)
cookies.update(substate_cookies)
local_storage.update(substate_local_storage)
session_storage.update(substate_session_storage)
return cookies, local_storage, session_storage
def compile_client_storage(state: Type[BaseState]) -> dict[str, dict]:
"""Compile the client-side storage for the given state.
Args:
state: The app state object.
Returns:
A dictionary of the compiled client-side storage info.
"""
cookies, local_storage, session_storage = _compile_client_storage_recursive(state)
return {
constants.COOKIES: cookies,
constants.LOCAL_STORAGE: local_storage,
constants.SESSION_STORAGE: session_storage,
}
def compile_custom_component(
component: CustomComponent,
) -> tuple[dict, ParsedImportDict]:
"""Compile a custom component.
Args:
component: The custom component to compile.
Returns:
A tuple of the compiled component and the imports required by the component.
"""
# Render the component.
render = component.get_component(component)
# Get the imports.
imports: ParsedImportDict = {
lib: fields
for lib, fields in render._get_all_imports().items()
if lib != component.library
}
# Concatenate the props.
props = [prop._js_expr for prop in component.get_prop_vars()]
# Compile the component.
return (
{
"name": component.tag,
"props": props,
"render": render.render(),
"hooks": render._get_all_hooks(),
"custom_code": render._get_all_custom_code(),
"dynamic_imports": render._get_all_dynamic_imports(),
},
imports,
)
def create_document_root(
head_components: list[Component] | None = None,
html_lang: str | None = None,
html_custom_attrs: dict[str, Var | str] | None = None,
) -> Component:
"""Create the document root.
Args:
head_components: The components to add to the head.
html_lang: The language of the document, will be added to the html root element.
html_custom_attrs: custom attributes added to the html root element.
Returns:
The document root.
"""
head_components = head_components or []
return Html.create(
DocumentHead.create(*head_components),
Body.create(
Main.create(),
NextScript.create(),
),
lang=html_lang or "en",
custom_attrs=html_custom_attrs or {},
)
def create_theme(style: ComponentStyle) -> dict:
"""Create the base style for the app.
Args:
style: The style dict for the app.
Returns:
The base style for the app.
"""
# Get the global style from the style dict.
style_rules = Style({k: v for k, v in style.items() if not isinstance(k, Callable)})
root_style = {
# Root styles.
":root": Style(
{f"*{k}": v for k, v in style_rules.items() if k.startswith(":")}
),
# Body styles.
"body": Style(
{k: v for k, v in style_rules.items() if not k.startswith(":")},
),
}
# Return the theme.
return {"styles": {"global": root_style}}
def get_page_path(path: str) -> str:
"""Get the path of the compiled JS file for the given page.
Args:
path: The path of the page.
Returns:
The path of the compiled JS file.
"""
return str(get_web_dir() / constants.Dirs.PAGES / (path + constants.Ext.JS))
def get_theme_path() -> str:
"""Get the path of the base theme style.
Returns:
The path of the theme style.
"""
return str(
get_web_dir()
/ constants.Dirs.UTILS
/ (constants.PageNames.THEME + constants.Ext.JS)
)
def get_root_stylesheet_path() -> str:
"""Get the path of the app root file.
Returns:
The path of the app root file.
"""
return str(
get_web_dir()
/ constants.Dirs.STYLES
/ (constants.PageNames.STYLESHEET_ROOT + constants.Ext.CSS)
)
def get_context_path() -> str:
"""Get the path of the context / initial state file.
Returns:
The path of the context module.
"""
return str(get_web_dir() / (constants.Dirs.CONTEXTS_PATH + constants.Ext.JS))
def get_components_path() -> str:
"""Get the path of the compiled components.
Returns:
The path of the compiled components.
"""
return str(
get_web_dir()
/ constants.Dirs.UTILS
/ (constants.PageNames.COMPONENTS + constants.Ext.JS),
)
def get_stateful_components_path() -> str:
"""Get the path of the compiled stateful components.
Returns:
The path of the compiled stateful components.
"""
return str(
get_web_dir()
/ constants.Dirs.UTILS
/ (constants.PageNames.STATEFUL_COMPONENTS + constants.Ext.JS)
)
def add_meta(
page: Component,
title: str,
image: str,
meta: list[dict],
description: str | None = None,
) -> Component:
"""Add metadata to a page.
Args:
page: The component for the page.
title: The title of the page.
image: The image for the page.
meta: The metadata list.
description: The description of the page.
Returns:
The component with the metadata added.
"""
meta_tags = [
item if isinstance(item, Component) else Meta.create(**item) for item in meta
]
children: list[Any] = [Title.create(title)]
if description:
children.append(Description.create(content=description))
children.append(Image.create(content=image))
page.children.append(
Head.create(
*children,
*meta_tags,
)
)
return page
def write_page(path: str | Path, code: str):
"""Write the given code to the given path.
Args:
path: The path to write the code to.
code: The code to write.
"""
path = Path(path)
path_ops.mkdir(path.parent)
if path.exists() and path.read_text(encoding="utf-8") == code:
return
path.write_text(code, encoding="utf-8")
def empty_dir(path: str | Path, keep_files: list[str] | None = None):
"""Remove all files and folders in a directory except for the keep_files.
Args:
path: The path to the directory that will be emptied
keep_files: List of filenames or foldernames that will not be deleted.
"""
path = Path(path)
# If the directory does not exist, return.
if not path.exists():
return
# Remove all files and folders in the directory.
keep_files = keep_files or []
for element in path.iterdir():
if element.name not in keep_files:
path_ops.rm(element)
def is_valid_url(url: str) -> bool:
"""Check if a url is valid.
Args:
url: The Url to check.
Returns:
Whether url is valid.
"""
result = urlparse(url)
return all([result.scheme, result.netloc])