pyttings 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyttings/__init__.py ADDED
@@ -0,0 +1,5 @@
1
+ from .core import Settings
2
+
3
+ settings = Settings()
4
+
5
+ __all__ = ["settings"]
pyttings/core.py ADDED
@@ -0,0 +1,60 @@
1
+ import importlib
2
+ import os
3
+ from functools import cached_property
4
+ from typing import Any
5
+
6
+
7
+ class Settings:
8
+ def __init__(self) -> None:
9
+ self._settings_module: str = self._get_settings_module()
10
+ self._env_prefix: str = os.getenv("PYTTING_ENV_PREFIX", "PYTTING_")
11
+ self._cache: dict[str, Any] = {}
12
+
13
+ @cached_property
14
+ def defaults(self) -> dict[str, Any]:
15
+ module = importlib.import_module(self._settings_module)
16
+ return {
17
+ key: getattr(module, key)
18
+ for key in dir(module)
19
+ if not key.startswith("__") and not key.endswith("__") and key.isupper()
20
+ }
21
+
22
+ def _get_settings_module(self) -> str:
23
+ settings_module: str | None = os.getenv("PYTTING_SETTINGS_MODULE")
24
+ if settings_module is None:
25
+ raise ValueError(
26
+ "PYTTING_SETTINGS_MODULE environment variable is not set. "
27
+ "Please specify a settings module."
28
+ )
29
+ return settings_module
30
+
31
+ def _get_env_var(self, name: str) -> Any | None:
32
+ value = os.getenv(f"{self._env_prefix}{name}")
33
+ if value is None:
34
+ return None
35
+
36
+ if name not in self.defaults:
37
+ return value
38
+
39
+ value_type = type(self.defaults[name])
40
+
41
+ if value_type is bool:
42
+ return value.lower() == "true"
43
+
44
+ if value_type is type(None):
45
+ value_type = str
46
+
47
+ return value_type(value)
48
+
49
+ def __getattr__(self, name: str) -> Any:
50
+ if name not in self._cache:
51
+ value: Any | None = self._get_env_var(name)
52
+ if value is None:
53
+ if name in self.defaults:
54
+ value = self.defaults[name]
55
+ else:
56
+ raise AttributeError(
57
+ f"'{self.__class__.__name__}' object has no attribute '{name}'"
58
+ )
59
+ self._cache[name] = value
60
+ return self._cache[name]
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Rui Catarino
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,85 @@
1
+ Metadata-Version: 2.2
2
+ Name: pyttings
3
+ Version: 0.1.0
4
+ Summary: Python settings management with namespacing and modular files. Inspired by Django.
5
+ Home-page: https://github.com/ruitcatarino/pyttings
6
+ Author: Rui Catarino
7
+ Requires-Python: >=3.12
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Dynamic: author
11
+ Dynamic: home-page
12
+
13
+ # Pyttings
14
+ Lightweight Python settings management with namespacing and modular files. Inspired by Django.
15
+
16
+ ## Features
17
+
18
+ - **Namespaced Settings**: Use a prefix (e.g., `PYTTING_`) to avoid conflicts.
19
+ - **Custom Prefix**: Change the prefix using `PYTTING_ENV_PREFIX`.
20
+ - **Modular Settings**: Load settings from a module with `PYTTING_SETTINGS_MODULE`.
21
+ - **Environment Variables**: Override settings easily.
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ pip install pyttings
27
+ ```
28
+
29
+ ## Quick Start
30
+
31
+ 1. **Define Your Settings Module** (`myapp/settings.py`):
32
+ ```python
33
+ DEBUG = True
34
+ DATABASE_URL = "sqlite:///db.sqlite3"
35
+ SECRET_KEY = "my-secret-key"
36
+ ```
37
+ 2. **Set the Settings Module**:
38
+ ```bash
39
+ export PYTTING_SETTINGS_MODULE="myapp.settings"
40
+ ```
41
+ 3. **Use Pyttings**:
42
+ ```python
43
+ from pyttings import settings
44
+
45
+ print(settings.DEBUG) # Output: True
46
+ print(settings.DATABASE_URL) # Output: sqlite:///db.sqlite3
47
+ ```
48
+
49
+ ## Configuration
50
+
51
+ ### Required: `PYTTING_SETTINGS_MODULE`
52
+
53
+ Specify the settings module using the `PYTTING_SETTINGS_MODULE` environment variable. This is mandatory for Pyttings to know where to load your settings from.
54
+
55
+ ```bash
56
+ export PYTTING_SETTINGS_MODULE="myapp.settings"
57
+ ```
58
+ ### Optional: `PYTTING_ENV_PREFIX`
59
+ By default, Pyttings uses `PYTTING_` as the prefix for environment variables. For example, to override the `DEBUG` setting, you would set:
60
+ ```bash
61
+ export PYTTING_DEBUG="False"
62
+ ```
63
+ If you want to use a custom prefix (e.g., `MYAPP_`), set the `PYTTING_ENV_PREFIX` environment variable:
64
+ ```bash
65
+ export PYTTING_ENV_PREFIX="MYAPP_"
66
+ export MYAPP_DEBUG="False"
67
+ ```
68
+ Now, Pyttings will look for `MYAPP_DEBUG` instead of `PYTTING_DEBUG`.
69
+
70
+ ## Contributing
71
+
72
+ Contributions are welcome! If you'd like to contribute to Pyttings, please follow these steps:
73
+
74
+ 1. Fork the repository on [GitHub](https://github.com/ruitcatarino/pyttings).
75
+ 2. Create a new branch for your feature or bugfix.
76
+ 3. Make your changes and ensure tests pass.
77
+ 4. Submit a pull request with a clear description of your changes.
78
+
79
+ Please ensure your code follows the project's style and includes appropriate tests.
80
+
81
+ ---
82
+
83
+ ## License
84
+
85
+ Pyttings is licensed under the **MIT License**. See the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,11 @@
1
+ pyttings/__init__.py,sha256=nSnDdMHhY5ISF_pNZU1LeSZzObonibIo-HrYq_Wo7oc,74
2
+ pyttings/core.py,sha256=QrVlsanKHCczBYayYRcNTmPjgA5c9eC5atqcgVITlDQ,1941
3
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ tests/conftest.py,sha256=2ngqsza1TbIaJh0577C8eFF-OwEi365hzp0UkuB7RNQ,161
5
+ tests/settings.py,sha256=-DNTeSn7DHAZ6V_xFd7Q7q52SCJfCmp5NqcwI6PxJ7Q,135
6
+ tests/test_core.py,sha256=VsCayrRyDUyv96NQrAjq5uqvUftIyXRIexG7S0IZzSc,1757
7
+ pyttings-0.1.0.dist-info/LICENSE,sha256=_RB5Hj8p2JyvlKArKoWW9fjjYTbA8mK74MWBIMExJkc,1069
8
+ pyttings-0.1.0.dist-info/METADATA,sha256=na0SmtC2D6fohtzjfkA16MixMZgrGwLTjYbG-KNASfo,2514
9
+ pyttings-0.1.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
10
+ pyttings-0.1.0.dist-info/top_level.txt,sha256=HTyqSUpjO8LaEfEM69tKFsA0IlkjiPKhb5WJOqDuow4,15
11
+ pyttings-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.8.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ pyttings
2
+ tests
tests/__init__.py ADDED
File without changes
tests/conftest.py ADDED
@@ -0,0 +1,6 @@
1
+ import os
2
+
3
+
4
+ # Set the PYTTING_SETTINGS_MODULE environment variable globally
5
+ def pytest_configure():
6
+ os.environ["PYTTING_SETTINGS_MODULE"] = "tests.settings"
tests/settings.py ADDED
@@ -0,0 +1,6 @@
1
+ # tests/settings.py
2
+ DEBUG = True
3
+ DATABASE_URL = "sqlite:///db.sqlite3"
4
+ SECRET_KEY = "my-secret-key"
5
+ PORT = 8000
6
+ ENABLE_FEATURE = False
tests/test_core.py ADDED
@@ -0,0 +1,60 @@
1
+ import os
2
+
3
+ import pytest
4
+
5
+ from pyttings import settings
6
+ from pyttings.core import Settings
7
+
8
+
9
+ @pytest.fixture(autouse=True)
10
+ def clear_settings_cache():
11
+ settings._cache = {}
12
+
13
+
14
+ # Test settings initialization
15
+ def test_settings_initialization():
16
+ assert settings.DEBUG is True
17
+ assert settings.DATABASE_URL == "sqlite:///db.sqlite3"
18
+ assert settings.SECRET_KEY == "my-secret-key"
19
+
20
+
21
+ # Test environment variable overrides
22
+ def test_env_var_overrides():
23
+ os.environ["PYTTING_DEBUG"] = "False"
24
+ os.environ["PYTTING_PORT"] = "8080"
25
+ print(os.environ["PYTTING_DEBUG"])
26
+ print(settings._cache)
27
+ assert settings.DEBUG is False # Overridden by environment variable
28
+ assert settings.PORT == 8080 # Overridden and converted to int
29
+
30
+
31
+ # Test missing settings module
32
+ def test_missing_settings_module():
33
+ if "PYTTING_SETTINGS_MODULE" in os.environ:
34
+ del os.environ["PYTTING_SETTINGS_MODULE"]
35
+ with pytest.raises(
36
+ ValueError, match="PYTTING_SETTINGS_MODULE environment variable is not set"
37
+ ):
38
+ Settings()
39
+
40
+
41
+ # Test missing attribute
42
+ def test_missing_attribute():
43
+ with pytest.raises(AttributeError, match="has no attribute 'MISSING_SETTING'"):
44
+ _ = settings.MISSING_SETTING
45
+
46
+
47
+ # Test environment variable type conversion
48
+ def test_env_var_type_conversion():
49
+ os.environ["PYTTING_ENABLE_FEATURE"] = "True"
50
+ assert settings.ENABLE_FEATURE is True # Converted to bool
51
+
52
+
53
+ # Test custom prefix
54
+ def test_custom_prefix():
55
+ os.environ["PYTTING_ENV_PREFIX"] = "TEST_"
56
+ os.environ["PYTTING_SETTINGS_MODULE"] = "tests.settings"
57
+ new_settings = Settings()
58
+ assert new_settings._env_prefix == "TEST_"
59
+ assert new_settings.DEBUG is True
60
+ assert new_settings.DATABASE_URL == "sqlite:///db.sqlite3"