route.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Route constants."""
  2. import re
  3. from types import SimpleNamespace
  4. class RouteArgType(SimpleNamespace):
  5. """Type of dynamic route arg extracted from URI route."""
  6. # Typecast to str is needed for Enum to work.
  7. SINGLE = str("arg_single")
  8. LIST = str("arg_list")
  9. # the name of the backend var containing path and client information
  10. ROUTER = "router"
  11. ROUTER_DATA = "router_data"
  12. class RouteVar(SimpleNamespace):
  13. """Names of variables used in the router_data dict stored in State."""
  14. CLIENT_IP = "ip"
  15. CLIENT_TOKEN = "token"
  16. HEADERS = "headers"
  17. PATH = "pathname"
  18. ORIGIN = "asPath"
  19. SESSION_ID = "sid"
  20. QUERY = "query"
  21. COOKIE = "cookie"
  22. # This subset of router_data is included in chained on_load events.
  23. ROUTER_DATA_INCLUDE = set((RouteVar.PATH, RouteVar.ORIGIN, RouteVar.QUERY))
  24. class RouteRegex(SimpleNamespace):
  25. """Regex used for extracting route args in route."""
  26. ARG = re.compile(r"\[(?!\.)([^\[\]]+)\]")
  27. # group return the catchall pattern (i.e. "[[..slug]]")
  28. CATCHALL = re.compile(r"(\[?\[\.{3}(?![0-9]).*\]?\])")
  29. # group return the arg name (i.e. "slug")
  30. STRICT_CATCHALL = re.compile(r"\[\.{3}([a-zA-Z_][\w]*)\]")
  31. # group return the arg name (i.e. "slug") (optional arg can be empty)
  32. OPT_CATCHALL = re.compile(r"\[\[\.{3}([a-zA-Z_][\w]*)\]\]")
  33. SINGLE_SEGMENT = "__SINGLE_SEGMENT__"
  34. DOUBLE_SEGMENT = "__DOUBLE_SEGMENT__"
  35. SINGLE_CATCHALL_SEGMENT = "__SINGLE_CATCHALL_SEGMENT__"
  36. DOUBLE_CATCHALL_SEGMENT = "__DOUBLE_CATCHALL_SEGMENT__"
  37. class DefaultPage(SimpleNamespace):
  38. """Default page constants."""
  39. # The default title to show for Reflex apps.
  40. TITLE = "{} | {}"
  41. # The default description to show for Reflex apps.
  42. DESCRIPTION = ""
  43. # The default image to show for Reflex apps.
  44. IMAGE = "favicon.ico"
  45. # The default meta list to show for Reflex apps.
  46. META_LIST = []
  47. # 404 variables
  48. class Page404(SimpleNamespace):
  49. """Page 404 constants."""
  50. SLUG = "404"
  51. TITLE = "404 - Not Found"
  52. IMAGE = "favicon.ico"
  53. DESCRIPTION = "The page was not found"
  54. ROUTE_NOT_FOUND = "routeNotFound"