improve EnvVar api, cleanup RELOAD_CONFIG question
This commit is contained in:
parent
2f91465106
commit
826660bebd
@ -12,7 +12,6 @@ import inspect
|
|||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
import os
|
|
||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
@ -505,10 +504,7 @@ class App(MiddlewareMixin, LifespanMixin, Base):
|
|||||||
# Check if the route given is valid
|
# Check if the route given is valid
|
||||||
verify_route_validity(route)
|
verify_route_validity(route)
|
||||||
|
|
||||||
# TODO: this was broken?
|
if route in self.unevaluated_pages and environment.RELOAD_CONFIG.is_set():
|
||||||
if route in self.unevaluated_pages and os.getenv(
|
|
||||||
environment.RELOAD_CONFIG.name
|
|
||||||
):
|
|
||||||
# when the app is reloaded(typically for app harness tests), we should maintain
|
# 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
|
# the latest render function of a route.This applies typically to decorated pages
|
||||||
# since they are only added when app._compile is called.
|
# since they are only added when app._compile is called.
|
||||||
|
@ -332,23 +332,45 @@ class EnvVar(Generic[T]):
|
|||||||
self.default = default
|
self.default = default
|
||||||
self.type_ = type_
|
self.type_ = type_
|
||||||
|
|
||||||
def getenv(self) -> Optional[str]:
|
def interpret(self, value: str) -> T:
|
||||||
"""Get the environment variable from os.environ.
|
"""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:
|
Returns:
|
||||||
The environment variable value.
|
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:
|
def get(self) -> T:
|
||||||
"""Get the interpreted environment variable value.
|
"""Get the interpreted environment variable value or the default value if not set.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The interpreted value.
|
The interpreted value.
|
||||||
"""
|
"""
|
||||||
env_value = self.getenv()
|
env_value = self.getenv()
|
||||||
if env_value is not None:
|
if env_value is not None:
|
||||||
return interpret_env_var_value(env_value, self.type_, self.name)
|
return env_value
|
||||||
return self.default
|
return self.default
|
||||||
|
|
||||||
def set(self, value: T | None) -> None:
|
def set(self, value: T | None) -> None:
|
||||||
|
Loading…
Reference in New Issue
Block a user