base.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 utils file.
  18. UTILS = "utils"
  19. # The name of the output static directory.
  20. STATIC = "_static"
  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 directory where the app pages are compiled to.
  28. WEB_PAGES = os.path.join(WEB, "pages")
  29. # The directory where the static build is located.
  30. WEB_STATIC = os.path.join(WEB, STATIC)
  31. # The directory where the utils file is located.
  32. WEB_UTILS = os.path.join(WEB, UTILS)
  33. # The directory where the assets are located.
  34. WEB_ASSETS = os.path.join(WEB, "public")
  35. # The env json file.
  36. ENV_JSON = os.path.join(WEB, "env.json")
  37. # The reflex json file.
  38. REFLEX_JSON = os.path.join(WEB, "reflex.json")
  39. # The path to postcss.config.js
  40. POSTCSS_JS = os.path.join(WEB, "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 = os.path.join(Dirs.WEB, "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. class ReflexHostingCLI(SimpleNamespace):
  66. """Base constants concerning Reflex Hosting CLI."""
  67. # The name of the Reflex Hosting CLI package.
  68. MODULE_NAME = "reflex-hosting-cli"
  69. class Templates(SimpleNamespace):
  70. """Constants related to Templates."""
  71. # Dynamically get the enum values from the .templates folder
  72. template_dir = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates/apps")
  73. template_dirs = next(os.walk(template_dir))[1]
  74. # Create an enum value for each directory in the .templates folder
  75. Kind = Enum("Kind", {template.upper(): template for template in template_dirs})
  76. class Dirs(SimpleNamespace):
  77. """Folders used by the template system of Reflex."""
  78. # The template directory used during reflex init.
  79. BASE = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates")
  80. # The web subdirectory of the template directory.
  81. WEB_TEMPLATE = os.path.join(BASE, "web")
  82. # The jinja template directory.
  83. JINJA_TEMPLATE = os.path.join(BASE, "jinja")
  84. # Where the code for the templates is stored.
  85. CODE = "code"
  86. class Next(SimpleNamespace):
  87. """Constants related to NextJS."""
  88. # The NextJS config file
  89. CONFIG_FILE = "next.config.js"
  90. # The sitemap config file.
  91. SITEMAP_CONFIG_FILE = os.path.join(Dirs.WEB, "next-sitemap.config.js")
  92. # The node modules directory.
  93. NODE_MODULES = "node_modules"
  94. # The package lock file.
  95. PACKAGE_LOCK = "package-lock.json"
  96. # Regex to check for message displayed when frontend comes up
  97. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  98. # Color mode variables
  99. class ColorMode(SimpleNamespace):
  100. """Constants related to ColorMode."""
  101. NAME = "colorMode"
  102. USE = "useColorMode"
  103. TOGGLE = "toggleColorMode"
  104. # Env modes
  105. class Env(str, Enum):
  106. """The environment modes."""
  107. DEV = "dev"
  108. PROD = "prod"
  109. # Log levels
  110. class LogLevel(str, Enum):
  111. """The log levels."""
  112. DEBUG = "debug"
  113. INFO = "info"
  114. WARNING = "warning"
  115. ERROR = "error"
  116. CRITICAL = "critical"
  117. def __le__(self, other: LogLevel) -> bool:
  118. """Compare log levels.
  119. Args:
  120. other: The other log level.
  121. Returns:
  122. True if the log level is less than or equal to the other log level.
  123. """
  124. levels = list(LogLevel)
  125. return levels.index(self) <= levels.index(other)
  126. # Server socket configuration variables
  127. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  128. class Ping(SimpleNamespace):
  129. """PING constants."""
  130. # The 'ping' interval
  131. INTERVAL = 25
  132. # The 'ping' timeout
  133. TIMEOUT = 120
  134. # Keys in the client_side_storage dict
  135. COOKIES = "cookies"
  136. LOCAL_STORAGE = "local_storage"
  137. # If this env var is set to "yes", App.compile will be a no-op
  138. SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
  139. # This env var stores the execution mode of the app
  140. ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
  141. # Testing variables.
  142. # Testing os env set by pytest when running a test case.
  143. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  144. RELOAD_CONFIG = "__REFLEX_RELOAD_CONFIG"
  145. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  146. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"