format.py 20 KB

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