Skip to content

Commit ed483e7

Browse files
authored
Merge pull request #3 from GilbN/feat/envs
1.3.0 - Added new method `get_envs`
2 parents 6a51ba0 + fb83403 commit ed483e7

File tree

5 files changed

+55
-17
lines changed

5 files changed

+55
-17
lines changed

CHANGELOG.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,25 @@ and this project adheres to [Semantic Versioning].
99

1010
- /
1111

12+
## [1.3.0] - 2024-07-14
13+
14+
### Added
15+
16+
Added new method `get_envs` that returns all the environment variables set in a dict.
17+
```python
18+
>>> settings.get_envs()
19+
{'PREFIX_APP_IP': '0.0.0.0'}
20+
```
21+
22+
### Fixed
23+
24+
Fixed docstring indentation.
25+
1226
## [1.2.2] - 2024-03-10
1327

1428
## New Release v.1.2.2
1529

16-
## Fixes
30+
### Fixes
1731

1832
Fixes `TypeError: unsupported operand type(s) for |: 'type' and 'type'.` error when using Python version < 3.10
1933

@@ -81,7 +95,8 @@ print(settings.mysql.databases.prod) # Output: 'new_value'
8195
[semantic versioning]: https://semver.org/spec/v2.0.0.html
8296

8397
<!-- Versions -->
84-
[unreleased]: https://github.com/gilbn/simple-toml-configurator/compare/1.2.2...HEAD
98+
[unreleased]: https://github.com/gilbn/simple-toml-configurator/compare/1.3.0...HEAD
99+
[1.3.0]: https://github.com/gilbn/simple-toml-configurator/releases/tag/1.3.0
85100
[1.2.2]: https://github.com/gilbn/simple-toml-configurator/releases/tag/1.2.2
86101
[1.2.1]: https://github.com/gilbn/simple-toml-configurator/releases/tag/1.2.1
87102
[1.1.0]: https://github.com/gilbn/simple-toml-configurator/releases/tag/1.1.0

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "Simple-TOML-Configurator"
7-
version = "1.2.2"
7+
version = "1.3.0"
88
license = {text = "MIT License"}
99
authors = [{name = "GilbN", email = "[email protected]"}]
1010
description = "A simple TOML configurator for Python"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .toml_configurator import Configuration
1+
from .toml_configurator import Configuration, ConfigObject
22

3-
__all__ = ["Configuration"]
3+
__all__ = ["Configuration"]

src/simple_toml_configurator/toml_configurator.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from pathlib import Path
1010
import tomlkit
1111
from tomlkit import TOMLDocument
12-
from .exceptions import *
12+
from .exceptions import TOMLConfigUpdateError, TOMLWriteConfigError, TOMLCreateConfigError, TOMLLoadConfigError
1313

14-
__version__ = "1.2.2"
14+
__version__ = "1.3.0"
1515

1616
logger = logging.getLogger(__name__)
1717

@@ -88,15 +88,15 @@ def logging_debug(self, value: bool):
8888
configure_logging(log_level)
8989
logger.debug(f"Debug logging set to {value}")
9090
91-
defaults = {
92-
"logging": {
93-
"debug": False
94-
...
95-
}
91+
defaults = {
92+
"logging": {
93+
"debug": False
94+
...
95+
}
9696
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"))
100100
```
101101
"""
102102

@@ -153,6 +153,7 @@ def init_config(self, config_path:str|Path, defaults:dict[str,dict], config_file
153153
self.config_path:str|Path = config_path
154154
self.config_file_name:str = f"{config_file_name}.toml"
155155
self.env_prefix:str = env_prefix
156+
self._envs: dict[str,Any] = {}
156157
self._full_config_path:str = os.path.join(self.config_path, self.config_file_name)
157158
self.config:TOMLDocument = self._load_config()
158159
self._sync_config_values()
@@ -237,6 +238,22 @@ def get_settings(self) -> dict[str, Any]:
237238
settings[f"{table}_{key}"] = value
238239
return settings
239240

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+
240257
def _set_attributes(self) -> dict[str, Any]:
241258
"""Set all config keys as attributes on the class.
242259
@@ -312,6 +329,7 @@ def _update_os_env(self, table:str, key:str, value:Any) -> None:
312329
else:
313330
env_var = self._make_env_name(table, key)
314331
os.environ[env_var] = str(value)
332+
self._envs[env_var] = value
315333

316334
def _set_os_env(self) -> None:
317335
"""Set all config keys as environment variables.
@@ -389,7 +407,7 @@ def _write_config_to_file(self) -> None:
389407
"""Update and write the config to file"""
390408
self.logger.debug("Writing config to file")
391409
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:
393411
toml_document = tomlkit.dumps(self.config)
394412
# Use regular expression to replace consecutive empty lines with a single newline
395413
cleaned_toml = re.sub(r'\n{3,}', '\n\n', toml_document)

tests/test_toml_configurator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,9 @@ def test_make_env_name_without_prefix(config_instance: Configuration):
269269
table = "app"
270270
key = "port"
271271
env_name = config_instance._make_env_name(table, key)
272-
assert env_name == "APP_PORT"
272+
assert env_name == "APP_PORT"
273+
274+
def test_get_envs(config_instance: Configuration):
275+
config_instance.logging.debug = False
276+
config_instance.update()
277+
assert config_instance.get_envs() == {"LOGGING_DEBUG": False}

0 commit comments

Comments
 (0)