From 6655b789e3c65af77458fd41cc4441dd2ce4dc18 Mon Sep 17 00:00:00 2001 From: Khaleel Al-Adhami Date: Tue, 29 Oct 2024 10:44:40 -0700 Subject: [PATCH] add existing path subclass for env checks --- reflex/config.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/reflex/config.py b/reflex/config.py index 22a04c50c..5910d553c 100644 --- a/reflex/config.py +++ b/reflex/config.py @@ -204,6 +204,25 @@ def interpret_int_env(value: str, field_name: str) -> int: ) from ve +def interpret_existing_path_env(value: str, field_name: str) -> ExistingPath: + """Interpret a path environment variable value as an existing path. + + Args: + value: The environment variable value. + field_name: The field name. + + Returns: + The interpreted value. + + Raises: + EnvironmentVarValueError: If the path does not exist. + """ + path = ExistingPath(value) + if not path.exists(): + raise EnvironmentVarValueError(f"Path does not exist: {path} for {field_name}") + return path + + def interpret_path_env(value: str, field_name: str) -> Path: """Interpret a path environment variable value. @@ -217,10 +236,7 @@ def interpret_path_env(value: str, field_name: str) -> Path: Raises: EnvironmentVarValueError: If the path does not exist. """ - path = Path(value) - if not path.exists(): - raise EnvironmentVarValueError(f"Path does not exist: {path} for {field_name}") - return path + return Path(value) def interpret_enum_env(value: str, field_type: GenericType, field_name: str) -> Any: @@ -276,6 +292,8 @@ def interpret_env_var_value( return interpret_int_env(value, field_name) elif field_type is Path: return interpret_path_env(value, field_name) + elif field_type is ExistingPath: + return interpret_existing_path_env(value, field_name) elif inspect.isclass(field_type) and issubclass(field_type, enum.Enum): return interpret_enum_env(value, field_type, field_name) @@ -285,6 +303,10 @@ def interpret_env_var_value( ) +class ExistingPath(Path): + """A path that must exist.""" + + @dataclasses.dataclass(init=False) class EnvironmentVariables: """Environment variables class to instantiate environment variables.""" @@ -314,7 +336,7 @@ class EnvironmentVariables: REFLEX_WEB_WORKDIR: Path = Path(constants.Dirs.WEB) # Path to the alembic config file - ALEMBIC_CONFIG: Path = Path(constants.ALEMBIC_CONFIG) + ALEMBIC_CONFIG: ExistingPath = ExistingPath(constants.ALEMBIC_CONFIG) # Disable SSL verification for HTTPX requests. SSL_NO_VERIFY: bool = False