format.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. """Formatting operations."""
  2. from __future__ import annotations
  3. import inspect
  4. import json
  5. import os
  6. import re
  7. import sys
  8. from typing import TYPE_CHECKING, Any, Union
  9. from reflex import constants
  10. from reflex.utils import exceptions, serializers, types
  11. from reflex.utils.serializers import serialize
  12. from reflex.vars import BaseVar, Var
  13. if TYPE_CHECKING:
  14. from reflex.components.component import ComponentStyle
  15. from reflex.event import EventChain, EventHandler, EventSpec
  16. WRAP_MAP = {
  17. "{": "}",
  18. "(": ")",
  19. "[": "]",
  20. "<": ">",
  21. '"': '"',
  22. "'": "'",
  23. "`": "`",
  24. }
  25. def get_close_char(open: str, close: str | None = None) -> str:
  26. """Check if the given character is a valid brace.
  27. Args:
  28. open: The open character.
  29. close: The close character if provided.
  30. Returns:
  31. The close character.
  32. Raises:
  33. ValueError: If the open character is not a valid brace.
  34. """
  35. if close is not None:
  36. return close
  37. if open not in WRAP_MAP:
  38. raise ValueError(f"Invalid wrap open: {open}, must be one of {WRAP_MAP.keys()}")
  39. return WRAP_MAP[open]
  40. def is_wrapped(text: str, open: str, close: str | None = None) -> bool:
  41. """Check if the given text is wrapped in the given open and close characters.
  42. Args:
  43. text: The text to check.
  44. open: The open character.
  45. close: The close character.
  46. Returns:
  47. Whether the text is wrapped.
  48. """
  49. close = get_close_char(open, close)
  50. return text.startswith(open) and text.endswith(close)
  51. def wrap(
  52. text: str,
  53. open: str,
  54. close: str | None = None,
  55. check_first: bool = True,
  56. num: int = 1,
  57. ) -> str:
  58. """Wrap the given text in the given open and close characters.
  59. Args:
  60. text: The text to wrap.
  61. open: The open character.
  62. close: The close character.
  63. check_first: Whether to check if the text is already wrapped.
  64. num: The number of times to wrap the text.
  65. Returns:
  66. The wrapped text.
  67. """
  68. close = get_close_char(open, close)
  69. # If desired, check if the text is already wrapped in braces.
  70. if check_first and is_wrapped(text=text, open=open, close=close):
  71. return text
  72. # Wrap the text in braces.
  73. return f"{open * num}{text}{close * num}"
  74. def indent(text: str, indent_level: int = 2) -> str:
  75. """Indent the given text by the given indent level.
  76. Args:
  77. text: The text to indent.
  78. indent_level: The indent level.
  79. Returns:
  80. The indented text.
  81. """
  82. lines = text.splitlines()
  83. if len(lines) < 2:
  84. return text
  85. return os.linesep.join(f"{' ' * indent_level}{line}" for line in lines) + os.linesep
  86. def to_snake_case(text: str) -> str:
  87. """Convert a string to snake case.
  88. The words in the text are converted to lowercase and
  89. separated by underscores.
  90. Args:
  91. text: The string to convert.
  92. Returns:
  93. The snake case string.
  94. """
  95. s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
  96. return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_")
  97. def to_camel_case(text: str) -> str:
  98. """Convert a string to camel case.
  99. The first word in the text is converted to lowercase and
  100. the rest of the words are converted to title case, removing underscores.
  101. Args:
  102. text: The string to convert.
  103. Returns:
  104. The camel case string.
  105. """
  106. words = re.split("[_-]", text.lstrip("-_"))
  107. leading_underscores_or_hyphens = "".join(re.findall(r"^[_-]+", text))
  108. # Capitalize the first letter of each word except the first one
  109. converted_word = words[0] + "".join(x.capitalize() for x in words[1:])
  110. return leading_underscores_or_hyphens + converted_word
  111. def to_title_case(text: str) -> str:
  112. """Convert a string from snake case to title case.
  113. Args:
  114. text: The string to convert.
  115. Returns:
  116. The title case string.
  117. """
  118. return "".join(word.capitalize() for word in text.split("_"))
  119. def to_kebab_case(text: str) -> str:
  120. """Convert a string to kebab case.
  121. The words in the text are converted to lowercase and
  122. separated by hyphens.
  123. Args:
  124. text: The string to convert.
  125. Returns:
  126. The title case string.
  127. """
  128. return to_snake_case(text).replace("_", "-")
  129. def format_string(string: str) -> str:
  130. """Format the given string as a JS string literal..
  131. Args:
  132. string: The string to format.
  133. Returns:
  134. The formatted string.
  135. """
  136. # Escape backticks.
  137. string = string.replace(r"\`", "`")
  138. string = string.replace("`", r"\`")
  139. # Wrap the string so it looks like {`string`}.
  140. string = wrap(string, "`")
  141. string = wrap(string, "{")
  142. return string
  143. def format_var(var: Var) -> str:
  144. """Format the given Var as a javascript value.
  145. Args:
  146. var: The Var to format.
  147. Returns:
  148. The formatted Var.
  149. """
  150. if not var._var_is_local or var._var_is_string:
  151. return str(var)
  152. if types._issubclass(var._var_type, str):
  153. return format_string(var._var_full_name)
  154. if is_wrapped(var._var_full_name, "{"):
  155. return var._var_full_name
  156. return json_dumps(var._var_full_name)
  157. def format_route(route: str, format_case=True) -> str:
  158. """Format the given route.
  159. Args:
  160. route: The route to format.
  161. format_case: whether to format case to kebab case.
  162. Returns:
  163. The formatted route.
  164. """
  165. route = route.strip("/")
  166. # Strip the route and format casing.
  167. if format_case:
  168. route = to_kebab_case(route)
  169. # If the route is empty, return the index route.
  170. if route == "":
  171. return constants.PageNames.INDEX_ROUTE
  172. return route
  173. def format_cond(
  174. cond: str | Var,
  175. true_value: str | Var,
  176. false_value: str | Var = '""',
  177. is_prop=False,
  178. ) -> str:
  179. """Format a conditional expression.
  180. Args:
  181. cond: The cond.
  182. true_value: The value to return if the cond is true.
  183. false_value: The value to return if the cond is false.
  184. is_prop: Whether the cond is a prop
  185. Returns:
  186. The formatted conditional expression.
  187. """
  188. # Use Python truthiness.
  189. cond = f"isTrue({cond})"
  190. # Format prop conds.
  191. if is_prop:
  192. prop1 = Var.create_safe(
  193. true_value,
  194. _var_is_string=type(true_value) is str,
  195. )
  196. prop1._var_is_local = True
  197. prop2 = Var.create_safe(
  198. false_value,
  199. _var_is_string=type(false_value) is str,
  200. )
  201. prop2._var_is_local = True
  202. prop1, prop2 = str(prop1), str(prop2) # avoid f-string semantics for Var
  203. return f"{cond} ? {prop1} : {prop2}".replace("{", "").replace("}", "")
  204. # Format component conds.
  205. return wrap(f"{cond} ? {true_value} : {false_value}", "{")
  206. def format_prop(
  207. prop: Union[Var, EventChain, ComponentStyle, str],
  208. ) -> Union[int, float, str]:
  209. """Format a prop.
  210. Args:
  211. prop: The prop to format.
  212. Returns:
  213. The formatted prop to display within a tag.
  214. Raises:
  215. exceptions.InvalidStylePropError: If the style prop value is not a valid type.
  216. TypeError: If the prop is not valid.
  217. """
  218. # import here to avoid circular import.
  219. from reflex.event import EventChain
  220. try:
  221. # Handle var props.
  222. if isinstance(prop, Var):
  223. if not prop._var_is_local or prop._var_is_string:
  224. return str(prop)
  225. if types._issubclass(prop._var_type, str):
  226. return format_string(prop._var_full_name)
  227. prop = prop._var_full_name
  228. # Handle event props.
  229. elif isinstance(prop, EventChain):
  230. sig = inspect.signature(prop.args_spec) # type: ignore
  231. if sig.parameters:
  232. arg_def = ",".join(f"_{p}" for p in sig.parameters)
  233. arg_def = f"({arg_def})"
  234. else:
  235. # add a default argument for addEvents if none were specified in prop.args_spec
  236. # used to trigger the preventDefault() on the event.
  237. arg_def = "(_e)"
  238. chain = ",".join([format_event(event) for event in prop.events])
  239. event = f"addEvents([{chain}], {arg_def}, {json_dumps(prop.event_actions)})"
  240. prop = f"{arg_def} => {event}"
  241. # Handle other types.
  242. elif isinstance(prop, str):
  243. if is_wrapped(prop, "{"):
  244. return prop
  245. return json_dumps(prop)
  246. # For dictionaries, convert any properties to strings.
  247. elif isinstance(prop, dict):
  248. prop = serializers.serialize_dict(prop) # type: ignore
  249. else:
  250. # Dump the prop as JSON.
  251. prop = json_dumps(prop)
  252. except exceptions.InvalidStylePropError:
  253. raise
  254. except TypeError as e:
  255. raise TypeError(f"Could not format prop: {prop} of type {type(prop)}") from e
  256. # Wrap the variable in braces.
  257. assert isinstance(prop, str), "The prop must be a string."
  258. return wrap(prop, "{", check_first=False)
  259. def format_props(*single_props, **key_value_props) -> list[str]:
  260. """Format the tag's props.
  261. Args:
  262. single_props: Props that are not key-value pairs.
  263. key_value_props: Props that are key-value pairs.
  264. Returns:
  265. The formatted props list.
  266. """
  267. # Format all the props.
  268. return [
  269. f"{name}={format_prop(prop)}"
  270. for name, prop in sorted(key_value_props.items())
  271. if prop is not None
  272. ] + [str(prop) for prop in single_props]
  273. def get_event_handler_parts(handler: EventHandler) -> tuple[str, str]:
  274. """Get the state and function name of an event handler.
  275. Args:
  276. handler: The event handler to get the parts of.
  277. Returns:
  278. The state and function name.
  279. """
  280. # Get the class that defines the event handler.
  281. parts = handler.fn.__qualname__.split(".")
  282. # If there's no enclosing class, just return the function name.
  283. if len(parts) == 1:
  284. return ("", parts[-1])
  285. # Get the state and the function name.
  286. state_name, name = parts[-2:]
  287. # Construct the full event handler name.
  288. try:
  289. # Try to get the state from the module.
  290. state = vars(sys.modules[handler.fn.__module__])[state_name]
  291. except Exception:
  292. # If the state isn't in the module, just return the function name.
  293. return ("", to_snake_case(handler.fn.__qualname__))
  294. return (state.get_full_name(), name)
  295. def format_event_handler(handler: EventHandler) -> str:
  296. """Format an event handler.
  297. Args:
  298. handler: The event handler to format.
  299. Returns:
  300. The formatted function.
  301. """
  302. state, name = get_event_handler_parts(handler)
  303. if state == "":
  304. return name
  305. return f"{state}.{name}"
  306. def format_event(event_spec: EventSpec) -> str:
  307. """Format an event.
  308. Args:
  309. event_spec: The event to format.
  310. Returns:
  311. The compiled event.
  312. """
  313. args = ",".join(
  314. [
  315. ":".join(
  316. (
  317. name._var_name,
  318. wrap(json.dumps(val._var_name).strip('"').replace("`", "\\`"), "`")
  319. if val._var_is_string
  320. else val._var_full_name,
  321. )
  322. )
  323. for name, val in event_spec.args
  324. ]
  325. )
  326. event_args = [
  327. wrap(format_event_handler(event_spec.handler), '"'),
  328. ]
  329. event_args.append(wrap(args, "{"))
  330. if event_spec.client_handler_name:
  331. event_args.append(wrap(event_spec.client_handler_name, '"'))
  332. return f"Event({', '.join(event_args)})"
  333. def format_event_chain(
  334. event_chain: EventChain | Var[EventChain],
  335. event_arg: Var | None = None,
  336. ) -> str:
  337. """Format an event chain as a javascript invocation.
  338. Args:
  339. event_chain: The event chain to queue on the frontend.
  340. event_arg: The browser-native event (only used to preventDefault).
  341. Returns:
  342. Compiled javascript code to queue the given event chain on the frontend.
  343. Raises:
  344. ValueError: When the given event chain is not a valid event chain.
  345. """
  346. if isinstance(event_chain, Var):
  347. from reflex.event import EventChain
  348. if event_chain._var_type is not EventChain:
  349. raise ValueError(f"Invalid event chain: {event_chain}")
  350. return "".join(
  351. [
  352. "(() => {",
  353. format_var(event_chain),
  354. f"; preventDefault({format_var(event_arg)})" if event_arg else "",
  355. "})()",
  356. ]
  357. )
  358. chain = ",".join([format_event(event) for event in event_chain.events])
  359. return "".join(
  360. [
  361. f"addEvents([{chain}]",
  362. f", {format_var(event_arg)}" if event_arg else "",
  363. ")",
  364. ]
  365. )
  366. def format_query_params(router_data: dict[str, Any]) -> dict[str, str]:
  367. """Convert back query params name to python-friendly case.
  368. Args:
  369. router_data: the router_data dict containing the query params
  370. Returns:
  371. The reformatted query params
  372. """
  373. params = router_data[constants.RouteVar.QUERY]
  374. return {k.replace("-", "_"): v for k, v in params.items()}
  375. def format_state(value: Any) -> Any:
  376. """Recursively format values in the given state.
  377. Args:
  378. value: The state to format.
  379. Returns:
  380. The formatted state.
  381. Raises:
  382. TypeError: If the given value is not a valid state.
  383. """
  384. # Handle dicts.
  385. if isinstance(value, dict):
  386. return {k: format_state(v) for k, v in value.items()}
  387. # Handle lists, sets, typles.
  388. if isinstance(value, types.StateIterBases):
  389. return [format_state(v) for v in value]
  390. # Return state vars as is.
  391. if isinstance(value, types.StateBases):
  392. return value
  393. # Serialize the value.
  394. serialized = serialize(value)
  395. if serialized is not None:
  396. return serialized
  397. raise TypeError(f"No JSON serializer found for var {value} of type {type(value)}.")
  398. def format_state_name(state_name: str) -> str:
  399. """Format a state name, replacing dots with double underscore.
  400. This allows individual substates to be accessed independently as javascript vars
  401. without using dot notation.
  402. Args:
  403. state_name: The state name to format.
  404. Returns:
  405. The formatted state name.
  406. """
  407. return state_name.replace(".", "__")
  408. def format_ref(ref: str) -> str:
  409. """Format a ref.
  410. Args:
  411. ref: The ref to format.
  412. Returns:
  413. The formatted ref.
  414. """
  415. # Replace all non-word characters with underscores.
  416. clean_ref = re.sub(r"[^\w]+", "_", ref)
  417. return f"ref_{clean_ref}"
  418. def format_array_ref(refs: str, idx: Var | None) -> str:
  419. """Format a ref accessed by array.
  420. Args:
  421. refs : The ref array to access.
  422. idx : The index of the ref in the array.
  423. Returns:
  424. The formatted ref.
  425. """
  426. clean_ref = re.sub(r"[^\w]+", "_", refs)
  427. if idx is not None:
  428. idx._var_is_local = True
  429. return f"refs_{clean_ref}[{idx}]"
  430. return f"refs_{clean_ref}"
  431. def format_breadcrumbs(route: str) -> list[tuple[str, str]]:
  432. """Take a route and return a list of tuple for use in breadcrumb.
  433. Args:
  434. route: The route to transform.
  435. Returns:
  436. list[tuple[str, str]]: the list of tuples for the breadcrumb.
  437. """
  438. route_parts = route.lstrip("/").split("/")
  439. # create and return breadcrumbs
  440. return [
  441. (part, "/".join(["", *route_parts[: i + 1]]))
  442. for i, part in enumerate(route_parts)
  443. ]
  444. def format_library_name(library_fullname: str):
  445. """Format the name of a library.
  446. Args:
  447. library_fullname: The fullname of the library.
  448. Returns:
  449. The name without the @version if it was part of the name
  450. """
  451. lib, at, version = library_fullname.rpartition("@")
  452. if not lib:
  453. lib = at + version
  454. return lib
  455. def json_dumps(obj: Any) -> str:
  456. """Takes an object and returns a jsonified string.
  457. Args:
  458. obj: The object to be serialized.
  459. Returns:
  460. A string
  461. """
  462. return json.dumps(obj, ensure_ascii=False, default=serialize)
  463. def unwrap_vars(value: str) -> str:
  464. """Unwrap var values from a JSON string.
  465. For example, "{var}" will be unwrapped to "var".
  466. Args:
  467. value: The JSON string to unwrap.
  468. Returns:
  469. The unwrapped JSON string.
  470. """
  471. def unescape_double_quotes_in_var(m: re.Match) -> str:
  472. prefix = m.group(1) or ""
  473. # Since the outer quotes are removed, the inner escaped quotes must be unescaped.
  474. return prefix + re.sub('\\\\"', '"', m.group(2))
  475. # This substitution is necessary to unwrap var values.
  476. return (
  477. re.sub(
  478. pattern=r"""
  479. (?<!\\) # must NOT start with a backslash
  480. " # match opening double quote of JSON value
  481. (<reflex.Var>.*?</reflex.Var>)? # Optional encoded VarData (non-greedy)
  482. {(.*?)} # extract the value between curly braces (non-greedy)
  483. " # match must end with an unescaped double quote
  484. """,
  485. repl=unescape_double_quotes_in_var,
  486. string=value,
  487. flags=re.VERBOSE,
  488. )
  489. .replace('"`', "`")
  490. .replace('`"', "`")
  491. )
  492. def collect_form_dict_names(form_dict: dict[str, Any]) -> dict[str, Any]:
  493. """Collapse keys with consecutive suffixes into a single list value.
  494. Separators dash and underscore are removed, unless this would overwrite an existing key.
  495. Args:
  496. form_dict: The dict to collapse.
  497. Returns:
  498. The collapsed dict.
  499. """
  500. ending_digit_regex = re.compile(r"^(.*?)[_-]?(\d+)$")
  501. collapsed = {}
  502. for k in sorted(form_dict):
  503. m = ending_digit_regex.match(k)
  504. if m:
  505. collapsed.setdefault(m.group(1), []).append(form_dict[k])
  506. # collapsing never overwrites valid data from the form_dict
  507. collapsed.update(form_dict)
  508. return collapsed
  509. def format_data_editor_column(col: str | dict):
  510. """Format a given column into the proper format.
  511. Args:
  512. col: The column.
  513. Raises:
  514. ValueError: invalid type provided for column.
  515. Returns:
  516. The formatted column.
  517. """
  518. if isinstance(col, str):
  519. return {"title": col, "id": col.lower(), "type": "str"}
  520. if isinstance(col, (dict,)):
  521. if "id" not in col:
  522. col["id"] = col["title"].lower()
  523. if "type" not in col:
  524. col["type"] = "str"
  525. if "overlayIcon" not in col:
  526. col["overlayIcon"] = None
  527. return col
  528. if isinstance(col, BaseVar):
  529. return col
  530. raise ValueError(
  531. f"unexpected type ({(type(col).__name__)}: {col}) for column header in data_editor"
  532. )
  533. def format_data_editor_cell(cell: Any):
  534. """Format a given data into a renderable cell for data_editor.
  535. Args:
  536. cell: The data to format.
  537. Returns:
  538. The formatted cell.
  539. """
  540. return {"kind": Var.create(value="GridCellKind.Text"), "data": cell}