move more variables to environment

This commit is contained in:
Khaleel Al-Adhami 2024-10-16 16:57:00 -07:00
parent 5dffc77f33
commit 11da6ecb25
6 changed files with 48 additions and 28 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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",

View File

@ -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():

View File

@ -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:

View File

@ -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():