base.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. class Reflex(SimpleNamespace):
  40. """Base constants concerning Reflex."""
  41. # App names and versions.
  42. # The name of the Reflex package.
  43. MODULE_NAME = "reflex"
  44. # The current version of Reflex.
  45. VERSION = metadata.version(MODULE_NAME)
  46. # The reflex json file.
  47. JSON = os.path.join(Dirs.WEB, "reflex.json")
  48. # Files and directories used to init a new project.
  49. # The directory to store reflex dependencies.
  50. DIR = (
  51. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  52. # on macOS, we use ~/Library/Application Support/reflex.
  53. # on linux, we use ~/.local/share/reflex.
  54. PlatformDirs(MODULE_NAME, False).user_data_dir
  55. )
  56. # The root directory of the reflex library.
  57. ROOT_DIR = os.path.dirname(
  58. os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  59. )
  60. class Templates(SimpleNamespace):
  61. """Constants related to Templates."""
  62. # Dynamically get the enum values from the .templates folder
  63. template_dir = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates/apps")
  64. template_dirs = next(os.walk(template_dir))[1]
  65. # Create an enum value for each directory in the .templates folder
  66. Kind = Enum("Kind", {template.upper(): template for template in template_dirs})
  67. class Dirs(SimpleNamespace):
  68. """Folders used by the template system of Reflex."""
  69. # The template directory used during reflex init.
  70. BASE = os.path.join(Reflex.ROOT_DIR, Reflex.MODULE_NAME, ".templates")
  71. # The web subdirectory of the template directory.
  72. WEB_TEMPLATE = os.path.join(BASE, "web")
  73. # The jinja template directory.
  74. JINJA_TEMPLATE = os.path.join(BASE, "jinja")
  75. # Where the code for the templates is stored.
  76. CODE = "code"
  77. class Next(SimpleNamespace):
  78. """Constants related to NextJS."""
  79. # The NextJS config file
  80. CONFIG_FILE = "next.config.js"
  81. # The sitemap config file.
  82. SITEMAP_CONFIG_FILE = os.path.join(Dirs.WEB, "next-sitemap.config.js")
  83. # The node modules directory.
  84. NODE_MODULES = "node_modules"
  85. # The package lock file.
  86. PACKAGE_LOCK = "package-lock.json"
  87. # Regex to check for message displayed when frontend comes up
  88. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  89. # Color mode variables
  90. class ColorMode(SimpleNamespace):
  91. """Constants related to ColorMode."""
  92. NAME = "colorMode"
  93. USE = "useColorMode"
  94. TOGGLE = "toggleColorMode"
  95. # Env modes
  96. class Env(str, Enum):
  97. """The environment modes."""
  98. DEV = "dev"
  99. PROD = "prod"
  100. # Log levels
  101. class LogLevel(str, Enum):
  102. """The log levels."""
  103. DEBUG = "debug"
  104. INFO = "info"
  105. WARNING = "warning"
  106. ERROR = "error"
  107. CRITICAL = "critical"
  108. def __le__(self, other: LogLevel) -> bool:
  109. """Compare log levels.
  110. Args:
  111. other: The other log level.
  112. Returns:
  113. True if the log level is less than or equal to the other log level.
  114. """
  115. levels = list(LogLevel)
  116. return levels.index(self) <= levels.index(other)
  117. # Server socket configuration variables
  118. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  119. class Ping(SimpleNamespace):
  120. """PING constants."""
  121. # The 'ping' interval
  122. INTERVAL = 25
  123. # The 'ping' timeout
  124. TIMEOUT = 120
  125. # Keys in the client_side_storage dict
  126. COOKIES = "cookies"
  127. LOCAL_STORAGE = "local_storage"
  128. # If this env var is set to "yes", App.compile will be a no-op
  129. SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
  130. # Testing variables.
  131. # Testing os env set by pytest when running a test case.
  132. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  133. RELOAD_CONFIG = "__REFLEX_RELOAD_CONFIG"