base.py 4.9 KB

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