utils.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. """Common utility functions used in the compiler."""
  2. from __future__ import annotations
  3. import asyncio
  4. import concurrent.futures
  5. import traceback
  6. from datetime import datetime
  7. from pathlib import Path
  8. from typing import Any, Callable, Dict, Optional, Type, Union
  9. from urllib.parse import urlparse
  10. from reflex.utils.exec import is_in_app_harness
  11. from reflex.utils.prerequisites import get_web_dir
  12. from reflex.vars.base import Var
  13. try:
  14. from pydantic.v1.fields import ModelField
  15. except ModuleNotFoundError:
  16. from pydantic.fields import (
  17. ModelField, # pyright: ignore [reportAttributeAccessIssue]
  18. )
  19. from reflex import constants
  20. from reflex.components.base import (
  21. Body,
  22. Description,
  23. DocumentHead,
  24. Head,
  25. Html,
  26. Image,
  27. Main,
  28. Meta,
  29. NextScript,
  30. Title,
  31. )
  32. from reflex.components.component import Component, ComponentStyle, CustomComponent
  33. from reflex.istate.storage import Cookie, LocalStorage, SessionStorage
  34. from reflex.state import BaseState, _resolve_delta
  35. from reflex.style import Style
  36. from reflex.utils import console, format, imports, path_ops
  37. from reflex.utils.imports import ImportVar, ParsedImportDict
  38. # To re-export this function.
  39. merge_imports = imports.merge_imports
  40. def compile_import_statement(fields: list[ImportVar]) -> tuple[str, list[str]]:
  41. """Compile an import statement.
  42. Args:
  43. fields: The set of fields to import from the library.
  44. Raises:
  45. ValueError: If there is more than one default import.
  46. Returns:
  47. The libraries for default and rest.
  48. default: default library. When install "import def from library".
  49. rest: rest of libraries. When install "import {rest1, rest2} from library"
  50. """
  51. # ignore the ImportVar fields with render=False during compilation
  52. fields_set = {field for field in fields if field.render}
  53. # Check for default imports.
  54. defaults = {field for field in fields_set if field.is_default}
  55. if len(defaults) >= 2:
  56. raise ValueError("Only one default import is allowed.")
  57. # Get the default import, and the specific imports.
  58. default = next(iter({field.name for field in defaults}), "")
  59. rest = {field.name for field in fields_set - defaults}
  60. return default, list(rest)
  61. def validate_imports(import_dict: ParsedImportDict):
  62. """Verify that the same Tag is not used in multiple import.
  63. Args:
  64. import_dict: The dict of imports to validate
  65. Raises:
  66. ValueError: if a conflict on "tag/alias" is detected for an import.
  67. """
  68. used_tags = {}
  69. for lib, _imports in import_dict.items():
  70. for _import in _imports:
  71. import_name = (
  72. f"{_import.tag}/{_import.alias}" if _import.alias else _import.tag
  73. )
  74. if import_name in used_tags:
  75. already_imported = used_tags[import_name]
  76. if (already_imported[0] == "$" and already_imported[1:] == lib) or (
  77. lib[0] == "$" and lib[1:] == already_imported
  78. ):
  79. used_tags[import_name] = lib if lib[0] == "$" else already_imported
  80. continue
  81. raise ValueError(
  82. f"Can not compile, the tag {import_name} is used multiple time from {lib} and {used_tags[import_name]}"
  83. )
  84. if import_name is not None:
  85. used_tags[import_name] = lib
  86. def compile_imports(import_dict: ParsedImportDict) -> list[dict]:
  87. """Compile an import dict.
  88. Args:
  89. import_dict: The import dict to compile.
  90. Raises:
  91. ValueError: If an import in the dict is invalid.
  92. Returns:
  93. The list of import dict.
  94. """
  95. collapsed_import_dict: ParsedImportDict = imports.collapse_imports(import_dict)
  96. validate_imports(collapsed_import_dict)
  97. import_dicts = []
  98. for lib, fields in collapsed_import_dict.items():
  99. default, rest = compile_import_statement(fields)
  100. # prevent lib from being rendered on the page if all imports are non rendered kind
  101. if not any(f.render for f in fields):
  102. continue
  103. if not lib:
  104. if default:
  105. raise ValueError("No default field allowed for empty library.")
  106. if rest is None or len(rest) == 0:
  107. raise ValueError("No fields to import.")
  108. import_dicts.extend(get_import_dict(module) for module in sorted(rest))
  109. continue
  110. # remove the version before rendering the package imports
  111. lib = format.format_library_name(lib)
  112. import_dicts.append(get_import_dict(lib, default, rest))
  113. return import_dicts
  114. def get_import_dict(lib: str, default: str = "", rest: list[str] | None = None) -> dict:
  115. """Get dictionary for import template.
  116. Args:
  117. lib: The importing react library.
  118. default: The default module to import.
  119. rest: The rest module to import.
  120. Returns:
  121. A dictionary for import template.
  122. """
  123. return {
  124. "lib": lib,
  125. "default": default,
  126. "rest": rest if rest else [],
  127. }
  128. def save_error(error: Exception) -> str:
  129. """Save the error to a file.
  130. Args:
  131. error: The error to save.
  132. Returns:
  133. The path of the saved error.
  134. """
  135. timestamp = datetime.now().strftime("%Y-%m-%d__%H-%M-%S")
  136. constants.Reflex.LOGS_DIR.mkdir(parents=True, exist_ok=True)
  137. log_path = constants.Reflex.LOGS_DIR / f"error_{timestamp}.log"
  138. traceback.TracebackException.from_exception(error).print(file=log_path.open("w+"))
  139. return str(log_path)
  140. def compile_state(state: Type[BaseState]) -> dict:
  141. """Compile the state of the app.
  142. Args:
  143. state: The app state object.
  144. Returns:
  145. A dictionary of the compiled state.
  146. """
  147. try:
  148. initial_state = state(_reflex_internal_init=True).dict(initial=True)
  149. except Exception as e:
  150. log_path = save_error(e)
  151. console.warn(
  152. f"Failed to compile initial state with computed vars. Error log saved to {log_path}"
  153. )
  154. initial_state = state(_reflex_internal_init=True).dict(
  155. initial=True, include_computed=False
  156. )
  157. try:
  158. _ = asyncio.get_running_loop()
  159. except RuntimeError:
  160. pass
  161. else:
  162. if is_in_app_harness():
  163. # Playwright tests already have an event loop running, so we can't use asyncio.run.
  164. with concurrent.futures.ThreadPoolExecutor() as pool:
  165. resolved_initial_state = pool.submit(
  166. asyncio.run, _resolve_delta(initial_state)
  167. ).result()
  168. console.warn(
  169. f"Had to get initial state in a thread 🤮 {resolved_initial_state}",
  170. )
  171. return resolved_initial_state
  172. # Normally the compile runs before any event loop starts, we asyncio.run is available for calling.
  173. return asyncio.run(_resolve_delta(initial_state))
  174. def _compile_client_storage_field(
  175. field: ModelField,
  176. ) -> tuple[
  177. Type[Cookie] | Type[LocalStorage] | Type[SessionStorage] | None,
  178. dict[str, Any] | None,
  179. ]:
  180. """Compile the given cookie, local_storage or session_storage field.
  181. Args:
  182. field: The possible cookie field to compile.
  183. Returns:
  184. A dictionary of the compiled cookie or None if the field is not cookie-like.
  185. """
  186. for field_type in (Cookie, LocalStorage, SessionStorage):
  187. if isinstance(field.default, field_type):
  188. cs_obj = field.default
  189. elif isinstance(field.type_, type) and issubclass(field.type_, field_type):
  190. cs_obj = field.type_()
  191. else:
  192. continue
  193. return field_type, cs_obj.options()
  194. return None, None
  195. def _compile_client_storage_recursive(
  196. state: Type[BaseState],
  197. ) -> tuple[dict[str, dict], dict[str, dict], dict[str, dict]]:
  198. """Compile the client-side storage for the given state recursively.
  199. Args:
  200. state: The app state object.
  201. Returns:
  202. A tuple of the compiled client-side storage info:
  203. (
  204. cookies: dict[str, dict],
  205. local_storage: dict[str, dict[str, str]]
  206. session_storage: dict[str, dict[str, str]]
  207. ).
  208. """
  209. cookies = {}
  210. local_storage = {}
  211. session_storage = {}
  212. state_name = state.get_full_name()
  213. for name, field in state.__fields__.items():
  214. if name in state.inherited_vars:
  215. # only include vars defined in this state
  216. continue
  217. state_key = f"{state_name}.{name}"
  218. field_type, options = _compile_client_storage_field(field)
  219. if field_type is Cookie:
  220. cookies[state_key] = options
  221. elif field_type is LocalStorage:
  222. local_storage[state_key] = options
  223. elif field_type is SessionStorage:
  224. session_storage[state_key] = options
  225. else:
  226. continue
  227. for substate in state.get_substates():
  228. (
  229. substate_cookies,
  230. substate_local_storage,
  231. substate_session_storage,
  232. ) = _compile_client_storage_recursive(substate)
  233. cookies.update(substate_cookies)
  234. local_storage.update(substate_local_storage)
  235. session_storage.update(substate_session_storage)
  236. return cookies, local_storage, session_storage
  237. def compile_client_storage(state: Type[BaseState]) -> dict[str, dict]:
  238. """Compile the client-side storage for the given state.
  239. Args:
  240. state: The app state object.
  241. Returns:
  242. A dictionary of the compiled client-side storage info.
  243. """
  244. cookies, local_storage, session_storage = _compile_client_storage_recursive(state)
  245. return {
  246. constants.COOKIES: cookies,
  247. constants.LOCAL_STORAGE: local_storage,
  248. constants.SESSION_STORAGE: session_storage,
  249. }
  250. def compile_custom_component(
  251. component: CustomComponent,
  252. ) -> tuple[dict, ParsedImportDict]:
  253. """Compile a custom component.
  254. Args:
  255. component: The custom component to compile.
  256. Returns:
  257. A tuple of the compiled component and the imports required by the component.
  258. """
  259. # Render the component.
  260. render = component.get_component(component)
  261. # Get the imports.
  262. imports: ParsedImportDict = {
  263. lib: fields
  264. for lib, fields in render._get_all_imports().items()
  265. if lib != component.library
  266. }
  267. # Concatenate the props.
  268. props = [prop._js_expr for prop in component.get_prop_vars()]
  269. # Compile the component.
  270. return (
  271. {
  272. "name": component.tag,
  273. "props": props,
  274. "render": render.render(),
  275. "hooks": render._get_all_hooks(),
  276. "custom_code": render._get_all_custom_code(),
  277. "dynamic_imports": render._get_all_dynamic_imports(),
  278. },
  279. imports,
  280. )
  281. def create_document_root(
  282. head_components: list[Component] | None = None,
  283. html_lang: Optional[str] = None,
  284. html_custom_attrs: Optional[Dict[str, Union[Var, str]]] = None,
  285. ) -> Component:
  286. """Create the document root.
  287. Args:
  288. head_components: The components to add to the head.
  289. html_lang: The language of the document, will be added to the html root element.
  290. html_custom_attrs: custom attributes added to the html root element.
  291. Returns:
  292. The document root.
  293. """
  294. head_components = head_components or []
  295. return Html.create(
  296. DocumentHead.create(*head_components),
  297. Body.create(
  298. Main.create(),
  299. NextScript.create(),
  300. ),
  301. lang=html_lang or "en",
  302. custom_attrs=html_custom_attrs or {},
  303. )
  304. def create_theme(style: ComponentStyle) -> dict:
  305. """Create the base style for the app.
  306. Args:
  307. style: The style dict for the app.
  308. Returns:
  309. The base style for the app.
  310. """
  311. # Get the global style from the style dict.
  312. style_rules = Style({k: v for k, v in style.items() if not isinstance(k, Callable)})
  313. root_style = {
  314. # Root styles.
  315. ":root": Style(
  316. {f"*{k}": v for k, v in style_rules.items() if k.startswith(":")}
  317. ),
  318. # Body styles.
  319. "body": Style(
  320. {k: v for k, v in style_rules.items() if not k.startswith(":")},
  321. ),
  322. }
  323. # Return the theme.
  324. return {"styles": {"global": root_style}}
  325. def get_page_path(path: str) -> str:
  326. """Get the path of the compiled JS file for the given page.
  327. Args:
  328. path: The path of the page.
  329. Returns:
  330. The path of the compiled JS file.
  331. """
  332. return str(get_web_dir() / constants.Dirs.PAGES / (path + constants.Ext.JS))
  333. def get_theme_path() -> str:
  334. """Get the path of the base theme style.
  335. Returns:
  336. The path of the theme style.
  337. """
  338. return str(
  339. get_web_dir()
  340. / constants.Dirs.UTILS
  341. / (constants.PageNames.THEME + constants.Ext.JS)
  342. )
  343. def get_root_stylesheet_path() -> str:
  344. """Get the path of the app root file.
  345. Returns:
  346. The path of the app root file.
  347. """
  348. return str(
  349. get_web_dir()
  350. / constants.Dirs.STYLES
  351. / (constants.PageNames.STYLESHEET_ROOT + constants.Ext.CSS)
  352. )
  353. def get_context_path() -> str:
  354. """Get the path of the context / initial state file.
  355. Returns:
  356. The path of the context module.
  357. """
  358. return str(get_web_dir() / (constants.Dirs.CONTEXTS_PATH + constants.Ext.JS))
  359. def get_components_path() -> str:
  360. """Get the path of the compiled components.
  361. Returns:
  362. The path of the compiled components.
  363. """
  364. return str(
  365. get_web_dir()
  366. / constants.Dirs.UTILS
  367. / (constants.PageNames.COMPONENTS + constants.Ext.JS),
  368. )
  369. def get_stateful_components_path() -> str:
  370. """Get the path of the compiled stateful components.
  371. Returns:
  372. The path of the compiled stateful components.
  373. """
  374. return str(
  375. get_web_dir()
  376. / constants.Dirs.UTILS
  377. / (constants.PageNames.STATEFUL_COMPONENTS + constants.Ext.JS)
  378. )
  379. def add_meta(
  380. page: Component,
  381. title: str,
  382. image: str,
  383. meta: list[dict],
  384. description: str | None = None,
  385. ) -> Component:
  386. """Add metadata to a page.
  387. Args:
  388. page: The component for the page.
  389. title: The title of the page.
  390. image: The image for the page.
  391. meta: The metadata list.
  392. description: The description of the page.
  393. Returns:
  394. The component with the metadata added.
  395. """
  396. meta_tags = [
  397. item if isinstance(item, Component) else Meta.create(**item) for item in meta
  398. ]
  399. children: list[Any] = [Title.create(title)]
  400. if description:
  401. children.append(Description.create(content=description))
  402. children.append(Image.create(content=image))
  403. page.children.append(
  404. Head.create(
  405. *children,
  406. *meta_tags,
  407. )
  408. )
  409. return page
  410. def write_page(path: str | Path, code: str):
  411. """Write the given code to the given path.
  412. Args:
  413. path: The path to write the code to.
  414. code: The code to write.
  415. """
  416. path = Path(path)
  417. path_ops.mkdir(path.parent)
  418. path.write_text(code, encoding="utf-8")
  419. def empty_dir(path: str | Path, keep_files: list[str] | None = None):
  420. """Remove all files and folders in a directory except for the keep_files.
  421. Args:
  422. path: The path to the directory that will be emptied
  423. keep_files: List of filenames or foldernames that will not be deleted.
  424. """
  425. path = Path(path)
  426. # If the directory does not exist, return.
  427. if not path.exists():
  428. return
  429. # Remove all files and folders in the directory.
  430. keep_files = keep_files or []
  431. for element in path.iterdir():
  432. if element.name not in keep_files:
  433. path_ops.rm(element)
  434. def is_valid_url(url: str) -> bool:
  435. """Check if a url is valid.
  436. Args:
  437. url: The Url to check.
  438. Returns:
  439. Whether url is valid.
  440. """
  441. result = urlparse(url)
  442. return all([result.scheme, result.netloc])