improve EnvVar api, cleanup RELOAD_CONFIG question

This commit is contained in:
Benedikt Bartscher 2024-10-31 23:32:05 +01:00
parent 2f91465106
commit 826660bebd
No known key found for this signature in database
2 changed files with 28 additions and 10 deletions

View File

@ -12,7 +12,6 @@ import inspect
import io
import json
import multiprocessing
import os
import platform
import sys
import traceback
@ -505,10 +504,7 @@ class App(MiddlewareMixin, LifespanMixin, Base):
# Check if the route given is valid
verify_route_validity(route)
# TODO: this was broken?
if route in self.unevaluated_pages and os.getenv(
environment.RELOAD_CONFIG.name
):
if route in self.unevaluated_pages and environment.RELOAD_CONFIG.is_set():
# when the app is reloaded(typically for app harness tests), we should maintain
# the latest render function of a route.This applies typically to decorated pages
# since they are only added when app._compile is called.

View File

@ -332,23 +332,45 @@ class EnvVar(Generic[T]):
self.default = default
self.type_ = type_
def getenv(self) -> Optional[str]:
"""Get the environment variable from os.environ.
def interpret(self, value: str) -> T:
"""Interpret the environment variable value.
Args:
value: The environment variable value.
Returns:
The interpreted value.
"""
return interpret_env_var_value(value, self.type_, self.name)
def getenv(self) -> Optional[T]:
"""Get the interpreted environment variable value.
Returns:
The environment variable value.
"""
return os.getenv(self.name, None)
env_value = os.getenv(self.name, None)
if env_value is not None:
return self.interpret(env_value)
return None
def is_set(self) -> bool:
"""Check if the environment variable is set.
Returns:
True if the environment variable is set.
"""
return self.name in os.environ
def get(self) -> T:
"""Get the interpreted environment variable value.
"""Get the interpreted environment variable value or the default value if not set.
Returns:
The interpreted value.
"""
env_value = self.getenv()
if env_value is not None:
return interpret_env_var_value(env_value, self.type_, self.name)
return env_value
return self.default
def set(self, value: T | None) -> None: