mooch.settings 0.0.1.dev4__py3-none-any.whl → 1.0.1__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.
- mooch/settings/utils.py +29 -0
- {mooch_settings-0.0.1.dev4.dist-info → mooch_settings-1.0.1.dist-info}/METADATA +16 -9
- mooch_settings-1.0.1.dist-info/RECORD +10 -0
- mooch_settings-0.0.1.dev4.dist-info/RECORD +0 -10
- {mooch_settings-0.0.1.dev4.dist-info → mooch_settings-1.0.1.dist-info}/WHEEL +0 -0
- {mooch_settings-0.0.1.dev4.dist-info → mooch_settings-1.0.1.dist-info}/licenses/LICENSE +0 -0
- {mooch_settings-0.0.1.dev4.dist-info → mooch_settings-1.0.1.dist-info}/top_level.txt +0 -0
mooch/settings/utils.py
CHANGED
@@ -1,15 +1,37 @@
|
|
1
1
|
from __future__ import annotations
|
2
2
|
|
3
|
+
import re
|
3
4
|
from typing import Any
|
4
5
|
|
5
6
|
|
7
|
+
def is_valid_key(key: str) -> bool:
|
8
|
+
# Bare keys: a-z, A-Z, 0-9, _, -, no quotes or spaces
|
9
|
+
# No support for keys with nested quotes
|
10
|
+
return bool(re.fullmatch(r"[A-Za-z0-9_-]+", key))
|
11
|
+
|
12
|
+
|
6
13
|
def set_nested(d: dict, key: str, value: Any, sep: str = ".") -> None: # noqa: ANN401
|
7
14
|
"""Set a nested value in a dictionary by key."""
|
8
15
|
keys = key.split(sep)
|
9
16
|
for k in keys[:-1]:
|
17
|
+
if not is_valid_key(k):
|
18
|
+
error_msg = (
|
19
|
+
f"Invalid key: '{k}'. "
|
20
|
+
"Keys must be alphanumeric, underscores, or dashes, "
|
21
|
+
"and cannot contain spaces or quotes."
|
22
|
+
)
|
23
|
+
raise ValueError(error_msg)
|
24
|
+
|
10
25
|
if k not in d or not isinstance(d[k], dict):
|
11
26
|
d[k] = {}
|
12
27
|
d = d[k]
|
28
|
+
if not is_valid_key(keys[-1]):
|
29
|
+
error_msg = (
|
30
|
+
f"Invalid key: '{keys[-1]}'. "
|
31
|
+
"Keys must be alphanumeric, underscores, or dashes, "
|
32
|
+
"and cannot contain spaces or quotes."
|
33
|
+
)
|
34
|
+
raise ValueError(error_msg)
|
13
35
|
d[keys[-1]] = value
|
14
36
|
|
15
37
|
|
@@ -17,6 +39,13 @@ def get_nested(d: dict, key: str, sep: str = ".") -> Any | None: # noqa: ANN401
|
|
17
39
|
"""Get a nested value from a dictionary by key."""
|
18
40
|
keys = key.split(sep)
|
19
41
|
for k in keys:
|
42
|
+
if not is_valid_key(k):
|
43
|
+
error_msg = (
|
44
|
+
f"Invalid key: '{k}'. "
|
45
|
+
"Keys must be alphanumeric, underscores, or dashes, "
|
46
|
+
"and cannot contain spaces or quotes."
|
47
|
+
)
|
48
|
+
raise ValueError(error_msg)
|
20
49
|
if isinstance(d, dict) and k in d:
|
21
50
|
d = d[k]
|
22
51
|
else:
|
@@ -1,11 +1,11 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: mooch.settings
|
3
|
-
Version:
|
3
|
+
Version: 1.0.1
|
4
4
|
Summary: Python settings management package (mooch.settings). Uses a TOML file.
|
5
5
|
Author-email: Nick Stuer <nickstuer@duck.com>
|
6
6
|
Project-URL: Homepage, https://github.com/nickstuer/mooch.settings
|
7
7
|
Project-URL: Issues, https://github.com/nickstuer/mooch.settings/issues
|
8
|
-
Classifier: Development Status ::
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
9
9
|
Classifier: Programming Language :: Python :: 3
|
10
10
|
Classifier: License :: OSI Approved :: MIT License
|
11
11
|
Classifier: Operating System :: OS Independent
|
@@ -28,7 +28,9 @@ Dynamic: license-file
|
|
28
28
|
[](https://github.com/RichardLitt/standard-readme)
|
29
29
|
[](LICENSE)
|
30
30
|
|
31
|
-
|
31
|
+
A lightweight, TOML-backed configuration/settings utility that feels like a dictionary.
|
32
|
+
|
33
|
+
mooch.settings is a Python configuration library designed for simplicity and developer ergonomics. It loads settings data from TOML files and exposes them as standard Python dictionaries — allowing you to work with settings in a familiar, Python way.
|
32
34
|
|
33
35
|
## Table of Contents
|
34
36
|
|
@@ -40,9 +42,11 @@ This Python package is a collection of useful Python code that is commonly used
|
|
40
42
|
|
41
43
|
## 📖 Features
|
42
44
|
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
- TOML-powered: Uses toml under the hood for modern, human-friendly config files.
|
46
|
+
- Dictionary-like interface: Access and manipulate settings with regular dict operations.
|
47
|
+
- Nested access: Supports nested structures and dotted key notation.
|
48
|
+
- Safe defaults: Easily provide fallback values or defaults when keys are missing from the config file.
|
49
|
+
- Optional dynamic reload: Reloads config file everytime a key is read. (Enabled by default)
|
46
50
|
|
47
51
|
|
48
52
|
## 🛠 Install
|
@@ -61,17 +65,20 @@ Python 3.9 or greater
|
|
61
65
|
|
62
66
|
## 🎮 Usage
|
63
67
|
|
64
|
-
###
|
68
|
+
### Example
|
69
|
+
This will create/use a 'settings.toml' file located in the .mooch directory of the user's home directory.
|
65
70
|
```python
|
66
71
|
from mooch.settings import Settings
|
72
|
+
from pathlib import Path
|
73
|
+
|
67
74
|
default_settings = {
|
68
75
|
"settings": {
|
69
|
-
"name": "MyName,
|
76
|
+
"name": "MyName",
|
70
77
|
"mood": "happy",
|
71
78
|
},
|
72
79
|
}
|
73
80
|
|
74
|
-
settings = Settings("settings.toml", default_settings)
|
81
|
+
settings = Settings(Path.home() / ".mooch/settings.toml", default_settings)
|
75
82
|
|
76
83
|
print(settings["settings.mood"])
|
77
84
|
settings["settings.mood"] = "angry"
|
@@ -0,0 +1,10 @@
|
|
1
|
+
mooch/__init__.py,sha256=_ZVUSDxucTCJe9q2wP1tI31qEX3m4QGcH7kn3BRv6ic,112
|
2
|
+
mooch/settings/__init__.py,sha256=D5YbYh6gPIJXOwYlX0FDTRLDoUu0wfnJd1jdvh59ju0,83
|
3
|
+
mooch/settings/filehandler.py,sha256=X3_kFcCtoJt3yI5v_r4GEKaoQIpimlwFPQxYPP5UKPE,1503
|
4
|
+
mooch/settings/settings.py,sha256=zN6l-8amrrm66h8afOvifv-WdgObpHVlUIEzEs9mN3g,2603
|
5
|
+
mooch/settings/utils.py,sha256=sWemTFz_hhDjEJMXse76_jxT9W-N1Jgt5OhXV_Fs86A,1688
|
6
|
+
mooch_settings-1.0.1.dist-info/licenses/LICENSE,sha256=FU8uKPcPjgV4M_aaYQPSM5abrktGqLFX2VEUk9oBYIE,1067
|
7
|
+
mooch_settings-1.0.1.dist-info/METADATA,sha256=34U-C9qXRoslRyYym0JblTyOPMdemhsbVeLt18pPdxs,3963
|
8
|
+
mooch_settings-1.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
9
|
+
mooch_settings-1.0.1.dist-info/top_level.txt,sha256=BbCxYfdneRNeyhPyAVvt8IEDA_Px7BIm5zhH6hEmsAo,6
|
10
|
+
mooch_settings-1.0.1.dist-info/RECORD,,
|
@@ -1,10 +0,0 @@
|
|
1
|
-
mooch/__init__.py,sha256=_ZVUSDxucTCJe9q2wP1tI31qEX3m4QGcH7kn3BRv6ic,112
|
2
|
-
mooch/settings/__init__.py,sha256=D5YbYh6gPIJXOwYlX0FDTRLDoUu0wfnJd1jdvh59ju0,83
|
3
|
-
mooch/settings/filehandler.py,sha256=X3_kFcCtoJt3yI5v_r4GEKaoQIpimlwFPQxYPP5UKPE,1503
|
4
|
-
mooch/settings/settings.py,sha256=zN6l-8amrrm66h8afOvifv-WdgObpHVlUIEzEs9mN3g,2603
|
5
|
-
mooch/settings/utils.py,sha256=V-aFKlrLaDfuJed4Yn-1pxlIM8jzdr4xRrNflBxLfXc,665
|
6
|
-
mooch_settings-0.0.1.dev4.dist-info/licenses/LICENSE,sha256=FU8uKPcPjgV4M_aaYQPSM5abrktGqLFX2VEUk9oBYIE,1067
|
7
|
-
mooch_settings-0.0.1.dev4.dist-info/METADATA,sha256=saXexWmtLe3kfExxjGVRDIFLkhHuLG2pBVl46fp4MA4,3308
|
8
|
-
mooch_settings-0.0.1.dev4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
9
|
-
mooch_settings-0.0.1.dev4.dist-info/top_level.txt,sha256=BbCxYfdneRNeyhPyAVvt8IEDA_Px7BIm5zhH6hEmsAo,6
|
10
|
-
mooch_settings-0.0.1.dev4.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|