From 11da6ecb258445d3adf85c4c448a134bc4a4ca60 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Wed, 16 Oct 2024 16:57:00 -0700 Subject: [PATCH] move more variables to environment --- reflex/config.py | 36 +++++++++++++++++++ reflex/constants/installer.py | 6 ++-- reflex/custom_components/custom_components.py | 6 ++-- reflex/utils/exec.py | 4 +-- reflex/utils/path_ops.py | 21 ++--------- reflex/utils/prerequisites.py | 3 +- 6 files changed, 48 insertions(+), 28 deletions(-) diff --git a/reflex/config.py b/reflex/config.py index ed9ce7820..34466962a 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -178,6 +178,21 @@ def interpret_boolean_env(value: str) -> bool: raise ValueError(f"Invalid boolean value: {value}") +def interpret_path_env(value: str) -> Path: + """Interpret a path environment variable value. + + Args: + value: The environment variable value. + + Returns: + The interpreted value. + + Raises: + ValueError: If the value is invalid. + """ + return Path(value) + + def interpret_env_var_value(value: str, field: dataclasses.Field) -> Any: """Interpret an environment variable value based on the field type. @@ -199,6 +214,9 @@ def interpret_env_var_value(value: str, field: dataclasses.Field) -> Any: elif field_type is str: return value + elif field_type is Path: + return interpret_path_env(value) + else: raise ValueError( f"Invalid type for environment variable {field.name}: {field_type}. This is probably an issue in Reflex." @@ -215,6 +233,24 @@ class EnvironmentVariables: # The npm registry to use. NPM_CONFIG_REGISTRY: Optional[str] = None + # Whether to use Granian for the backend. Otherwise, use Uvicorn. + REFLEX_USE_GRANIAN: bool = False + + # The username to use for authentication on python package repository. Username and password must both be provided. + TWINE_USERNAME: Optional[str] = None + + # The password to use for authentication on python package repository. Username and password must both be provided. + TWINE_PASSWORD: Optional[str] = None + + # Whether to use the system installed bun. If set to false, bun will be bundled with the app. + REFLEX_USE_SYSTEM_BUN: bool = False + + # Whether to use the system installed node and npm. If set to false, node and npm will be bundled with the app. + REFLEX_USE_SYSTEM_NODE: bool = False + + # The working directory for the next.js commands. + REFLEX_WEB_WORKDIR: Path = Path(constants.Dirs.WEB) + def __init__(self): """Initialize the environment variables.""" for field in dataclasses.fields(self): diff --git a/reflex/constants/installer.py b/reflex/constants/installer.py index b12d56c78..a6acf277e 100644 --- a/reflex/constants/installer.py +++ b/reflex/constants/installer.py @@ -5,6 +5,8 @@ from __future__ import annotations import platform from types import SimpleNamespace +from reflex.config import environment + from .base import IS_WINDOWS, Reflex @@ -54,7 +56,7 @@ class Bun(SimpleNamespace): CONFIG_PATH = "bunfig.toml" # The environment variable to use the system installed bun. - USE_SYSTEM_VAR = "REFLEX_USE_SYSTEM_BUN" + USE_SYSTEM = environment.REFLEX_USE_SYSTEM_BUN # FNM config. @@ -100,7 +102,7 @@ class Node(SimpleNamespace): NPM_PATH = BIN_PATH / "npm" # The environment variable to use the system installed node. - USE_SYSTEM_VAR = "REFLEX_USE_SYSTEM_NODE" + USE_SYSTEM = environment.REFLEX_USE_SYSTEM_NODE class PackageJson(SimpleNamespace): diff --git a/reflex/custom_components/custom_components.py b/reflex/custom_components/custom_components.py index ee24a7cd0..ddda3de56 100644 --- a/reflex/custom_components/custom_components.py +++ b/reflex/custom_components/custom_components.py @@ -17,7 +17,7 @@ import typer from tomlkit.exceptions import TOMLKitError from reflex import constants -from reflex.config import get_config +from reflex.config import environment, get_config from reflex.constants import CustomComponents from reflex.utils import console @@ -609,14 +609,14 @@ def publish( help="The API token to use for authentication on python package repository. If token is provided, no username/password should be provided at the same time", ), username: Optional[str] = typer.Option( - os.getenv("TWINE_USERNAME"), + environment.TWINE_USERNAME, "-u", "--username", show_default="TWINE_USERNAME environment variable value if set", help="The username to use for authentication on python package repository. Username and password must both be provided.", ), password: Optional[str] = typer.Option( - os.getenv("TWINE_PASSWORD"), + environment.TWINE_PASSWORD, "-p", "--password", show_default="TWINE_PASSWORD environment variable value if set", diff --git a/reflex/utils/exec.py b/reflex/utils/exec.py index acb69ee19..293a778c4 100644 --- a/reflex/utils/exec.py +++ b/reflex/utils/exec.py @@ -15,7 +15,7 @@ from urllib.parse import urljoin import psutil from reflex import constants -from reflex.config import get_config +from reflex.config import environment, get_config from reflex.constants.base import LogLevel from reflex.utils import console, path_ops from reflex.utils.prerequisites import get_web_dir @@ -184,7 +184,7 @@ def should_use_granian(): Returns: True if Granian should be used. """ - return os.getenv("REFLEX_USE_GRANIAN", "0") == "1" + return environment.REFLEX_USE_GRANIAN def get_app_module(): diff --git a/reflex/utils/path_ops.py b/reflex/utils/path_ops.py index f795e1aa4..902f37c97 100644 --- a/reflex/utils/path_ops.py +++ b/reflex/utils/path_ops.py @@ -129,30 +129,13 @@ def which(program: str | Path) -> str | Path | None: return shutil.which(str(program)) -def use_system_install(var_name: str) -> bool: - """Check if the system install should be used. - - Args: - var_name: The name of the environment variable. - - Raises: - ValueError: If the variable name is invalid. - - Returns: - Whether the associated env var should use the system install. - """ - if not var_name.startswith("REFLEX_USE_SYSTEM_"): - raise ValueError("Invalid system install variable name.") - return os.getenv(var_name, "").lower() in ["true", "1", "yes"] - - def use_system_node() -> bool: """Check if the system node should be used. Returns: Whether the system node should be used. """ - return use_system_install(constants.Node.USE_SYSTEM_VAR) + return constants.Node.USE_SYSTEM def use_system_bun() -> bool: @@ -161,7 +144,7 @@ def use_system_bun() -> bool: Returns: Whether the system bun should be used. """ - return use_system_install(constants.Bun.USE_SYSTEM_VAR) + return constants.Bun.USE_SYSTEM def get_node_bin_path() -> Path | None: diff --git a/reflex/utils/prerequisites.py b/reflex/utils/prerequisites.py index 989dce3fa..8bb78cad7 100644 --- a/reflex/utils/prerequisites.py +++ b/reflex/utils/prerequisites.py @@ -69,8 +69,7 @@ def get_web_dir() -> Path: Returns: The working directory. """ - workdir = Path(os.getenv("REFLEX_WEB_WORKDIR", constants.Dirs.WEB)) - return workdir + return environment.REFLEX_WEB_WORKDIR def _python_version_check():