base.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. """Base file for constants that don't fit any other categories."""
  2. from __future__ import annotations
  3. import os
  4. import platform
  5. from enum import Enum
  6. from importlib import metadata
  7. from types import SimpleNamespace
  8. from platformdirs import PlatformDirs
  9. IS_WINDOWS = platform.system() == "Windows"
  10. class Dirs(SimpleNamespace):
  11. """Various directories/paths used by Reflex."""
  12. # The frontend directories in a project.
  13. # The web folder where the NextJS app is compiled to.
  14. WEB = ".web"
  15. # The name of the assets directory.
  16. APP_ASSETS = "assets"
  17. # The name of the assets directory for external ressource (a subfolder of APP_ASSETS).
  18. EXTERNAL_APP_ASSETS = "external"
  19. # The name of the utils file.
  20. UTILS = "utils"
  21. # The name of the state file.
  22. STATE_PATH = "/".join([UTILS, "state"])
  23. # The name of the components file.
  24. COMPONENTS_PATH = "/".join([UTILS, "components"])
  25. # The name of the contexts file.
  26. CONTEXTS_PATH = "/".join([UTILS, "context"])
  27. # The name of the output static directory.
  28. STATIC = "_static"
  29. # The name of the public html directory served at "/"
  30. PUBLIC = "public"
  31. # The directory where styles are located.
  32. STYLES = "styles"
  33. # The name of the pages directory.
  34. PAGES = "pages"
  35. # The name of the env json file.
  36. ENV_JSON = "env.json"
  37. # The name of the reflex json file.
  38. REFLEX_JSON = "reflex.json"
  39. # The name of the postcss config file.
  40. POSTCSS_JS = "postcss.config.js"
  41. # The name of the states directory.
  42. STATES = "states"
  43. class Reflex(SimpleNamespace):
  44. """Base constants concerning Reflex."""
  45. # App names and versions.
  46. # The name of the Reflex package.
  47. MODULE_NAME = "reflex"
  48. # The current version of Reflex.
  49. VERSION = metadata.version(MODULE_NAME)
  50. # The reflex json file.
  51. JSON = "reflex.json"
  52. # Files and directories used to init a new project.
  53. # The directory to store reflex dependencies.
  54. # Get directory value from enviroment variables if it exists.
  55. _dir = os.environ.get("REFLEX_DIR", "")
  56. DIR = _dir or (
  57. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  58. # on macOS, we use ~/Library/Application Support/reflex.
  59. # on linux, we use ~/.local/share/reflex.
  60. # If user sets REFLEX_DIR envroment variable use that instead.
  61. PlatformDirs(MODULE_NAME, False).user_data_dir
  62. )
  63. # The root directory of the reflex library.
  64. ROOT_DIR = os.path.dirname(
  65. os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  66. )
  67. RELEASES_URL = f"https://api.github.com/repos/reflex-dev/templates/releases"
  68. class ReflexHostingCLI(SimpleNamespace):
  69. """Base constants concerning Reflex Hosting CLI."""
  70. # The name of the Reflex Hosting CLI package.
  71. MODULE_NAME = "reflex-hosting-cli"
  72. class Templates(SimpleNamespace):
  73. """Constants related to Templates."""
  74. # The route on Reflex backend to query which templates are available and their URLs.
  75. APP_TEMPLATES_ROUTE = "/app-templates"
  76. # The default template
  77. DEFAULT = "blank"
  78. # The reflex.build frontend host
  79. REFLEX_BUILD_FRONTEND = os.environ.get(
  80. "REFLEX_BUILD_FRONTEND", "https://flexgen.reflex.run"
  81. )
  82. # The reflex.build backend host
  83. REFLEX_BUILD_BACKEND = os.environ.get(
  84. "REFLEX_BUILD_BACKEND", "https://flexgen-prod-flexgen.fly.dev"
  85. )
  86. # The URL to redirect to reflex.build
  87. REFLEX_BUILD_URL = (
  88. REFLEX_BUILD_FRONTEND + "/gen?reflex_init_token={reflex_init_token}"
  89. )
  90. # The URL to poll waiting for the user to select a generation.
  91. REFLEX_BUILD_POLL_URL = REFLEX_BUILD_BACKEND + "/api/init/{reflex_init_token}"
  92. # The URL to fetch the generation's reflex code
  93. REFLEX_BUILD_CODE_URL = (
  94. REFLEX_BUILD_BACKEND + "/api/gen/{generation_hash}/refactored"
  95. )
  96. class Dirs(SimpleNamespace):
  97. """Folders used by the template system of Reflex."""
  98. # The template directory used during reflex init.
  99. BASE = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates")
  100. # The web subdirectory of the template directory.
  101. WEB_TEMPLATE = os.path.join(BASE, "web")
  102. # The jinja template directory.
  103. JINJA_TEMPLATE = os.path.join(BASE, "jinja")
  104. # Where the code for the templates is stored.
  105. CODE = "code"
  106. class Next(SimpleNamespace):
  107. """Constants related to NextJS."""
  108. # The NextJS config file
  109. CONFIG_FILE = "next.config.js"
  110. # The sitemap config file.
  111. SITEMAP_CONFIG_FILE = "next-sitemap.config.js"
  112. # The node modules directory.
  113. NODE_MODULES = "node_modules"
  114. # The package lock file.
  115. PACKAGE_LOCK = "package-lock.json"
  116. # Regex to check for message displayed when frontend comes up
  117. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  118. # Color mode variables
  119. class ColorMode(SimpleNamespace):
  120. """Constants related to ColorMode."""
  121. NAME = "rawColorMode"
  122. RESOLVED_NAME = "resolvedColorMode"
  123. USE = "useColorMode"
  124. TOGGLE = "toggleColorMode"
  125. SET = "setColorMode"
  126. # Env modes
  127. class Env(str, Enum):
  128. """The environment modes."""
  129. DEV = "dev"
  130. PROD = "prod"
  131. # Log levels
  132. class LogLevel(str, Enum):
  133. """The log levels."""
  134. DEBUG = "debug"
  135. INFO = "info"
  136. WARNING = "warning"
  137. ERROR = "error"
  138. CRITICAL = "critical"
  139. def __le__(self, other: LogLevel) -> bool:
  140. """Compare log levels.
  141. Args:
  142. other: The other log level.
  143. Returns:
  144. True if the log level is less than or equal to the other log level.
  145. """
  146. levels = list(LogLevel)
  147. return levels.index(self) <= levels.index(other)
  148. # Server socket configuration variables
  149. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  150. class Ping(SimpleNamespace):
  151. """PING constants."""
  152. # The 'ping' interval
  153. INTERVAL = 25
  154. # The 'ping' timeout
  155. TIMEOUT = 120
  156. # Keys in the client_side_storage dict
  157. COOKIES = "cookies"
  158. LOCAL_STORAGE = "local_storage"
  159. SESSION_STORAGE = "session_storage"
  160. # If this env var is set to "yes", App.compile will be a no-op
  161. SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
  162. # This env var stores the execution mode of the app
  163. ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
  164. # Testing variables.
  165. # Testing os env set by pytest when running a test case.
  166. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  167. RELOAD_CONFIG = "__REFLEX_RELOAD_CONFIG"
  168. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  169. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"