python-simpleconf 0.6.2__tar.gz → 0.7.0__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.
Files changed (21) hide show
  1. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/PKG-INFO +3 -1
  2. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/README.md +2 -0
  3. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/pyproject.toml +1 -1
  4. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/__init__.py +1 -1
  5. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/caster.py +3 -4
  6. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/loaders/__init__.py +7 -8
  7. python_simpleconf-0.7.0/simpleconf/loaders/dict.py +19 -0
  8. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/loaders/env.py +14 -6
  9. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/loaders/ini.py +12 -4
  10. python_simpleconf-0.7.0/simpleconf/loaders/json.py +28 -0
  11. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/loaders/osenv.py +3 -3
  12. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/loaders/toml.py +15 -8
  13. python_simpleconf-0.7.0/simpleconf/loaders/yaml.py +30 -0
  14. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/utils.py +30 -7
  15. python_simpleconf-0.6.2/simpleconf/loaders/dict.py +0 -12
  16. python_simpleconf-0.6.2/simpleconf/loaders/json.py +0 -21
  17. python_simpleconf-0.6.2/simpleconf/loaders/yaml.py +0 -23
  18. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/LICENSE +0 -0
  19. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/config.py +0 -0
  20. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/exceptions.py +0 -0
  21. {python_simpleconf-0.6.2 → python_simpleconf-0.7.0}/simpleconf/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: python-simpleconf
3
- Version: 0.6.2
3
+ Version: 0.7.0
4
4
  Summary: Simple configuration management with python.
5
5
  License: MIT
6
6
  Author: pwwang
@@ -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
 
@@ -4,7 +4,7 @@ build-backend = "poetry.masonry.api"
4
4
 
5
5
  [tool.poetry]
6
6
  name = "python-simpleconf"
7
- version = "0.6.2"
7
+ version = "0.7.0"
8
8
  description = "Simple configuration management with python."
9
9
  authors = [ "pwwang <pwwang@pwwang.com>",]
10
10
  license = "MIT"
@@ -1,3 +1,3 @@
1
1
  from .config import Config, ProfileConfig
2
2
 
3
- __version__ = "0.6.2"
3
+ __version__ = "0.7.0"
@@ -1,12 +1,11 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import json
4
- from typing import Any, Callable, Sequence, TYPE_CHECKING
4
+ from typing import Any, Callable, Dict, Sequence, TypeVar
5
5
  from ast import literal_eval
6
6
 
7
7
 
8
- if TYPE_CHECKING:
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: Diot, casters: Sequence[Callable]) -> Diot:
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):
@@ -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 TYPE_CHECKING, Any, Callable, List
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) -> "Diot":
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) -> "Diot":
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
- return cast(loaded, self.__class__.CASTERS)
42
- return loaded
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) -> Diot:
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 Diot(dotenv.dotenv_values(stream=io.StringIO(content)))
42
+ return dotenv.dotenv_values(stream=io.StringIO(content))
43
43
 
44
44
  if not self._exists(conf, ignore_nonexist):
45
- return Diot()
45
+ return {}
46
46
 
47
- return Diot(dotenv.main.DotEnv(conf).dict())
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) -> Diot:
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 Diot(default={})
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) -> Diot:
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 = Diot()
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) -> Diot:
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 Diot(toml.loads(content))
25
+ return toml.loads(content)
27
26
 
28
27
  if not self._exists(conf, ignore_nonexist):
29
- return Diot()
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 Diot(toml.load(f))
32
+ return toml.load(f)
34
33
 
35
34
  with open(conf, "r") as f: # rtoml
36
- return Diot(toml.load(f))
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,12 +0,0 @@
1
- from typing import Any
2
- from diot import Diot
3
-
4
- from . import Loader
5
-
6
-
7
- class DictLoader(Loader):
8
- """Dict loader"""
9
-
10
- def loading(self, conf: Any, ignore_nonexist: bool) -> Diot:
11
- """Load the configuration from a dict"""
12
- return Diot(conf)
@@ -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))