|
9 | 9 | from pathlib import Path
|
10 | 10 | import tomlkit
|
11 | 11 | from tomlkit import TOMLDocument
|
12 |
| -from .exceptions import * |
| 12 | +from .exceptions import TOMLConfigUpdateError, TOMLWriteConfigError, TOMLCreateConfigError, TOMLLoadConfigError |
13 | 13 |
|
14 |
| -__version__ = "1.2.2" |
| 14 | +__version__ = "1.3.0" |
15 | 15 |
|
16 | 16 | logger = logging.getLogger(__name__)
|
17 | 17 |
|
@@ -88,15 +88,15 @@ def logging_debug(self, value: bool):
|
88 | 88 | configure_logging(log_level)
|
89 | 89 | logger.debug(f"Debug logging set to {value}")
|
90 | 90 |
|
91 |
| - defaults = { |
92 |
| - "logging": { |
93 |
| - "debug": False |
94 |
| - ... |
95 |
| - } |
| 91 | + defaults = { |
| 92 | + "logging": { |
| 93 | + "debug": False |
| 94 | + ... |
| 95 | + } |
96 | 96 |
|
97 |
| - config_path = os.environ.get("CONFIG_PATH", "config") |
98 |
| - settings = CustomConfiguration() |
99 |
| - settings.init_config(config_path, defaults, "app_config")) |
| 97 | + config_path = os.environ.get("CONFIG_PATH", "config") |
| 98 | + settings = CustomConfiguration() |
| 99 | + settings.init_config(config_path, defaults, "app_config")) |
100 | 100 | ```
|
101 | 101 | """
|
102 | 102 |
|
@@ -153,6 +153,7 @@ def init_config(self, config_path:str|Path, defaults:dict[str,dict], config_file
|
153 | 153 | self.config_path:str|Path = config_path
|
154 | 154 | self.config_file_name:str = f"{config_file_name}.toml"
|
155 | 155 | self.env_prefix:str = env_prefix
|
| 156 | + self._envs: dict[str,Any] = {} |
156 | 157 | self._full_config_path:str = os.path.join(self.config_path, self.config_file_name)
|
157 | 158 | self.config:TOMLDocument = self._load_config()
|
158 | 159 | self._sync_config_values()
|
@@ -237,6 +238,22 @@ def get_settings(self) -> dict[str, Any]:
|
237 | 238 | settings[f"{table}_{key}"] = value
|
238 | 239 | return settings
|
239 | 240 |
|
| 241 | + def get_envs(self) -> dict[str, Any]: |
| 242 | + """Get all the environment variables we set as a dictionary. |
| 243 | + |
| 244 | + Examples: |
| 245 | + ```pycon |
| 246 | + >>> defaults = {...} |
| 247 | + >>> settings = Configuration() |
| 248 | + >>> settings.init_config("config", defaults, "app_config")) |
| 249 | + >>> settings.get_envs() |
| 250 | + {'PREFIX_APP_IP': '0.0.0.0'} |
| 251 | + |
| 252 | + Returns: |
| 253 | + dict[str, Any]: Dictionary with all environment variables name as keys and their value. |
| 254 | + """ |
| 255 | + return self._envs |
| 256 | + |
240 | 257 | def _set_attributes(self) -> dict[str, Any]:
|
241 | 258 | """Set all config keys as attributes on the class.
|
242 | 259 |
|
@@ -312,6 +329,7 @@ def _update_os_env(self, table:str, key:str, value:Any) -> None:
|
312 | 329 | else:
|
313 | 330 | env_var = self._make_env_name(table, key)
|
314 | 331 | os.environ[env_var] = str(value)
|
| 332 | + self._envs[env_var] = value |
315 | 333 |
|
316 | 334 | def _set_os_env(self) -> None:
|
317 | 335 | """Set all config keys as environment variables.
|
@@ -389,7 +407,7 @@ def _write_config_to_file(self) -> None:
|
389 | 407 | """Update and write the config to file"""
|
390 | 408 | self.logger.debug("Writing config to file")
|
391 | 409 | try:
|
392 |
| - with Path(self._full_config_path).open("w") as conf: |
| 410 | + with Path(self._full_config_path).open("w", encoding="utf-8") as conf: |
393 | 411 | toml_document = tomlkit.dumps(self.config)
|
394 | 412 | # Use regular expression to replace consecutive empty lines with a single newline
|
395 | 413 | cleaned_toml = re.sub(r'\n{3,}', '\n\n', toml_document)
|
|
0 commit comments