base.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. # https://github.com/oven-sh/bun/blob/main/src/cli/install.ps1
  11. IS_WINDOWS_BUN_SUPPORTED_MACHINE = IS_WINDOWS and platform.machine() in [
  12. "AMD64",
  13. "x86_64",
  14. ] # filter out 32 bit + ARM
  15. class Dirs(SimpleNamespace):
  16. """Various directories/paths used by Reflex."""
  17. # The frontend directories in a project.
  18. # The web folder where the NextJS app is compiled to.
  19. WEB = ".web"
  20. # The name of the assets directory.
  21. APP_ASSETS = "assets"
  22. # The name of the utils file.
  23. UTILS = "utils"
  24. # The name of the output static directory.
  25. STATIC = "_static"
  26. # The name of the state file.
  27. STATE_PATH = "/".join([UTILS, "state"])
  28. # The name of the components file.
  29. COMPONENTS_PATH = "/".join([UTILS, "components"])
  30. # The name of the contexts file.
  31. CONTEXTS_PATH = "/".join([UTILS, "context"])
  32. # The directory where the app pages are compiled to.
  33. WEB_PAGES = os.path.join(WEB, "pages")
  34. # The directory where the static build is located.
  35. WEB_STATIC = os.path.join(WEB, STATIC)
  36. # The directory where the utils file is located.
  37. WEB_UTILS = os.path.join(WEB, UTILS)
  38. # The directory where the assets are located.
  39. WEB_ASSETS = os.path.join(WEB, "public")
  40. # The env json file.
  41. ENV_JSON = os.path.join(WEB, "env.json")
  42. # The reflex json file.
  43. REFLEX_JSON = os.path.join(WEB, "reflex.json")
  44. # The path to postcss.config.js
  45. POSTCSS_JS = os.path.join(WEB, "postcss.config.js")
  46. class Reflex(SimpleNamespace):
  47. """Base constants concerning Reflex."""
  48. # App names and versions.
  49. # The name of the Reflex package.
  50. MODULE_NAME = "reflex"
  51. # The current version of Reflex.
  52. VERSION = metadata.version(MODULE_NAME)
  53. # The reflex json file.
  54. JSON = os.path.join(Dirs.WEB, "reflex.json")
  55. # Files and directories used to init a new project.
  56. # The directory to store reflex dependencies.
  57. # Get directory value from enviroment variables if it exists.
  58. _dir = os.environ.get("REFLEX_DIR", "")
  59. DIR = _dir or (
  60. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  61. # on macOS, we use ~/Library/Application Support/reflex.
  62. # on linux, we use ~/.local/share/reflex.
  63. # If user sets REFLEX_DIR envroment variable use that instead.
  64. PlatformDirs(MODULE_NAME, False).user_data_dir
  65. )
  66. # The root directory of the reflex library.
  67. ROOT_DIR = os.path.dirname(
  68. os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  69. )
  70. class ReflexHostingCLI(SimpleNamespace):
  71. """Base constants concerning Reflex Hosting CLI."""
  72. # The name of the Reflex Hosting CLI package.
  73. MODULE_NAME = "reflex-hosting-cli"
  74. class Templates(SimpleNamespace):
  75. """Constants related to Templates."""
  76. # The route on Reflex backend to query which templates are available and their URLs.
  77. APP_TEMPLATES_ROUTE = "/app-templates"
  78. # The default template
  79. DEFAULT = "blank"
  80. class Dirs(SimpleNamespace):
  81. """Folders used by the template system of Reflex."""
  82. # The template directory used during reflex init.
  83. BASE = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates")
  84. # The web subdirectory of the template directory.
  85. WEB_TEMPLATE = os.path.join(BASE, "web")
  86. # The jinja template directory.
  87. JINJA_TEMPLATE = os.path.join(BASE, "jinja")
  88. # Where the code for the templates is stored.
  89. CODE = "code"
  90. class Next(SimpleNamespace):
  91. """Constants related to NextJS."""
  92. # The NextJS config file
  93. CONFIG_FILE = "next.config.js"
  94. # The sitemap config file.
  95. SITEMAP_CONFIG_FILE = os.path.join(Dirs.WEB, "next-sitemap.config.js")
  96. # The node modules directory.
  97. NODE_MODULES = "node_modules"
  98. # The package lock file.
  99. PACKAGE_LOCK = "package-lock.json"
  100. # Regex to check for message displayed when frontend comes up
  101. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  102. # Color mode variables
  103. class ColorMode(SimpleNamespace):
  104. """Constants related to ColorMode."""
  105. NAME = "colorMode"
  106. USE = "useColorMode"
  107. TOGGLE = "toggleColorMode"
  108. # Env modes
  109. class Env(str, Enum):
  110. """The environment modes."""
  111. DEV = "dev"
  112. PROD = "prod"
  113. # Log levels
  114. class LogLevel(str, Enum):
  115. """The log levels."""
  116. DEBUG = "debug"
  117. INFO = "info"
  118. WARNING = "warning"
  119. ERROR = "error"
  120. CRITICAL = "critical"
  121. def __le__(self, other: LogLevel) -> bool:
  122. """Compare log levels.
  123. Args:
  124. other: The other log level.
  125. Returns:
  126. True if the log level is less than or equal to the other log level.
  127. """
  128. levels = list(LogLevel)
  129. return levels.index(self) <= levels.index(other)
  130. # Server socket configuration variables
  131. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  132. class Ping(SimpleNamespace):
  133. """PING constants."""
  134. # The 'ping' interval
  135. INTERVAL = 25
  136. # The 'ping' timeout
  137. TIMEOUT = 120
  138. # Keys in the client_side_storage dict
  139. COOKIES = "cookies"
  140. LOCAL_STORAGE = "local_storage"
  141. # If this env var is set to "yes", App.compile will be a no-op
  142. SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
  143. # This env var stores the execution mode of the app
  144. ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
  145. # Testing variables.
  146. # Testing os env set by pytest when running a test case.
  147. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  148. RELOAD_CONFIG = "__REFLEX_RELOAD_CONFIG"
  149. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  150. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"