utils.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. """Common utility functions used in the compiler."""
  2. from __future__ import annotations
  3. import os
  4. from typing import Any, Type
  5. from urllib.parse import urlparse
  6. from pydantic.fields import ModelField
  7. from reflex import constants
  8. from reflex.components.base import (
  9. Body,
  10. ColorModeScript,
  11. Description,
  12. DocumentHead,
  13. Head,
  14. Html,
  15. Image,
  16. Main,
  17. Meta,
  18. NextScript,
  19. Title,
  20. )
  21. from reflex.components.component import Component, ComponentStyle, CustomComponent
  22. from reflex.state import Cookie, LocalStorage, State
  23. from reflex.style import Style
  24. from reflex.utils import format, imports, path_ops
  25. from reflex.vars import ImportVar
  26. # To re-export this function.
  27. merge_imports = imports.merge_imports
  28. def compile_import_statement(fields: set[ImportVar]) -> tuple[str, set[str]]:
  29. """Compile an import statement.
  30. Args:
  31. fields: The set of fields to import from the library.
  32. Returns:
  33. The libraries for default and rest.
  34. default: default library. When install "import def from library".
  35. rest: rest of libraries. When install "import {rest1, rest2} from library"
  36. """
  37. # ignore the ImportVar fields with render=False during compilation
  38. fields = {field for field in fields if field.render}
  39. # Check for default imports.
  40. defaults = {field for field in fields if field.is_default}
  41. assert len(defaults) < 2
  42. # Get the default import, and the specific imports.
  43. default = next(iter({field.name for field in defaults}), "")
  44. rest = {field.name for field in fields - defaults}
  45. return default, rest
  46. def validate_imports(imports: imports.ImportDict):
  47. """Verify that the same Tag is not used in multiple import.
  48. Args:
  49. imports: The dict of imports to validate
  50. Raises:
  51. ValueError: if a conflict on "tag/alias" is detected for an import.
  52. """
  53. used_tags = {}
  54. for lib, _imports in imports.items():
  55. for _import in _imports:
  56. import_name = (
  57. f"{_import.tag}/{_import.alias}" if _import.alias else _import.tag
  58. )
  59. if import_name in used_tags:
  60. raise ValueError(
  61. f"Can not compile, the tag {import_name} is used multiple time from {lib} and {used_tags[import_name]}"
  62. )
  63. if import_name is not None:
  64. used_tags[import_name] = lib
  65. def compile_imports(imports: imports.ImportDict) -> list[dict]:
  66. """Compile an import dict.
  67. Args:
  68. imports: The import dict to compile.
  69. Returns:
  70. The list of import dict.
  71. """
  72. import_dicts = []
  73. for lib, fields in imports.items():
  74. default, rest = compile_import_statement(fields)
  75. # prevent lib from being rendered on the page if all imports are non rendered kind
  76. if not any({f.render for f in fields}): # type: ignore
  77. continue
  78. if not lib:
  79. assert not default, "No default field allowed for empty library."
  80. assert rest is not None and len(rest) > 0, "No fields to import."
  81. for module in sorted(rest):
  82. import_dicts.append(get_import_dict(module))
  83. continue
  84. # remove the version before rendering the package imports
  85. lib = format.format_library_name(lib)
  86. import_dicts.append(get_import_dict(lib, default, rest))
  87. return import_dicts
  88. def get_import_dict(lib: str, default: str = "", rest: set[str] | None = None) -> dict:
  89. """Get dictionary for import template.
  90. Args:
  91. lib: The importing react library.
  92. default: The default module to import.
  93. rest: The rest module to import.
  94. Returns:
  95. A dictionary for import template.
  96. """
  97. return {
  98. "lib": lib,
  99. "default": default,
  100. "rest": rest if rest else set(),
  101. }
  102. def compile_state(state: Type[State]) -> dict:
  103. """Compile the state of the app.
  104. Args:
  105. state: The app state object.
  106. Returns:
  107. A dictionary of the compiled state.
  108. """
  109. try:
  110. initial_state = state().dict()
  111. except Exception:
  112. initial_state = state().dict(include_computed=False)
  113. return format.format_state(initial_state)
  114. def _compile_client_storage_field(
  115. field: ModelField,
  116. ) -> tuple[Type[Cookie] | Type[LocalStorage] | None, dict[str, Any] | None]:
  117. """Compile the given cookie or local_storage field.
  118. Args:
  119. field: The possible cookie field to compile.
  120. Returns:
  121. A dictionary of the compiled cookie or None if the field is not cookie-like.
  122. """
  123. for field_type in (Cookie, LocalStorage):
  124. if isinstance(field.default, field_type):
  125. cs_obj = field.default
  126. elif isinstance(field.type_, type) and issubclass(field.type_, field_type):
  127. cs_obj = field.type_()
  128. else:
  129. continue
  130. return field_type, cs_obj.options()
  131. return None, None
  132. def _compile_client_storage_recursive(
  133. state: Type[State],
  134. ) -> tuple[dict[str, dict], dict[str, dict[str, str]]]:
  135. """Compile the client-side storage for the given state recursively.
  136. Args:
  137. state: The app state object.
  138. Returns:
  139. A tuple of the compiled client-side storage info:
  140. (
  141. cookies: dict[str, dict],
  142. local_storage: dict[str, dict[str, str]]
  143. )
  144. """
  145. cookies = {}
  146. local_storage = {}
  147. state_name = state.get_full_name()
  148. for name, field in state.__fields__.items():
  149. if name in state.inherited_vars:
  150. # only include vars defined in this state
  151. continue
  152. state_key = f"{state_name}.{name}"
  153. field_type, options = _compile_client_storage_field(field)
  154. if field_type is Cookie:
  155. cookies[state_key] = options
  156. elif field_type is LocalStorage:
  157. local_storage[state_key] = options
  158. else:
  159. continue
  160. for substate in state.get_substates():
  161. substate_cookies, substate_local_storage = _compile_client_storage_recursive(
  162. substate
  163. )
  164. cookies.update(substate_cookies)
  165. local_storage.update(substate_local_storage)
  166. return cookies, local_storage
  167. def compile_client_storage(state: Type[State]) -> dict[str, dict]:
  168. """Compile the client-side storage for the given state.
  169. Args:
  170. state: The app state object.
  171. Returns:
  172. A dictionary of the compiled client-side storage info.
  173. """
  174. cookies, local_storage = _compile_client_storage_recursive(state)
  175. return {
  176. constants.COOKIES: cookies,
  177. constants.LOCAL_STORAGE: local_storage,
  178. }
  179. def compile_custom_component(
  180. component: CustomComponent,
  181. ) -> tuple[dict, imports.ImportDict]:
  182. """Compile a custom component.
  183. Args:
  184. component: The custom component to compile.
  185. Returns:
  186. A tuple of the compiled component and the imports required by the component.
  187. """
  188. # Render the component.
  189. render = component.get_component()
  190. # Get the imports.
  191. imports = {
  192. lib: fields
  193. for lib, fields in render.get_imports().items()
  194. if lib != component.library
  195. }
  196. # Concatenate the props.
  197. props = [prop.name for prop in component.get_prop_vars()]
  198. # Compile the component.
  199. return (
  200. {
  201. "name": component.tag,
  202. "props": props,
  203. "render": render.render(),
  204. },
  205. imports,
  206. )
  207. def create_document_root(head_components: list[Component] | None = None) -> Component:
  208. """Create the document root.
  209. Args:
  210. head_components: The components to add to the head.
  211. Returns:
  212. The document root.
  213. """
  214. head_components = head_components or []
  215. return Html.create(
  216. DocumentHead.create(*head_components),
  217. Body.create(
  218. ColorModeScript.create(),
  219. Main.create(),
  220. NextScript.create(),
  221. ),
  222. )
  223. def create_theme(style: ComponentStyle) -> dict:
  224. """Create the base style for the app.
  225. Args:
  226. style: The style dict for the app.
  227. Returns:
  228. The base style for the app.
  229. """
  230. # Get the global style from the style dict.
  231. global_style = Style({k: v for k, v in style.items() if not isinstance(k, type)})
  232. # Root styles.
  233. root_style = Style({k: v for k, v in global_style.items() if k.startswith("::")})
  234. # Body styles.
  235. root_style["body"] = Style(
  236. {k: v for k, v in global_style.items() if k not in root_style}
  237. )
  238. # Return the theme.
  239. return {
  240. "styles": {"global": root_style},
  241. }
  242. def get_page_path(path: str) -> str:
  243. """Get the path of the compiled JS file for the given page.
  244. Args:
  245. path: The path of the page.
  246. Returns:
  247. The path of the compiled JS file.
  248. """
  249. return os.path.join(constants.Dirs.WEB_PAGES, path + constants.Ext.JS)
  250. def get_theme_path() -> str:
  251. """Get the path of the base theme style.
  252. Returns:
  253. The path of the theme style.
  254. """
  255. return os.path.join(
  256. constants.Dirs.WEB_UTILS, constants.PageNames.THEME + constants.Ext.JS
  257. )
  258. def get_root_stylesheet_path() -> str:
  259. """Get the path of the app root file.
  260. Returns:
  261. The path of the app root file.
  262. """
  263. return os.path.join(
  264. constants.STYLES_DIR, constants.PageNames.STYLESHEET_ROOT + constants.Ext.CSS
  265. )
  266. def get_context_path() -> str:
  267. """Get the path of the context / initial state file.
  268. Returns:
  269. The path of the context module.
  270. """
  271. return os.path.join(constants.Dirs.WEB_UTILS, "context" + constants.Ext.JS)
  272. def get_components_path() -> str:
  273. """Get the path of the compiled components.
  274. Returns:
  275. The path of the compiled components.
  276. """
  277. return os.path.join(constants.Dirs.WEB_UTILS, "components" + constants.Ext.JS)
  278. def get_asset_path(filename: str | None = None) -> str:
  279. """Get the path for an asset.
  280. Args:
  281. filename: If given, is added to the root path of assets dir.
  282. Returns:
  283. The path of the asset.
  284. """
  285. if filename is None:
  286. return constants.Dirs.WEB_ASSETS
  287. else:
  288. return os.path.join(constants.Dirs.WEB_ASSETS, filename)
  289. def add_meta(
  290. page: Component, title: str, image: str, description: str, meta: list[dict]
  291. ) -> Component:
  292. """Add metadata to a page.
  293. Args:
  294. page: The component for the page.
  295. title: The title of the page.
  296. image: The image for the page.
  297. description: The description of the page.
  298. meta: The metadata list.
  299. Returns:
  300. The component with the metadata added.
  301. """
  302. meta_tags = [Meta.create(**item) for item in meta]
  303. page.children.append(
  304. Head.create(
  305. Title.create(title),
  306. Description.create(content=description),
  307. Image.create(content=image),
  308. *meta_tags,
  309. )
  310. )
  311. return page
  312. def write_page(path: str, code: str):
  313. """Write the given code to the given path.
  314. Args:
  315. path: The path to write the code to.
  316. code: The code to write.
  317. """
  318. path_ops.mkdir(os.path.dirname(path))
  319. with open(path, "w", encoding="utf-8") as f:
  320. f.write(code)
  321. def empty_dir(path: str, keep_files: list[str] | None = None):
  322. """Remove all files and folders in a directory except for the keep_files.
  323. Args:
  324. path: The path to the directory that will be emptied
  325. keep_files: List of filenames or foldernames that will not be deleted.
  326. """
  327. # If the directory does not exist, return.
  328. if not os.path.exists(path):
  329. return
  330. # Remove all files and folders in the directory.
  331. keep_files = keep_files or []
  332. directory_contents = os.listdir(path)
  333. for element in directory_contents:
  334. if element not in keep_files:
  335. path_ops.rm(os.path.join(path, element))
  336. def is_valid_url(url) -> bool:
  337. """Check if a url is valid.
  338. Args:
  339. url: The Url to check.
  340. Returns:
  341. Whether url is valid.
  342. """
  343. result = urlparse(url)
  344. return all([result.scheme, result.netloc])