format.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. """Formatting operations."""
  2. from __future__ import annotations
  3. import dataclasses
  4. import inspect
  5. import json
  6. import os
  7. import re
  8. from typing import TYPE_CHECKING, Any, Callable, List, Optional, Union
  9. from reflex import constants
  10. from reflex.utils import exceptions, types
  11. if TYPE_CHECKING:
  12. from reflex.components.component import ComponentStyle
  13. from reflex.event import ArgsSpec, EventChain, EventHandler, EventSpec
  14. WRAP_MAP = {
  15. "{": "}",
  16. "(": ")",
  17. "[": "]",
  18. "<": ">",
  19. '"': '"',
  20. "'": "'",
  21. "`": "`",
  22. }
  23. def get_close_char(open: str, close: str | None = None) -> str:
  24. """Check if the given character is a valid brace.
  25. Args:
  26. open: The open character.
  27. close: The close character if provided.
  28. Returns:
  29. The close character.
  30. Raises:
  31. ValueError: If the open character is not a valid brace.
  32. """
  33. if close is not None:
  34. return close
  35. if open not in WRAP_MAP:
  36. raise ValueError(f"Invalid wrap open: {open}, must be one of {WRAP_MAP.keys()}")
  37. return WRAP_MAP[open]
  38. def is_wrapped(text: str, open: str, close: str | None = None) -> bool:
  39. """Check if the given text is wrapped in the given open and close characters.
  40. "(a) + (b)" --> False
  41. "((abc))" --> True
  42. "(abc)" --> True
  43. Args:
  44. text: The text to check.
  45. open: The open character.
  46. close: The close character.
  47. Returns:
  48. Whether the text is wrapped.
  49. """
  50. close = get_close_char(open, close)
  51. if not (text.startswith(open) and text.endswith(close)):
  52. return False
  53. depth = 0
  54. for ch in text[:-1]:
  55. if ch == open:
  56. depth += 1
  57. if ch == close:
  58. depth -= 1
  59. if depth == 0: # it shouldn't close before the end
  60. return False
  61. return True
  62. def wrap(
  63. text: str,
  64. open: str,
  65. close: str | None = None,
  66. check_first: bool = True,
  67. num: int = 1,
  68. ) -> str:
  69. """Wrap the given text in the given open and close characters.
  70. Args:
  71. text: The text to wrap.
  72. open: The open character.
  73. close: The close character.
  74. check_first: Whether to check if the text is already wrapped.
  75. num: The number of times to wrap the text.
  76. Returns:
  77. The wrapped text.
  78. """
  79. close = get_close_char(open, close)
  80. # If desired, check if the text is already wrapped in braces.
  81. if check_first and is_wrapped(text=text, open=open, close=close):
  82. return text
  83. # Wrap the text in braces.
  84. return f"{open * num}{text}{close * num}"
  85. def indent(text: str, indent_level: int = 2) -> str:
  86. """Indent the given text by the given indent level.
  87. Args:
  88. text: The text to indent.
  89. indent_level: The indent level.
  90. Returns:
  91. The indented text.
  92. """
  93. lines = text.splitlines()
  94. if len(lines) < 2:
  95. return text
  96. return os.linesep.join(f"{' ' * indent_level}{line}" for line in lines) + os.linesep
  97. def to_snake_case(text: str) -> str:
  98. """Convert a string to snake case.
  99. The words in the text are converted to lowercase and
  100. separated by underscores.
  101. Args:
  102. text: The string to convert.
  103. Returns:
  104. The snake case string.
  105. """
  106. s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
  107. return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_")
  108. def to_camel_case(text: str, allow_hyphens: bool = False) -> str:
  109. """Convert a string to camel case.
  110. The first word in the text is converted to lowercase and
  111. the rest of the words are converted to title case, removing underscores.
  112. Args:
  113. text: The string to convert.
  114. allow_hyphens: Whether to allow hyphens in the string.
  115. Returns:
  116. The camel case string.
  117. """
  118. char = "_" if allow_hyphens else "-_"
  119. words = re.split(f"[{char}]", text.lstrip(char))
  120. leading_underscores_or_hyphens = "".join(re.findall(rf"^[{char}]+", text))
  121. # Capitalize the first letter of each word except the first one
  122. converted_word = words[0] + "".join(x.capitalize() for x in words[1:])
  123. return leading_underscores_or_hyphens + converted_word
  124. def to_title_case(text: str, sep: str = "") -> str:
  125. """Convert a string from snake case to title case.
  126. Args:
  127. text: The string to convert.
  128. sep: The separator to use to join the words.
  129. Returns:
  130. The title case string.
  131. """
  132. return sep.join(word.title() for word in text.split("_"))
  133. def to_kebab_case(text: str) -> str:
  134. """Convert a string to kebab case.
  135. The words in the text are converted to lowercase and
  136. separated by hyphens.
  137. Args:
  138. text: The string to convert.
  139. Returns:
  140. The title case string.
  141. """
  142. return to_snake_case(text).replace("_", "-")
  143. def make_default_page_title(app_name: str, route: str) -> str:
  144. """Make a default page title from a route.
  145. Args:
  146. app_name: The name of the app owning the page.
  147. route: The route to make the title from.
  148. Returns:
  149. The default page title.
  150. """
  151. title = constants.DefaultPage.TITLE.format(app_name, route)
  152. return to_title_case(title, " ")
  153. def _escape_js_string(string: str) -> str:
  154. """Escape the string for use as a JS string literal.
  155. Args:
  156. string: The string to escape.
  157. Returns:
  158. The escaped string.
  159. """
  160. # TODO: we may need to re-vist this logic after new Var API is implemented.
  161. def escape_outside_segments(segment):
  162. """Escape backticks in segments outside of `${}`.
  163. Args:
  164. segment: The part of the string to escape.
  165. Returns:
  166. The escaped or unescaped segment.
  167. """
  168. if segment.startswith("${") and segment.endswith("}"):
  169. # Return the `${}` segment unchanged
  170. return segment
  171. else:
  172. # Escape backticks in the segment
  173. segment = segment.replace(r"\`", "`")
  174. segment = segment.replace("`", r"\`")
  175. return segment
  176. # Split the string into parts, keeping the `${}` segments
  177. parts = re.split(r"(\$\{.*?\})", string)
  178. escaped_parts = [escape_outside_segments(part) for part in parts]
  179. escaped_string = "".join(escaped_parts)
  180. return escaped_string
  181. def _wrap_js_string(string: str) -> str:
  182. """Wrap string so it looks like {`string`}.
  183. Args:
  184. string: The string to wrap.
  185. Returns:
  186. The wrapped string.
  187. """
  188. string = wrap(string, "`")
  189. string = wrap(string, "{")
  190. return string
  191. def format_string(string: str) -> str:
  192. """Format the given string as a JS string literal..
  193. Args:
  194. string: The string to format.
  195. Returns:
  196. The formatted string.
  197. """
  198. return _wrap_js_string(_escape_js_string(string))
  199. def format_var(var: Var) -> str:
  200. """Format the given Var as a javascript value.
  201. Args:
  202. var: The Var to format.
  203. Returns:
  204. The formatted Var.
  205. """
  206. return str(var)
  207. def format_route(route: str, format_case=True) -> str:
  208. """Format the given route.
  209. Args:
  210. route: The route to format.
  211. format_case: whether to format case to kebab case.
  212. Returns:
  213. The formatted route.
  214. """
  215. route = route.strip("/")
  216. # Strip the route and format casing.
  217. if format_case:
  218. route = to_kebab_case(route)
  219. # If the route is empty, return the index route.
  220. if route == "":
  221. return constants.PageNames.INDEX_ROUTE
  222. return route
  223. def format_match(
  224. cond: str | Var,
  225. match_cases: List[List[Var]],
  226. default: Var,
  227. ) -> str:
  228. """Format a match expression whose return type is a Var.
  229. Args:
  230. cond: The condition.
  231. match_cases: The list of cases to match.
  232. default: The default case.
  233. Returns:
  234. The formatted match expression
  235. """
  236. switch_code = f"(() => {{ switch (JSON.stringify({cond})) {{"
  237. for case in match_cases:
  238. conditions = case[:-1]
  239. return_value = case[-1]
  240. case_conditions = " ".join(
  241. [f"case JSON.stringify({str(condition)}):" for condition in conditions]
  242. )
  243. case_code = f"{case_conditions} return ({str(return_value)}); break;"
  244. switch_code += case_code
  245. switch_code += f"default: return ({str(default)}); break;"
  246. switch_code += "};})()"
  247. return switch_code
  248. def format_prop(
  249. prop: Union[Var, EventChain, ComponentStyle, str],
  250. ) -> Union[int, float, str]:
  251. """Format a prop.
  252. Args:
  253. prop: The prop to format.
  254. Returns:
  255. The formatted prop to display within a tag.
  256. Raises:
  257. exceptions.InvalidStylePropError: If the style prop value is not a valid type.
  258. TypeError: If the prop is not valid.
  259. """
  260. # import here to avoid circular import.
  261. from reflex.event import EventChain
  262. from reflex.utils import serializers
  263. from reflex.vars import Var
  264. try:
  265. # Handle var props.
  266. if isinstance(prop, Var):
  267. return str(prop)
  268. # Handle event props.
  269. if isinstance(prop, EventChain):
  270. sig = inspect.signature(prop.args_spec) # type: ignore
  271. if sig.parameters:
  272. arg_def = ",".join(f"_{p}" for p in sig.parameters)
  273. arg_def_expr = f"[{arg_def}]"
  274. else:
  275. # add a default argument for addEvents if none were specified in prop.args_spec
  276. # used to trigger the preventDefault() on the event.
  277. arg_def = "...args"
  278. arg_def_expr = "args"
  279. chain = ",".join([format_event(event) for event in prop.events])
  280. event = f"addEvents([{chain}], {arg_def_expr}, {json_dumps(prop.event_actions)})"
  281. prop = f"({arg_def}) => {event}"
  282. # Handle other types.
  283. elif isinstance(prop, str):
  284. if is_wrapped(prop, "{"):
  285. return prop
  286. return json_dumps(prop)
  287. # For dictionaries, convert any properties to strings.
  288. elif isinstance(prop, dict):
  289. prop = serializers.serialize_dict(prop) # type: ignore
  290. else:
  291. # Dump the prop as JSON.
  292. prop = json_dumps(prop)
  293. except exceptions.InvalidStylePropError:
  294. raise
  295. except TypeError as e:
  296. raise TypeError(f"Could not format prop: {prop} of type {type(prop)}") from e
  297. # Wrap the variable in braces.
  298. assert isinstance(prop, str), "The prop must be a string."
  299. return wrap(prop, "{", check_first=False)
  300. def format_props(*single_props, **key_value_props) -> list[str]:
  301. """Format the tag's props.
  302. Args:
  303. single_props: Props that are not key-value pairs.
  304. key_value_props: Props that are key-value pairs.
  305. Returns:
  306. The formatted props list.
  307. """
  308. # Format all the props.
  309. from reflex.vars.base import LiteralVar, Var
  310. return [
  311. (
  312. f"{name}={format_prop(prop)}"
  313. if isinstance(prop, Var) and not isinstance(prop, Var)
  314. else (
  315. f"{name}={{{format_prop(prop if isinstance(prop, Var) else LiteralVar.create(prop))}}}"
  316. )
  317. )
  318. for name, prop in sorted(key_value_props.items())
  319. if prop is not None
  320. ] + [
  321. (
  322. str(prop)
  323. if isinstance(prop, Var) and not isinstance(prop, Var)
  324. else f"{str(LiteralVar.create(prop))}"
  325. )
  326. for prop in single_props
  327. ]
  328. def get_event_handler_parts(handler: EventHandler) -> tuple[str, str]:
  329. """Get the state and function name of an event handler.
  330. Args:
  331. handler: The event handler to get the parts of.
  332. Returns:
  333. The state and function name.
  334. """
  335. # Get the class that defines the event handler.
  336. parts = handler.fn.__qualname__.split(".")
  337. # Get the state full name
  338. state_full_name = handler.state_full_name
  339. # If there's no enclosing class, just return the function name.
  340. if not state_full_name:
  341. return ("", parts[-1])
  342. # Get the function name
  343. name = parts[-1]
  344. from reflex.state import State
  345. if state_full_name == "state" and name not in State.__dict__:
  346. return ("", to_snake_case(handler.fn.__qualname__))
  347. return (state_full_name, name)
  348. def format_event_handler(handler: EventHandler) -> str:
  349. """Format an event handler.
  350. Args:
  351. handler: The event handler to format.
  352. Returns:
  353. The formatted function.
  354. """
  355. state, name = get_event_handler_parts(handler)
  356. if state == "":
  357. return name
  358. return f"{state}.{name}"
  359. def format_event(event_spec: EventSpec) -> str:
  360. """Format an event.
  361. Args:
  362. event_spec: The event to format.
  363. Returns:
  364. The compiled event.
  365. """
  366. args = ",".join(
  367. [
  368. ":".join(
  369. (
  370. name._js_expr,
  371. (
  372. wrap(
  373. json.dumps(val._js_expr).strip('"').replace("`", "\\`"),
  374. "`",
  375. )
  376. if val._var_is_string
  377. else str(val)
  378. ),
  379. )
  380. )
  381. for name, val in event_spec.args
  382. ]
  383. )
  384. event_args = [
  385. wrap(format_event_handler(event_spec.handler), '"'),
  386. ]
  387. event_args.append(wrap(args, "{"))
  388. if event_spec.client_handler_name:
  389. event_args.append(wrap(event_spec.client_handler_name, '"'))
  390. return f"Event({', '.join(event_args)})"
  391. if TYPE_CHECKING:
  392. from reflex.vars import Var
  393. def format_queue_events(
  394. events: (
  395. EventSpec
  396. | EventHandler
  397. | Callable
  398. | List[EventSpec | EventHandler | Callable]
  399. | None
  400. ) = None,
  401. args_spec: Optional[ArgsSpec] = None,
  402. ) -> Var[EventChain]:
  403. """Format a list of event handler / event spec as a javascript callback.
  404. The resulting code can be passed to interfaces that expect a callback
  405. function and when triggered it will directly call queueEvents.
  406. It is intended to be executed in the rx.call_script context, where some
  407. existing API needs a callback to trigger a backend event handler.
  408. Args:
  409. events: The events to queue.
  410. args_spec: The argument spec for the callback.
  411. Returns:
  412. The compiled javascript callback to queue the given events on the frontend.
  413. Raises:
  414. ValueError: If a lambda function is given which returns a Var.
  415. """
  416. from reflex.event import (
  417. EventChain,
  418. EventHandler,
  419. EventSpec,
  420. call_event_fn,
  421. call_event_handler,
  422. )
  423. from reflex.vars import FunctionVar, Var
  424. if not events:
  425. return Var("(() => null)").to(FunctionVar, EventChain) # type: ignore
  426. # If no spec is provided, the function will take no arguments.
  427. def _default_args_spec():
  428. return []
  429. # Construct the arguments that the function accepts.
  430. sig = inspect.signature(args_spec or _default_args_spec) # type: ignore
  431. if sig.parameters:
  432. arg_def = ",".join(f"_{p}" for p in sig.parameters)
  433. arg_def = f"({arg_def})"
  434. else:
  435. arg_def = "()"
  436. payloads = []
  437. if not isinstance(events, list):
  438. events = [events]
  439. # Process each event/spec/lambda (similar to Component._create_event_chain).
  440. for spec in events:
  441. specs: list[EventSpec] = []
  442. if isinstance(spec, (EventHandler, EventSpec)):
  443. specs = [call_event_handler(spec, args_spec or _default_args_spec)]
  444. elif isinstance(spec, type(lambda: None)):
  445. specs = call_event_fn(spec, args_spec or _default_args_spec) # type: ignore
  446. if isinstance(specs, Var):
  447. raise ValueError(
  448. f"Invalid event spec: {specs}. Expected a list of EventSpecs."
  449. )
  450. payloads.extend(format_event(s) for s in specs)
  451. # Return the final code snippet, expecting queueEvents, processEvent, and socket to be in scope.
  452. # Typically this snippet will _only_ run from within an rx.call_script eval context.
  453. return Var(
  454. f"{arg_def} => {{queueEvents([{','.join(payloads)}], {constants.CompileVars.SOCKET}); "
  455. f"processEvent({constants.CompileVars.SOCKET})}}",
  456. ).to(FunctionVar, EventChain) # type: ignore
  457. def format_query_params(router_data: dict[str, Any]) -> dict[str, str]:
  458. """Convert back query params name to python-friendly case.
  459. Args:
  460. router_data: the router_data dict containing the query params
  461. Returns:
  462. The reformatted query params
  463. """
  464. params = router_data[constants.RouteVar.QUERY]
  465. return {k.replace("-", "_"): v for k, v in params.items()}
  466. def format_state(value: Any, key: Optional[str] = None) -> Any:
  467. """Recursively format values in the given state.
  468. Args:
  469. value: The state to format.
  470. key: The key associated with the value (optional).
  471. Returns:
  472. The formatted state.
  473. Raises:
  474. TypeError: If the given value is not a valid state.
  475. """
  476. from reflex.utils import serializers
  477. # Handle dicts.
  478. if isinstance(value, dict):
  479. return {k: format_state(v, k) for k, v in value.items()}
  480. # Hand dataclasses.
  481. if dataclasses.is_dataclass(value):
  482. if isinstance(value, type):
  483. raise TypeError(
  484. f"Cannot format state of type {type(value)}. Please provide an instance of the dataclass."
  485. )
  486. return {k: format_state(v, k) for k, v in dataclasses.asdict(value).items()}
  487. # Handle lists, sets, typles.
  488. if isinstance(value, types.StateIterBases):
  489. return [format_state(v) for v in value]
  490. # Return state vars as is.
  491. if isinstance(value, types.StateBases):
  492. return value
  493. # Serialize the value.
  494. serialized = serializers.serialize(value)
  495. if serialized is not None:
  496. return serialized
  497. if key is None:
  498. raise TypeError(
  499. f"No JSON serializer found for var {value} of type {type(value)}."
  500. )
  501. else:
  502. raise TypeError(
  503. f"No JSON serializer found for State Var '{key}' of value {value} of type {type(value)}."
  504. )
  505. def format_state_name(state_name: str) -> str:
  506. """Format a state name, replacing dots with double underscore.
  507. This allows individual substates to be accessed independently as javascript vars
  508. without using dot notation.
  509. Args:
  510. state_name: The state name to format.
  511. Returns:
  512. The formatted state name.
  513. """
  514. return state_name.replace(".", "__")
  515. def format_ref(ref: str) -> str:
  516. """Format a ref.
  517. Args:
  518. ref: The ref to format.
  519. Returns:
  520. The formatted ref.
  521. """
  522. # Replace all non-word characters with underscores.
  523. clean_ref = re.sub(r"[^\w]+", "_", ref)
  524. return f"ref_{clean_ref}"
  525. def format_library_name(library_fullname: str):
  526. """Format the name of a library.
  527. Args:
  528. library_fullname: The fullname of the library.
  529. Returns:
  530. The name without the @version if it was part of the name
  531. """
  532. lib, at, version = library_fullname.rpartition("@")
  533. if not lib:
  534. lib = at + version
  535. return lib
  536. def json_dumps(obj: Any) -> str:
  537. """Takes an object and returns a jsonified string.
  538. Args:
  539. obj: The object to be serialized.
  540. Returns:
  541. A string
  542. """
  543. from reflex.utils import serializers
  544. return json.dumps(obj, ensure_ascii=False, default=serializers.serialize)
  545. def collect_form_dict_names(form_dict: dict[str, Any]) -> dict[str, Any]:
  546. """Collapse keys with consecutive suffixes into a single list value.
  547. Separators dash and underscore are removed, unless this would overwrite an existing key.
  548. Args:
  549. form_dict: The dict to collapse.
  550. Returns:
  551. The collapsed dict.
  552. """
  553. ending_digit_regex = re.compile(r"^(.*?)[_-]?(\d+)$")
  554. collapsed = {}
  555. for k in sorted(form_dict):
  556. m = ending_digit_regex.match(k)
  557. if m:
  558. collapsed.setdefault(m.group(1), []).append(form_dict[k])
  559. # collapsing never overwrites valid data from the form_dict
  560. collapsed.update(form_dict)
  561. return collapsed
  562. def format_array_ref(refs: str, idx: Var | None) -> str:
  563. """Format a ref accessed by array.
  564. Args:
  565. refs : The ref array to access.
  566. idx : The index of the ref in the array.
  567. Returns:
  568. The formatted ref.
  569. """
  570. clean_ref = re.sub(r"[^\w]+", "_", refs)
  571. if idx is not None:
  572. # idx._var_is_local = True
  573. return f"refs_{clean_ref}[{str(idx)}]"
  574. return f"refs_{clean_ref}"
  575. def format_data_editor_column(col: str | dict):
  576. """Format a given column into the proper format.
  577. Args:
  578. col: The column.
  579. Raises:
  580. ValueError: invalid type provided for column.
  581. Returns:
  582. The formatted column.
  583. """
  584. from reflex.vars import Var
  585. if isinstance(col, str):
  586. return {"title": col, "id": col.lower(), "type": "str"}
  587. if isinstance(col, (dict,)):
  588. if "id" not in col:
  589. col["id"] = col["title"].lower()
  590. if "type" not in col:
  591. col["type"] = "str"
  592. if "overlayIcon" not in col:
  593. col["overlayIcon"] = None
  594. return col
  595. if isinstance(col, Var):
  596. return col
  597. raise ValueError(
  598. f"unexpected type ({(type(col).__name__)}: {col}) for column header in data_editor"
  599. )
  600. def format_data_editor_cell(cell: Any):
  601. """Format a given data into a renderable cell for data_editor.
  602. Args:
  603. cell: The data to format.
  604. Returns:
  605. The formatted cell.
  606. """
  607. from reflex.vars.base import Var
  608. return {
  609. "kind": Var(_js_expr="GridCellKind.Text"),
  610. "data": cell,
  611. }