|
12 | 12 | """ |
13 | 13 |
|
14 | 14 | # Standard |
15 | | -from typing import Literal |
| 15 | +from functools import lru_cache |
| 16 | +import os |
| 17 | +from typing import Any, Literal |
16 | 18 |
|
17 | 19 | # Third-Party |
18 | 20 | from pydantic import AliasChoices, Field |
@@ -99,4 +101,56 @@ class PluginsSettings(BaseSettings): |
99 | 101 | model_config = SettingsConfigDict(env_prefix="PLUGINS_") |
100 | 102 |
|
101 | 103 |
|
102 | | -settings = PluginsSettings() |
| 104 | +@lru_cache() |
| 105 | +def get_settings(**kwargs: Any) -> PluginsSettings: |
| 106 | + """Get cached plugins settings instance. |
| 107 | +
|
| 108 | + Args: |
| 109 | + **kwargs: Keyword arguments to pass to the PluginsSettings setup. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + PluginsSettings: A cached instance of the PluginsSettings class. |
| 113 | +
|
| 114 | + Examples: |
| 115 | + >>> settings = get_settings() |
| 116 | + >>> isinstance(settings, PluginsSettings) |
| 117 | + True |
| 118 | + >>> # Second call returns the same cached instance |
| 119 | + >>> settings2 = get_settings() |
| 120 | + >>> settings is settings2 |
| 121 | + True |
| 122 | + """ |
| 123 | + # Instantiate a fresh Pydantic PluginsSettings object, |
| 124 | + # loading from env vars or .env exactly once. |
| 125 | + return PluginsSettings(**kwargs) |
| 126 | + |
| 127 | + |
| 128 | +class LazySettingsWrapper: |
| 129 | + """Lazily initialize plugins settings singleton on getattr with override support.""" |
| 130 | + |
| 131 | + @property |
| 132 | + def enabled(self) -> bool: |
| 133 | + """Access plugin enabled flag with env override support. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + True if plugin framework is enabled. |
| 137 | + """ |
| 138 | + env_flag = os.getenv("PLUGINS_ENABLED") |
| 139 | + if env_flag is not None: |
| 140 | + return env_flag.strip().lower() in {"1", "true", "yes", "on"} |
| 141 | + return False |
| 142 | + |
| 143 | + def __getattr__(self, key: str) -> Any: |
| 144 | + """Get the real settings object and forward to it |
| 145 | +
|
| 146 | + Args: |
| 147 | + key: The key to fetch from settings |
| 148 | +
|
| 149 | + Returns: |
| 150 | + Any: The value of the attribute on the settings |
| 151 | + """ |
| 152 | + |
| 153 | + return getattr(get_settings(), key) |
| 154 | + |
| 155 | + |
| 156 | +settings = LazySettingsWrapper() |
0 commit comments