base.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. class Reflex(SimpleNamespace):
  42. """Base constants concerning Reflex."""
  43. # App names and versions.
  44. # The name of the Reflex package.
  45. MODULE_NAME = "reflex"
  46. # The current version of Reflex.
  47. VERSION = metadata.version(MODULE_NAME)
  48. # The reflex json file.
  49. JSON = "reflex.json"
  50. # Files and directories used to init a new project.
  51. # The directory to store reflex dependencies.
  52. # Get directory value from enviroment variables if it exists.
  53. _dir = os.environ.get("REFLEX_DIR", "")
  54. DIR = _dir or (
  55. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  56. # on macOS, we use ~/Library/Application Support/reflex.
  57. # on linux, we use ~/.local/share/reflex.
  58. # If user sets REFLEX_DIR envroment variable use that instead.
  59. PlatformDirs(MODULE_NAME, False).user_data_dir
  60. )
  61. # The root directory of the reflex library.
  62. ROOT_DIR = os.path.dirname(
  63. os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  64. )
  65. RELEASES_URL = f"https://api.github.com/repos/reflex-dev/templates/releases"
  66. class ReflexHostingCLI(SimpleNamespace):
  67. """Base constants concerning Reflex Hosting CLI."""
  68. # The name of the Reflex Hosting CLI package.
  69. MODULE_NAME = "reflex-hosting-cli"
  70. class Templates(SimpleNamespace):
  71. """Constants related to Templates."""
  72. # The route on Reflex backend to query which templates are available and their URLs.
  73. APP_TEMPLATES_ROUTE = "/app-templates"
  74. # The default template
  75. DEFAULT = "blank"
  76. # The reflex.build frontend host
  77. REFLEX_BUILD_FRONTEND = os.environ.get(
  78. "REFLEX_BUILD_FRONTEND", "https://flexgen.reflex.run"
  79. )
  80. # The reflex.build backend host
  81. REFLEX_BUILD_BACKEND = os.environ.get(
  82. "REFLEX_BUILD_BACKEND", "https://rxh-prod-flexgen.fly.dev"
  83. )
  84. # The URL to redirect to reflex.build
  85. REFLEX_BUILD_URL = (
  86. REFLEX_BUILD_FRONTEND + "/gen?reflex_init_token={reflex_init_token}"
  87. )
  88. # The URL to poll waiting for the user to select a generation.
  89. REFLEX_BUILD_POLL_URL = REFLEX_BUILD_BACKEND + "/api/init/{reflex_init_token}"
  90. # The URL to fetch the generation's reflex code
  91. REFLEX_BUILD_CODE_URL = REFLEX_BUILD_BACKEND + "/api/gen/{generation_hash}"
  92. class Dirs(SimpleNamespace):
  93. """Folders used by the template system of Reflex."""
  94. # The template directory used during reflex init.
  95. BASE = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates")
  96. # The web subdirectory of the template directory.
  97. WEB_TEMPLATE = os.path.join(BASE, "web")
  98. # The jinja template directory.
  99. JINJA_TEMPLATE = os.path.join(BASE, "jinja")
  100. # Where the code for the templates is stored.
  101. CODE = "code"
  102. class Next(SimpleNamespace):
  103. """Constants related to NextJS."""
  104. # The NextJS config file
  105. CONFIG_FILE = "next.config.js"
  106. # The sitemap config file.
  107. SITEMAP_CONFIG_FILE = "next-sitemap.config.js"
  108. # The node modules directory.
  109. NODE_MODULES = "node_modules"
  110. # The package lock file.
  111. PACKAGE_LOCK = "package-lock.json"
  112. # Regex to check for message displayed when frontend comes up
  113. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  114. # Color mode variables
  115. class ColorMode(SimpleNamespace):
  116. """Constants related to ColorMode."""
  117. NAME = "rawColorMode"
  118. RESOLVED_NAME = "resolvedColorMode"
  119. USE = "useColorMode"
  120. TOGGLE = "toggleColorMode"
  121. SET = "setColorMode"
  122. # Env modes
  123. class Env(str, Enum):
  124. """The environment modes."""
  125. DEV = "dev"
  126. PROD = "prod"
  127. # Log levels
  128. class LogLevel(str, Enum):
  129. """The log levels."""
  130. DEBUG = "debug"
  131. INFO = "info"
  132. WARNING = "warning"
  133. ERROR = "error"
  134. CRITICAL = "critical"
  135. def __le__(self, other: LogLevel) -> bool:
  136. """Compare log levels.
  137. Args:
  138. other: The other log level.
  139. Returns:
  140. True if the log level is less than or equal to the other log level.
  141. """
  142. levels = list(LogLevel)
  143. return levels.index(self) <= levels.index(other)
  144. # Server socket configuration variables
  145. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  146. class Ping(SimpleNamespace):
  147. """PING constants."""
  148. # The 'ping' interval
  149. INTERVAL = 25
  150. # The 'ping' timeout
  151. TIMEOUT = 120
  152. # Keys in the client_side_storage dict
  153. COOKIES = "cookies"
  154. LOCAL_STORAGE = "local_storage"
  155. SESSION_STORAGE = "session_storage"
  156. # If this env var is set to "yes", App.compile will be a no-op
  157. SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
  158. # This env var stores the execution mode of the app
  159. ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
  160. # Testing variables.
  161. # Testing os env set by pytest when running a test case.
  162. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  163. RELOAD_CONFIG = "__REFLEX_RELOAD_CONFIG"
  164. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  165. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"