route.py 2.0 KB

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