configobjectify 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.
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from configparser import ConfigParser, SectionProxy
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Config:
|
|
7
|
+
def __init__(self, filepath: str | Path, section: str):
|
|
8
|
+
self.section: str = section.upper()
|
|
9
|
+
self.config_file = self._get_config_file(filepath)
|
|
10
|
+
self._parser: ConfigParser = self._get_parser()
|
|
11
|
+
self._data: SectionProxy = self._parse_to_dict
|
|
12
|
+
self._attr: Optional[str] = None
|
|
13
|
+
|
|
14
|
+
def _get_config_file(self, filename: str | Path) -> Path:
|
|
15
|
+
"""Validate the configuration file path and return it as a Path object.
|
|
16
|
+
|
|
17
|
+
Args:
|
|
18
|
+
filename: Path to the configuration file.
|
|
19
|
+
|
|
20
|
+
Returns:
|
|
21
|
+
A Path object representing the validated INI file.
|
|
22
|
+
|
|
23
|
+
Raises:
|
|
24
|
+
FileNotFoundError: If the path does not point to an existing file.
|
|
25
|
+
ValueError: If the file does not have an .ini extension.
|
|
26
|
+
"""
|
|
27
|
+
path = Path(filename)
|
|
28
|
+
if not path.is_file():
|
|
29
|
+
raise FileNotFoundError(f"{filename!r} not found")
|
|
30
|
+
if path.suffix.lower() != ".ini":
|
|
31
|
+
raise ValueError(f"Invalid file extension: {path.suffix!r}")
|
|
32
|
+
return path
|
|
33
|
+
|
|
34
|
+
def _get_parser(self) -> ConfigParser:
|
|
35
|
+
"""Return a configured `ConfigParser` instance.
|
|
36
|
+
The parser reads configuration data from `valid.ini`.
|
|
37
|
+
"""
|
|
38
|
+
parser = ConfigParser()
|
|
39
|
+
parser.read(self.config_file)
|
|
40
|
+
return parser
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def _parse_to_dict(self) -> SectionProxy:
|
|
44
|
+
"""Return the selected configuration section as a dictionary.
|
|
45
|
+
Each configuration key in the selected section is stored as a
|
|
46
|
+
key-value pair in the returned dictionary.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
A dictionary containing the configuration values for the
|
|
50
|
+
selected section.
|
|
51
|
+
|
|
52
|
+
Raises:
|
|
53
|
+
KeyError: If the selected section does not exist.
|
|
54
|
+
"""
|
|
55
|
+
if not self._parser.has_section(self.section):
|
|
56
|
+
raise KeyError(f"Invalid section {self.section!r}")
|
|
57
|
+
return self._parser[self.section]
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def value(self) -> str:
|
|
61
|
+
"""Return the value of the most recently accessed configuration key.
|
|
62
|
+
Returns:
|
|
63
|
+
The configuration value associated with the accessed key,
|
|
64
|
+
or an empty string if the key does not exist.
|
|
65
|
+
"""
|
|
66
|
+
return self._data.get(self._attr, "")
|
|
67
|
+
|
|
68
|
+
def __getattr__(self, name):
|
|
69
|
+
"""Capture access to an undefined attribute.
|
|
70
|
+
The attribute name is stored internally and the current
|
|
71
|
+
`Config` instance is returned, allowing configuration values
|
|
72
|
+
to be accessed through expressions such as `config.device_name.value`.
|
|
73
|
+
|
|
74
|
+
Args:
|
|
75
|
+
name: Name of the configuration attribute being accessed.
|
|
76
|
+
|
|
77
|
+
Returns:
|
|
78
|
+
The current `Config` instance.
|
|
79
|
+
"""
|
|
80
|
+
self._attr = name
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
def __repr__(self):
|
|
84
|
+
"""Return the string representation of the configuration object.
|
|
85
|
+
Returns:
|
|
86
|
+
A string containing the selected configuration section.
|
|
87
|
+
"""
|
|
88
|
+
return f'Config("{self.section}")'
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: configobjectify
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A lightweight Python library for converting Configuration Files into Python Objects.
|
|
5
|
+
Keywords: config-parsing,attribute-lookup,dot-access,config-files,config-ini
|
|
6
|
+
Author: Sandeep Suryaprasad
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Requires-Python: >=3.12
|
|
15
|
+
Project-URL: Homepage, https://github.com/sandeepsuryaprasad/configobjectify
|
|
16
|
+
Project-URL: Repository, https://github.com/sandeepsuryaprasad/configobjectify
|
|
17
|
+
Project-URL: Issues, https://github.com/sandeepsuryaprasad/configobjectify/issues
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
## configobjectify
|
|
21
|
+
|
|
22
|
+
[](https://github.com/sandeepsuryaprasad/configbjectify/actions/workflows/ci.yml)
|
|
23
|
+
[](https://pypi.org/project/configobjectify/)
|
|
24
|
+
[](https://pypi.org/project/configobjectify/)
|
|
25
|
+
|
|
26
|
+
A lightweight Python library that provides an object-oriented
|
|
27
|
+
interface for accessing values from INI configuration files. Instead of retrieving
|
|
28
|
+
values using dictionary-style or section-based lookups, you can access configuration
|
|
29
|
+
keys through dot notation.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
```console
|
|
33
|
+
pip install configobjectify
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
Consider the following `config.ini` file,
|
|
38
|
+
```console
|
|
39
|
+
[APPLE]
|
|
40
|
+
device_name = iPhone
|
|
41
|
+
model = iPhone 17
|
|
42
|
+
os_version = 26.0
|
|
43
|
+
|
|
44
|
+
[ANDROID]
|
|
45
|
+
device_name = Pixel
|
|
46
|
+
model = Pixel 10
|
|
47
|
+
os_version = 16
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```console
|
|
51
|
+
>>> from configobjectify import Config
|
|
52
|
+
>>> config = Config("config.ini", "APPLE")
|
|
53
|
+
>>> config.device_name.value
|
|
54
|
+
'iPhone'
|
|
55
|
+
>>> config.model.value
|
|
56
|
+
'iPhone 17'
|
|
57
|
+
>>> config.os_version.value
|
|
58
|
+
'26.0'
|
|
59
|
+
```
|
|
60
|
+
### Selecting a different section
|
|
61
|
+
```console
|
|
62
|
+
>>> config = Config("config.ini", "ANDROID")
|
|
63
|
+
>>> config.device_name.value
|
|
64
|
+
'Pixel'
|
|
65
|
+
>>> config.model.value
|
|
66
|
+
'Pixel 10'
|
|
67
|
+
>>> config.os_version.value
|
|
68
|
+
'16.0'
|
|
69
|
+
```
|
|
70
|
+
NOTE: Section names are normalized to uppercase by the library before lookup.
|
|
71
|
+
|
|
72
|
+
### Handling missing configuration keys
|
|
73
|
+
```console
|
|
74
|
+
>>> config = Config("config.ini", "APPLE")
|
|
75
|
+
>>> config.country_of_origin.value
|
|
76
|
+
>>> ''
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Installed version
|
|
80
|
+
To get to know the version of `configobjectify`,
|
|
81
|
+
```console
|
|
82
|
+
>>> import configobjectify
|
|
83
|
+
>>> print(configobjectify.__version__)
|
|
84
|
+
```
|
|
85
|
+
or
|
|
86
|
+
```console
|
|
87
|
+
>>> from configobjectify import __version__
|
|
88
|
+
>>> print(__version__)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Requirements
|
|
92
|
+
* Python 3.12 or later
|
|
93
|
+
|
|
94
|
+
### Contributing
|
|
95
|
+
Contributions, bug reports, and feature suggestions are welcome. Please open an issue to
|
|
96
|
+
discuss a proposed change before submitting a pull request.
|
|
97
|
+
|
|
98
|
+
### License
|
|
99
|
+
|
|
100
|
+
This package is distributed under the terms of [MIT](https://github.com/sandeepsuryaprasad/configobjectify/blob/main/LICENSE) license
|
|
101
|
+
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
configobjectify/__init__.py,sha256=hgMn80XcbK4ROwVVV-Ii-2F5-NwCEsIq5tHa4kxUGlQ,144
|
|
2
|
+
configobjectify/config.py,sha256=unHtvOSBUBvrfCdl5cgY3LfFp36xTQO3F5PeG7dcAGc,3099
|
|
3
|
+
configobjectify-0.1.0.dist-info/licenses/LICENSE,sha256=_d_Gwo-7FYsk-c6_vlvLIZTRy9byE2RSBxaCq_MUV4M,1076
|
|
4
|
+
configobjectify-0.1.0.dist-info/WHEEL,sha256=3NO1oYsF7mANFUWVdyEQLM-zmecczWWEmmF5rEbXrWk,80
|
|
5
|
+
configobjectify-0.1.0.dist-info/METADATA,sha256=iwmw7M5ZanWCthkF3J60OIgJXi69aBP7cqJAfY29itA,3076
|
|
6
|
+
configobjectify-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sandeep Suryaprasad
|
|
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.
|