python-simpleconf 0.6.2__tar.gz → 0.7.1__tar.gz
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.
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/PKG-INFO +5 -3
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/README.md +2 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/pyproject.toml +5 -5
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/__init__.py +1 -1
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/caster.py +3 -4
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/config.py +38 -8
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/loaders/__init__.py +7 -8
- python_simpleconf-0.7.1/simpleconf/loaders/dict.py +19 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/loaders/env.py +14 -6
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/loaders/ini.py +12 -4
- python_simpleconf-0.7.1/simpleconf/loaders/json.py +28 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/loaders/osenv.py +3 -3
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/loaders/toml.py +15 -8
- python_simpleconf-0.7.1/simpleconf/loaders/yaml.py +30 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/utils.py +30 -7
- python_simpleconf-0.6.2/simpleconf/loaders/dict.py +0 -12
- python_simpleconf-0.6.2/simpleconf/loaders/json.py +0 -21
- python_simpleconf-0.6.2/simpleconf/loaders/yaml.py +0 -23
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/LICENSE +0 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/exceptions.py +0 -0
- {python_simpleconf-0.6.2 → python_simpleconf-0.7.1}/simpleconf/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: python-simpleconf
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.1
|
|
4
4
|
Summary: Simple configuration management with python.
|
|
5
5
|
License: MIT
|
|
6
6
|
Author: pwwang
|
|
@@ -18,9 +18,9 @@ Provides-Extra: env
|
|
|
18
18
|
Provides-Extra: ini
|
|
19
19
|
Provides-Extra: toml
|
|
20
20
|
Provides-Extra: yaml
|
|
21
|
-
Requires-Dist: diot (>=0.3.
|
|
21
|
+
Requires-Dist: diot (>=0.3.2,<0.4.0)
|
|
22
22
|
Requires-Dist: iniconfig (>=2.0,<3.0) ; extra == "ini" or extra == "all"
|
|
23
|
-
Requires-Dist: python-dotenv (>=1.
|
|
23
|
+
Requires-Dist: python-dotenv (>=1.1,<2.0) ; extra == "env" or extra == "all"
|
|
24
24
|
Requires-Dist: pyyaml (>=6,<7) ; extra == "yaml" or extra == "all"
|
|
25
25
|
Requires-Dist: rtoml (>=0.12,<0.13) ; (sys_platform == "linux") and (extra == "toml" or extra == "all")
|
|
26
26
|
Requires-Dist: tomli (>=2.0,<3.0) ; (sys_platform != "linux") and (extra == "toml" or extra == "all")
|
|
@@ -99,6 +99,8 @@ conf = Config.load({'a': 1, 'b': {'c': 2}})
|
|
|
99
99
|
- `XXX.osenv`: System environment variables with prefix `XXX_` (case-sensitive) is used.
|
|
100
100
|
- `XXX_A=1` will be loaded as `conf.A = 1`.
|
|
101
101
|
- python dictionary.
|
|
102
|
+
- Strings format of the above formats are also supported.
|
|
103
|
+
- `"{'a': 1}"` will be loaded as `conf.a = 1`.
|
|
102
104
|
|
|
103
105
|
### Profile support
|
|
104
106
|
|
|
@@ -69,6 +69,8 @@ conf = Config.load({'a': 1, 'b': {'c': 2}})
|
|
|
69
69
|
- `XXX.osenv`: System environment variables with prefix `XXX_` (case-sensitive) is used.
|
|
70
70
|
- `XXX_A=1` will be loaded as `conf.A = 1`.
|
|
71
71
|
- python dictionary.
|
|
72
|
+
- Strings format of the above formats are also supported.
|
|
73
|
+
- `"{'a': 1}"` will be loaded as `conf.a = 1`.
|
|
72
74
|
|
|
73
75
|
### Profile support
|
|
74
76
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = [ "poetry>=
|
|
2
|
+
requires = [ "poetry>=2",]
|
|
3
3
|
build-backend = "poetry.masonry.api"
|
|
4
4
|
|
|
5
5
|
[tool.poetry]
|
|
6
6
|
name = "python-simpleconf"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.7.1"
|
|
8
8
|
description = "Simple configuration management with python."
|
|
9
9
|
authors = [ "pwwang <pwwang@pwwang.com>",]
|
|
10
10
|
license = "MIT"
|
|
@@ -17,8 +17,8 @@ packages = [
|
|
|
17
17
|
|
|
18
18
|
[tool.poetry.dependencies]
|
|
19
19
|
python = "^3.9"
|
|
20
|
-
diot = "^0.3.
|
|
21
|
-
python-dotenv = { version="^1.
|
|
20
|
+
diot = "^0.3.2"
|
|
21
|
+
python-dotenv = { version="^1.1", optional = true}
|
|
22
22
|
pyyaml = { version="^6", optional = true}
|
|
23
23
|
# Use rtoml only when the wheel is available (linux)
|
|
24
24
|
rtoml = {version = "^0.12", optional = true, platform = "linux"}
|
|
@@ -32,7 +32,7 @@ yaml = [ "pyyaml"]
|
|
|
32
32
|
toml = [ "rtoml", "tomli"]
|
|
33
33
|
all = [ "iniconfig", "python-dotenv", "pyyaml", "rtoml", "tomli"]
|
|
34
34
|
|
|
35
|
-
[tool.poetry.dev
|
|
35
|
+
[tool.poetry.group.dev.dependencies]
|
|
36
36
|
pytest = "^8"
|
|
37
37
|
pytest-cov = "^6"
|
|
38
38
|
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
|
-
from typing import Any, Callable, Sequence,
|
|
4
|
+
from typing import Any, Callable, Dict, Sequence, TypeVar
|
|
5
5
|
from ast import literal_eval
|
|
6
6
|
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
from diot import Diot
|
|
8
|
+
T = TypeVar("T", bound=Dict[str, Any])
|
|
10
9
|
|
|
11
10
|
|
|
12
11
|
def type_caster(prefix: str, cast_fun: Callable) -> Any:
|
|
@@ -77,7 +76,7 @@ def cast_value(value: Any, casters: Sequence[Callable]) -> Any:
|
|
|
77
76
|
return value
|
|
78
77
|
|
|
79
78
|
|
|
80
|
-
def cast(conf:
|
|
79
|
+
def cast(conf: T, casters: Sequence[Callable]) -> T:
|
|
81
80
|
"""Cast the configuration"""
|
|
82
81
|
for key, value in conf.items():
|
|
83
82
|
if isinstance(value, dict):
|
|
@@ -87,6 +87,8 @@ class ProfileConfig:
|
|
|
87
87
|
*configs: Any,
|
|
88
88
|
loader: LoaderType | Sequence[LoaderType] = None,
|
|
89
89
|
ignore_nonexist: bool = False,
|
|
90
|
+
base: str = "default",
|
|
91
|
+
allow_missing_base: bool = False,
|
|
90
92
|
) -> Diot:
|
|
91
93
|
"""Load the configuration from the files, or other configurations
|
|
92
94
|
|
|
@@ -98,6 +100,10 @@ class ProfileConfig:
|
|
|
98
100
|
same length as configs.
|
|
99
101
|
ignore_nonexist: Whether to ignore non-existent files
|
|
100
102
|
Otherwise, will raise errors
|
|
103
|
+
base: The default profile to use after loading
|
|
104
|
+
allow_missing_base: Whether to allow missing base profile
|
|
105
|
+
If False, will raise errors when the base profile is not found
|
|
106
|
+
in the loaded profiles.
|
|
101
107
|
"""
|
|
102
108
|
if not isinstance(loader, Sequence) or isinstance(loader, str):
|
|
103
109
|
loader = [loader] * len(configs)
|
|
@@ -132,7 +138,12 @@ class ProfileConfig:
|
|
|
132
138
|
pool.setdefault(profile, Diot())
|
|
133
139
|
pool[profile].update_recursively(value)
|
|
134
140
|
|
|
135
|
-
|
|
141
|
+
if base and base not in pool and not allow_missing_base:
|
|
142
|
+
raise ValueError(f"Base profile '{base}' not found")
|
|
143
|
+
|
|
144
|
+
if base and base in pool:
|
|
145
|
+
ProfileConfig.use_profile(out, base, base=base)
|
|
146
|
+
|
|
136
147
|
return out
|
|
137
148
|
|
|
138
149
|
@staticmethod
|
|
@@ -140,6 +151,8 @@ class ProfileConfig:
|
|
|
140
151
|
conf: Any,
|
|
141
152
|
loader: str | Loader | None = None,
|
|
142
153
|
ignore_nonexist: bool = False,
|
|
154
|
+
base: str = "default",
|
|
155
|
+
allow_missing_base: bool = False,
|
|
143
156
|
) -> Diot:
|
|
144
157
|
"""Load the configuration from the file
|
|
145
158
|
|
|
@@ -148,6 +161,7 @@ class ProfileConfig:
|
|
|
148
161
|
loader: The loader to use. Will detect from conf by default
|
|
149
162
|
ignore_nonexist: Whether to ignore non-existent files
|
|
150
163
|
Otherwise, will raise errors
|
|
164
|
+
base: The default profile to use after loading
|
|
151
165
|
|
|
152
166
|
Returns:
|
|
153
167
|
A Diot object with the loaded configuration
|
|
@@ -175,7 +189,12 @@ class ProfileConfig:
|
|
|
175
189
|
pool.setdefault(profile, Diot())
|
|
176
190
|
pool[profile].update_recursively(value)
|
|
177
191
|
|
|
178
|
-
|
|
192
|
+
if base and base not in pool and not allow_missing_base:
|
|
193
|
+
raise ValueError(f"Base profile '{base}' not found")
|
|
194
|
+
|
|
195
|
+
if base and base in pool:
|
|
196
|
+
ProfileConfig.use_profile(out, base, base=base)
|
|
197
|
+
|
|
179
198
|
return out
|
|
180
199
|
|
|
181
200
|
@staticmethod
|
|
@@ -184,24 +203,35 @@ class ProfileConfig:
|
|
|
184
203
|
profile: str,
|
|
185
204
|
base: str = "default",
|
|
186
205
|
copy: bool = False,
|
|
206
|
+
allow_missing_base: bool = False,
|
|
187
207
|
) -> Diot:
|
|
188
208
|
"""Switch the configuration to the given profile, based on the
|
|
189
|
-
|
|
209
|
+
base profile.
|
|
190
210
|
|
|
191
211
|
Args:
|
|
192
212
|
conf: The configuration object by the `load` function
|
|
193
213
|
profile: The profile to use
|
|
194
|
-
|
|
214
|
+
base: The base profile.
|
|
215
|
+
If it does not exist, no base profile will be used.
|
|
216
|
+
copy: Whether to return a new configuration object
|
|
217
|
+
If False, the given configuration object will be updated
|
|
218
|
+
in-place.
|
|
219
|
+
allow_missing_base: Whether to allow missing base profile
|
|
220
|
+
If False, will raise errors when the base profile is not found
|
|
221
|
+
in the loaded profiles.
|
|
195
222
|
|
|
196
223
|
Returns:
|
|
197
224
|
The configuration object with the switched profile if copy is True
|
|
198
225
|
Otherwise None (updated in-place)
|
|
199
226
|
"""
|
|
200
227
|
pool = conf[POOL_KEY]
|
|
228
|
+
if base and base not in pool and not allow_missing_base:
|
|
229
|
+
raise ValueError(f"Base profile '{base}' not found")
|
|
230
|
+
|
|
201
231
|
if copy:
|
|
202
232
|
out = Diot({POOL_KEY: pool, META_KEY: conf[META_KEY].copy()})
|
|
203
|
-
if base is not None:
|
|
204
|
-
out.update_recursively(pool
|
|
233
|
+
if base is not None and base != profile:
|
|
234
|
+
out.update_recursively(pool.get(base, {}))
|
|
205
235
|
out[META_KEY]["current_profile"] = profile
|
|
206
236
|
out[META_KEY]["base_profile"] = base
|
|
207
237
|
out.update_recursively(pool[profile])
|
|
@@ -213,8 +243,8 @@ class ProfileConfig:
|
|
|
213
243
|
continue
|
|
214
244
|
del conf[key]
|
|
215
245
|
|
|
216
|
-
if base is not None:
|
|
217
|
-
conf.update_recursively(pool
|
|
246
|
+
if base is not None and base != profile:
|
|
247
|
+
conf.update_recursively(pool.get(base, {}))
|
|
218
248
|
conf.update_recursively(pool[profile])
|
|
219
249
|
conf[META_KEY]["current_profile"] = profile
|
|
220
250
|
conf[META_KEY]["base_profile"] = base
|
|
@@ -3,20 +3,18 @@ from __future__ import annotations
|
|
|
3
3
|
from abc import ABC, abstractmethod
|
|
4
4
|
from os import PathLike
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from typing import
|
|
6
|
+
from typing import Any, Callable, List, Dict
|
|
7
7
|
|
|
8
|
+
from diot import Diot
|
|
8
9
|
from ..caster import cast
|
|
9
10
|
|
|
10
|
-
if TYPE_CHECKING:
|
|
11
|
-
from diot import Diot
|
|
12
|
-
|
|
13
11
|
|
|
14
12
|
class Loader(ABC):
|
|
15
13
|
|
|
16
14
|
CASTERS: List[Callable[[str, bool], Any]] | None = None
|
|
17
15
|
|
|
18
16
|
@abstractmethod
|
|
19
|
-
def loading(self, conf: Any, ignore_nonexist: bool) ->
|
|
17
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
20
18
|
"""Load the configuration from the path or configurations"""
|
|
21
19
|
|
|
22
20
|
def _exists(self, conf: PathLike, ignore_exist: bool) -> bool:
|
|
@@ -26,7 +24,7 @@ class Loader(ABC):
|
|
|
26
24
|
raise FileNotFoundError(f"{conf} does not exist")
|
|
27
25
|
return path.exists()
|
|
28
26
|
|
|
29
|
-
def load(self, conf: Any, ignore_nonexist: bool = False) ->
|
|
27
|
+
def load(self, conf: Any, ignore_nonexist: bool = False) -> Diot:
|
|
30
28
|
"""Load the configuration from the path or configurations and cast
|
|
31
29
|
values
|
|
32
30
|
|
|
@@ -38,7 +36,8 @@ class Loader(ABC):
|
|
|
38
36
|
"""
|
|
39
37
|
loaded = self.loading(conf, ignore_nonexist)
|
|
40
38
|
if self.__class__.CASTERS:
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
loaded = cast(loaded, self.__class__.CASTERS)
|
|
40
|
+
|
|
41
|
+
return Diot(loaded)
|
|
43
42
|
|
|
44
43
|
load_with_profiles = load
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from typing import Any, Dict
|
|
2
|
+
|
|
3
|
+
from . import Loader
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class DictLoader(Loader):
|
|
7
|
+
"""Dict loader"""
|
|
8
|
+
|
|
9
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
10
|
+
"""Load the configuration from a dict"""
|
|
11
|
+
return conf
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class DictsLoader(DictLoader):
|
|
15
|
+
"""Dict string loader"""
|
|
16
|
+
|
|
17
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
18
|
+
"""Load the configuration from a dict"""
|
|
19
|
+
return eval(conf)
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import warnings
|
|
2
2
|
import io
|
|
3
3
|
from pathlib import Path
|
|
4
|
-
from typing import Any
|
|
5
|
-
from simpleconf.utils import require_package
|
|
4
|
+
from typing import Any, Dict
|
|
6
5
|
from diot import Diot
|
|
7
6
|
|
|
7
|
+
from ..utils import require_package
|
|
8
8
|
from ..caster import (
|
|
9
9
|
cast,
|
|
10
10
|
int_caster,
|
|
@@ -35,16 +35,16 @@ class EnvLoader(Loader):
|
|
|
35
35
|
toml_caster,
|
|
36
36
|
]
|
|
37
37
|
|
|
38
|
-
def loading(self, conf: Any, ignore_nonexist: bool = False) ->
|
|
38
|
+
def loading(self, conf: Any, ignore_nonexist: bool = False) -> Dict[str, Any]:
|
|
39
39
|
"""Load the configuration from a .env file"""
|
|
40
40
|
if hasattr(conf, "read"):
|
|
41
41
|
content = conf.read()
|
|
42
|
-
return
|
|
42
|
+
return dotenv.dotenv_values(stream=io.StringIO(content))
|
|
43
43
|
|
|
44
44
|
if not self._exists(conf, ignore_nonexist):
|
|
45
|
-
return
|
|
45
|
+
return {}
|
|
46
46
|
|
|
47
|
-
return
|
|
47
|
+
return dotenv.main.DotEnv(conf).dict()
|
|
48
48
|
|
|
49
49
|
def load_with_profiles( # type: ignore[override]
|
|
50
50
|
self,
|
|
@@ -65,3 +65,11 @@ class EnvLoader(Loader):
|
|
|
65
65
|
out.setdefault(profile, Diot())[key] = v
|
|
66
66
|
|
|
67
67
|
return cast(out, self.__class__.CASTERS)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class EnvsLoader(EnvLoader):
|
|
71
|
+
"""Env string loader"""
|
|
72
|
+
|
|
73
|
+
def loading(self, conf: Any, ignore_nonexist: bool = False) -> Dict[str, Any]:
|
|
74
|
+
"""Load the configuration from a .env file"""
|
|
75
|
+
return dotenv.dotenv_values(stream=io.StringIO(conf))
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import warnings
|
|
4
|
-
from typing import Any
|
|
4
|
+
from typing import Any, Dict
|
|
5
5
|
from pathlib import Path
|
|
6
|
-
from simpleconf.utils import require_package
|
|
7
6
|
from diot import Diot
|
|
8
7
|
|
|
8
|
+
from ..utils import require_package
|
|
9
9
|
from ..caster import (
|
|
10
10
|
cast,
|
|
11
11
|
int_caster,
|
|
@@ -36,14 +36,14 @@ class IniLoader(Loader):
|
|
|
36
36
|
toml_caster,
|
|
37
37
|
]
|
|
38
38
|
|
|
39
|
-
def loading(self, conf: Any, ignore_nonexist: bool) ->
|
|
39
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
40
40
|
"""Load the configuration from an ini-like file"""
|
|
41
41
|
if hasattr(conf, "read"):
|
|
42
42
|
content = conf.read()
|
|
43
43
|
return iniconfig.IniConfig("<config>", content).sections
|
|
44
44
|
|
|
45
45
|
if not self._exists(conf, ignore_nonexist):
|
|
46
|
-
return
|
|
46
|
+
return {"default": {}}
|
|
47
47
|
|
|
48
48
|
return iniconfig.IniConfig(conf).sections
|
|
49
49
|
|
|
@@ -83,3 +83,11 @@ class IniLoader(Loader):
|
|
|
83
83
|
for k, v in sections.items():
|
|
84
84
|
out[k.lower()] = cast(v, self.__class__.CASTERS)
|
|
85
85
|
return out
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class InisLoader(IniLoader):
|
|
89
|
+
"""Ini-like string loader"""
|
|
90
|
+
|
|
91
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
92
|
+
"""Load the configuration from an ini-like file"""
|
|
93
|
+
return iniconfig.IniConfig("<config>", conf).sections
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Any, Dict
|
|
3
|
+
|
|
4
|
+
from . import Loader
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class JsonLoader(Loader):
|
|
8
|
+
"""Json file loader"""
|
|
9
|
+
|
|
10
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
11
|
+
"""Load the configuration from a json file"""
|
|
12
|
+
if hasattr(conf, "read"):
|
|
13
|
+
content = conf.read()
|
|
14
|
+
return json.loads(content)
|
|
15
|
+
|
|
16
|
+
if not self._exists(conf, ignore_nonexist):
|
|
17
|
+
return {}
|
|
18
|
+
|
|
19
|
+
with open(conf) as f:
|
|
20
|
+
return json.load(f)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class JsonsLoader(JsonLoader):
|
|
24
|
+
"""Json string loader"""
|
|
25
|
+
|
|
26
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
27
|
+
"""Load the configuration from a json file"""
|
|
28
|
+
return json.loads(conf)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from os import environ
|
|
2
|
-
from typing import Any
|
|
2
|
+
from typing import Any, Dict
|
|
3
3
|
import warnings
|
|
4
4
|
|
|
5
5
|
from diot import Diot
|
|
@@ -32,11 +32,11 @@ class OsenvLoader(Loader):
|
|
|
32
32
|
toml_caster,
|
|
33
33
|
]
|
|
34
34
|
|
|
35
|
-
def loading(self, conf: Any, ignore_nonexist: bool = False) ->
|
|
35
|
+
def loading(self, conf: Any, ignore_nonexist: bool = False) -> Dict[str, Any]:
|
|
36
36
|
"""Load the configuration from environment variables"""
|
|
37
37
|
prefix = f"{conf[:-6]}_" if len(conf) > 6 else ""
|
|
38
38
|
len_prefix = len(prefix)
|
|
39
|
-
out =
|
|
39
|
+
out = {}
|
|
40
40
|
for k, v in environ.items():
|
|
41
41
|
if k.startswith(prefix):
|
|
42
42
|
out[k[len_prefix:]] = v
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
from typing import Any
|
|
2
|
-
from simpleconf.utils import require_package
|
|
3
|
-
from diot import Diot
|
|
1
|
+
from typing import Any, Dict
|
|
4
2
|
|
|
3
|
+
from ..utils import require_package
|
|
5
4
|
from ..caster import (
|
|
6
5
|
none_caster,
|
|
7
6
|
null_caster,
|
|
@@ -19,18 +18,26 @@ class TomlLoader(Loader):
|
|
|
19
18
|
null_caster,
|
|
20
19
|
]
|
|
21
20
|
|
|
22
|
-
def loading(self, conf: Any, ignore_nonexist: bool) ->
|
|
21
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
23
22
|
"""Load the configuration from a toml file"""
|
|
24
23
|
if hasattr(conf, "read"):
|
|
25
24
|
content = conf.read()
|
|
26
|
-
return
|
|
25
|
+
return toml.loads(content)
|
|
27
26
|
|
|
28
27
|
if not self._exists(conf, ignore_nonexist):
|
|
29
|
-
return
|
|
28
|
+
return {}
|
|
30
29
|
|
|
31
30
|
if toml.__name__ in ("tomli", "tomllib"): # pragma: no cover
|
|
32
31
|
with open(conf, "rb") as f:
|
|
33
|
-
return
|
|
32
|
+
return toml.load(f)
|
|
34
33
|
|
|
35
34
|
with open(conf, "r") as f: # rtoml
|
|
36
|
-
return
|
|
35
|
+
return toml.load(f)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TomlsLoader(TomlLoader):
|
|
39
|
+
"""Toml string loader"""
|
|
40
|
+
|
|
41
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
42
|
+
"""Load the configuration from a toml file"""
|
|
43
|
+
return toml.loads(conf)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from typing import Any, Dict
|
|
2
|
+
|
|
3
|
+
from . import Loader
|
|
4
|
+
from ..utils import require_package
|
|
5
|
+
|
|
6
|
+
yaml = require_package("yaml")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class YamlLoader(Loader):
|
|
10
|
+
"""Yaml file loader"""
|
|
11
|
+
|
|
12
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
13
|
+
"""Load the configuration from a yaml file"""
|
|
14
|
+
if hasattr(conf, "read"):
|
|
15
|
+
content = conf.read()
|
|
16
|
+
return yaml.load(content, Loader=yaml.FullLoader)
|
|
17
|
+
|
|
18
|
+
if not self._exists(conf, ignore_nonexist):
|
|
19
|
+
return {}
|
|
20
|
+
|
|
21
|
+
with open(conf) as f:
|
|
22
|
+
return yaml.load(f, Loader=yaml.FullLoader)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class YamlsLoader(YamlLoader):
|
|
26
|
+
"""Yaml string loader"""
|
|
27
|
+
|
|
28
|
+
def loading(self, conf: Any, ignore_nonexist: bool) -> Dict[str, Any]:
|
|
29
|
+
"""Load the configuration from a yaml file"""
|
|
30
|
+
return yaml.load(conf, Loader=yaml.FullLoader)
|
|
@@ -38,33 +38,56 @@ def get_loader(ext: str | Loader) -> Loader:
|
|
|
38
38
|
|
|
39
39
|
if ext == "dict":
|
|
40
40
|
from .loaders.dict import DictLoader
|
|
41
|
-
|
|
42
41
|
return DictLoader()
|
|
42
|
+
|
|
43
|
+
if ext == "dicts":
|
|
44
|
+
from .loaders.dict import DictsLoader
|
|
45
|
+
return DictsLoader()
|
|
46
|
+
|
|
43
47
|
if ext == "env":
|
|
44
48
|
from .loaders.env import EnvLoader
|
|
45
|
-
|
|
46
49
|
return EnvLoader()
|
|
50
|
+
|
|
51
|
+
if ext == "envs":
|
|
52
|
+
from .loaders.env import EnvsLoader
|
|
53
|
+
return EnvsLoader()
|
|
54
|
+
|
|
47
55
|
if ext == "ini":
|
|
48
56
|
from .loaders.ini import IniLoader
|
|
49
|
-
|
|
50
57
|
return IniLoader()
|
|
58
|
+
|
|
59
|
+
if ext == "inis":
|
|
60
|
+
from .loaders.ini import InisLoader
|
|
61
|
+
return InisLoader()
|
|
62
|
+
|
|
51
63
|
if ext == "json":
|
|
52
64
|
from .loaders.json import JsonLoader
|
|
53
|
-
|
|
54
65
|
return JsonLoader()
|
|
66
|
+
|
|
67
|
+
if ext == "jsons":
|
|
68
|
+
from .loaders.json import JsonsLoader
|
|
69
|
+
return JsonsLoader()
|
|
70
|
+
|
|
55
71
|
if ext == "osenv":
|
|
56
72
|
from .loaders.osenv import OsenvLoader
|
|
57
|
-
|
|
58
73
|
return OsenvLoader()
|
|
74
|
+
|
|
59
75
|
if ext == "toml":
|
|
60
76
|
from .loaders.toml import TomlLoader
|
|
61
|
-
|
|
62
77
|
return TomlLoader()
|
|
78
|
+
|
|
79
|
+
if ext == "tomls":
|
|
80
|
+
from .loaders.toml import TomlsLoader
|
|
81
|
+
return TomlsLoader()
|
|
82
|
+
|
|
63
83
|
if ext == "yaml":
|
|
64
84
|
from .loaders.yaml import YamlLoader
|
|
65
|
-
|
|
66
85
|
return YamlLoader()
|
|
67
86
|
|
|
87
|
+
if ext == "yamls":
|
|
88
|
+
from .loaders.yaml import YamlsLoader
|
|
89
|
+
return YamlsLoader()
|
|
90
|
+
|
|
68
91
|
raise FormatNotSupported(f"{ext} is not supported.")
|
|
69
92
|
|
|
70
93
|
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
from typing import Any
|
|
3
|
-
|
|
4
|
-
from diot import Diot
|
|
5
|
-
|
|
6
|
-
from . import Loader
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class JsonLoader(Loader):
|
|
10
|
-
"""Json file loader"""
|
|
11
|
-
|
|
12
|
-
def loading(self, conf: Any, ignore_nonexist: bool) -> Diot:
|
|
13
|
-
"""Load the configuration from a json file"""
|
|
14
|
-
if hasattr(conf, "read"):
|
|
15
|
-
content = conf.read()
|
|
16
|
-
return Diot(json.loads(content))
|
|
17
|
-
|
|
18
|
-
if not self._exists(conf, ignore_nonexist):
|
|
19
|
-
return Diot()
|
|
20
|
-
with open(conf) as f:
|
|
21
|
-
return Diot(json.load(f))
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
from typing import Any
|
|
2
|
-
from simpleconf.utils import require_package
|
|
3
|
-
from diot import Diot
|
|
4
|
-
|
|
5
|
-
from . import Loader
|
|
6
|
-
|
|
7
|
-
yaml = require_package("yaml")
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
class YamlLoader(Loader):
|
|
11
|
-
"""Yaml file loader"""
|
|
12
|
-
|
|
13
|
-
def loading(self, conf: Any, ignore_nonexist: bool) -> Diot:
|
|
14
|
-
"""Load the configuration from a yaml file"""
|
|
15
|
-
if hasattr(conf, "read"):
|
|
16
|
-
content = conf.read()
|
|
17
|
-
return Diot(yaml.load(content, Loader=yaml.FullLoader))
|
|
18
|
-
|
|
19
|
-
if not self._exists(conf, ignore_nonexist):
|
|
20
|
-
return Diot()
|
|
21
|
-
|
|
22
|
-
with open(conf) as f:
|
|
23
|
-
return Diot(yaml.load(f, Loader=yaml.FullLoader))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|