comprehensiveconfig 1.0.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.
- comprehensiveconfig-1.0.0/LICENSE.txt +65 -0
- comprehensiveconfig-1.0.0/PKG-INFO +102 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig/__init__.py +153 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig/configio.py +42 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig/json.py +50 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig/spec.py +696 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig/toml.py +156 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig.egg-info/PKG-INFO +102 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig.egg-info/SOURCES.txt +12 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig.egg-info/dependency_links.txt +1 -0
- comprehensiveconfig-1.0.0/comprehensiveconfig.egg-info/top_level.txt +1 -0
- comprehensiveconfig-1.0.0/pyproject.toml +49 -0
- comprehensiveconfig-1.0.0/readme.md +80 -0
- comprehensiveconfig-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
2
|
+
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
|
6
|
+
|
|
7
|
+
Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below.
|
|
10
|
+
0. Additional Definitions.
|
|
11
|
+
|
|
12
|
+
As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License.
|
|
13
|
+
|
|
14
|
+
“The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below.
|
|
15
|
+
|
|
16
|
+
An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library.
|
|
17
|
+
|
|
18
|
+
A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”.
|
|
19
|
+
|
|
20
|
+
The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version.
|
|
21
|
+
|
|
22
|
+
The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work.
|
|
23
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
24
|
+
|
|
25
|
+
You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL.
|
|
26
|
+
2. Conveying Modified Versions.
|
|
27
|
+
|
|
28
|
+
If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version:
|
|
29
|
+
|
|
30
|
+
a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or
|
|
31
|
+
b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy.
|
|
32
|
+
|
|
33
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
34
|
+
|
|
35
|
+
The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following:
|
|
36
|
+
|
|
37
|
+
a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License.
|
|
38
|
+
b) Accompany the object code with a copy of the GNU GPL and this license document.
|
|
39
|
+
|
|
40
|
+
4. Combined Works.
|
|
41
|
+
|
|
42
|
+
You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following:
|
|
43
|
+
|
|
44
|
+
a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License.
|
|
45
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license document.
|
|
46
|
+
c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document.
|
|
47
|
+
d) Do one of the following:
|
|
48
|
+
0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.
|
|
49
|
+
1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version.
|
|
50
|
+
e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.)
|
|
51
|
+
|
|
52
|
+
5. Combined Libraries.
|
|
53
|
+
|
|
54
|
+
You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following:
|
|
55
|
+
|
|
56
|
+
a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License.
|
|
57
|
+
b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work.
|
|
58
|
+
|
|
59
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
60
|
+
|
|
61
|
+
The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
|
|
62
|
+
|
|
63
|
+
Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation.
|
|
64
|
+
|
|
65
|
+
If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: comprehensiveconfig
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A library to create ergonomic, auto-validated configuration models with great support for static type annotations.
|
|
5
|
+
License: LGPL-3.0-only
|
|
6
|
+
Project-URL: Homepage, https://summersweet.software
|
|
7
|
+
Project-URL: Repository, https://github.com/summersweet-software/comprehensiveconfig
|
|
8
|
+
Project-URL: Issues, https://github.com/summersweet-software/comprehensiveconfig/issues
|
|
9
|
+
Keywords: config,configuration,validation,validator,ergonomic,models,model,pydantic-like,toml,toml-writer,json,json-writer,type-annotated,type-annotations
|
|
10
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
|
|
18
|
+
Requires-Python: >=3.12
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE.txt
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# Comprehensive Configuration
|
|
24
|
+
A simple configuration library that lets you create a pydantic-like model for your configuration.
|
|
25
|
+
|
|
26
|
+
# Features
|
|
27
|
+
- [x] Supports static type checking
|
|
28
|
+
- [x] toml writer
|
|
29
|
+
- [x] json writer
|
|
30
|
+
- [x] Number Fields
|
|
31
|
+
- [x] Text Fields (with regex filtering)
|
|
32
|
+
- [x] List fields
|
|
33
|
+
- [x] Table fields
|
|
34
|
+
- [x] TableSpec (Model) fields
|
|
35
|
+
- [x] Sections
|
|
36
|
+
- [x] Include doc comments in Section
|
|
37
|
+
- [x] auto loading
|
|
38
|
+
- [x] initialize default config (with auto loader)
|
|
39
|
+
- [ ] yaml writer
|
|
40
|
+
- [x] section list (via a Table field)
|
|
41
|
+
- [x] Field type unions (overwriting normal union syntax)
|
|
42
|
+
- [x] per attribute doc comments (inline and noninline)
|
|
43
|
+
- [x] enum support (Via `spec.ConfigEnum` and python's `enum.Enum`)
|
|
44
|
+
- [x] fully supported string escapes
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
# Example (Python)
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
# example.py
|
|
51
|
+
from comprehensiveconfig import ConfigSpec
|
|
52
|
+
from comprehensiveconfig.spec import Section, Integer, Float, Text, List
|
|
53
|
+
from comprehensiveconfig.toml import TomlWriter
|
|
54
|
+
|
|
55
|
+
class MyConfigSpec(ConfigSpec,
|
|
56
|
+
default_file="myconfig.toml",
|
|
57
|
+
writer=TomlWriter,
|
|
58
|
+
create_file=True):
|
|
59
|
+
class MySection(Section, name="My_Section"):
|
|
60
|
+
some_field = Integer(10)
|
|
61
|
+
other_field = Text("Some Default Text")
|
|
62
|
+
|
|
63
|
+
class SubSection(Section):
|
|
64
|
+
'''An example Sub Section'''
|
|
65
|
+
x = Integer(10)
|
|
66
|
+
|
|
67
|
+
class Credentials(Section):
|
|
68
|
+
'''Example credentials section'''
|
|
69
|
+
email = Text("example@email.com", regex=r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")
|
|
70
|
+
password = Text("MyPassword")
|
|
71
|
+
|
|
72
|
+
some_field = Float(6.9)
|
|
73
|
+
list_of_stuff = List(["12", "13", "14"], inner_type=Text())
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
# Accessing values from globally loaded config (if one exists, otherwise it accesses the actual Field class)
|
|
77
|
+
print(MyConfigSpec.some_field)
|
|
78
|
+
print(MyConfigSpec.MySection.other_field)
|
|
79
|
+
|
|
80
|
+
# Another way to open configuration
|
|
81
|
+
second_config = MyConfigSpec.load("another_config.toml", TomlWriter)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
# Example (toml output)
|
|
85
|
+
|
|
86
|
+
```toml
|
|
87
|
+
some_field = 6.9
|
|
88
|
+
list_of_stuff = ["12", "13", "14"]
|
|
89
|
+
|
|
90
|
+
[My_Section]
|
|
91
|
+
some_field = 10
|
|
92
|
+
other_field = "Some Default Text"
|
|
93
|
+
|
|
94
|
+
[My_Section.SubSection]
|
|
95
|
+
# An example Sub Section
|
|
96
|
+
x = 10
|
|
97
|
+
|
|
98
|
+
[Credentials]
|
|
99
|
+
# Example credentials section
|
|
100
|
+
email = "example@email.com"
|
|
101
|
+
password = "MyPassword"
|
|
102
|
+
```
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
from abc import ABCMeta
|
|
2
|
+
import os
|
|
3
|
+
from typing import Any, Self, Type, Union
|
|
4
|
+
|
|
5
|
+
from . import configio
|
|
6
|
+
from . import spec
|
|
7
|
+
from .json import JsonWriter
|
|
8
|
+
from .toml import TomlWriter
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _ConfigSpecMeta(type):
|
|
12
|
+
"""handles getting of attributes"""
|
|
13
|
+
|
|
14
|
+
_WRITER: Type[configio.ConfigurationWriter] | None
|
|
15
|
+
_DEFAULT_FILE: str | None
|
|
16
|
+
_AUTO_LOAD: bool
|
|
17
|
+
_CREATE_FILE: bool
|
|
18
|
+
_INST: Union["ConfigSpec", None]
|
|
19
|
+
|
|
20
|
+
def __new__(
|
|
21
|
+
cls,
|
|
22
|
+
name,
|
|
23
|
+
bases,
|
|
24
|
+
attrs,
|
|
25
|
+
default_file: str | None = None, # automatically load a specific file
|
|
26
|
+
writer=None,
|
|
27
|
+
create_file: bool = False, # create the default file if not exists
|
|
28
|
+
auto_load: bool = True,
|
|
29
|
+
**kwargs,
|
|
30
|
+
):
|
|
31
|
+
cls._DEFAULT_FILE = default_file
|
|
32
|
+
cls._WRITER = writer
|
|
33
|
+
cls._INST = None
|
|
34
|
+
cls._CREATE_FILE = create_file
|
|
35
|
+
cls._AUTO_LOAD = auto_load
|
|
36
|
+
return super().__new__(cls, name, bases, attrs)
|
|
37
|
+
|
|
38
|
+
def __get__(self, instance, owner):
|
|
39
|
+
if self._INST is None:
|
|
40
|
+
return self
|
|
41
|
+
return self._INST
|
|
42
|
+
|
|
43
|
+
def __getattribute__(self, name):
|
|
44
|
+
"""get attributes from active instance if available"""
|
|
45
|
+
inst = object.__getattribute__(self, "_INST")
|
|
46
|
+
if inst is not None and name in inst._ALL_FIELDS.keys():
|
|
47
|
+
return inst.__getattribute__(name)
|
|
48
|
+
|
|
49
|
+
return super().__getattribute__(name)
|
|
50
|
+
|
|
51
|
+
def __setattribute__(self, name, value):
|
|
52
|
+
"""set attributes from active instance if available"""
|
|
53
|
+
inst = object.__getattribute__(self, "_INST")
|
|
54
|
+
if inst is not None and name in inst._ALL_FIELDS.keys():
|
|
55
|
+
return inst.__setattribute__(name, value)
|
|
56
|
+
|
|
57
|
+
return super().__setattr__(name, value)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class _ConfigSpecABCMeta(spec.ConfigurationFieldABCMeta, _ConfigSpecMeta):
|
|
61
|
+
"""A combination of ABCMeta and config spec meta"""
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class ConfigSpec(spec.Section, metaclass=_ConfigSpecABCMeta):
|
|
65
|
+
|
|
66
|
+
@classmethod
|
|
67
|
+
def __init_subclass__(
|
|
68
|
+
cls,
|
|
69
|
+
**kwargs,
|
|
70
|
+
):
|
|
71
|
+
super().__init_subclass__(**kwargs)
|
|
72
|
+
if not cls._AUTO_LOAD:
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
if cls._WRITER is None or cls._DEFAULT_FILE is None:
|
|
76
|
+
raise ValueError("config writer is not specified")
|
|
77
|
+
|
|
78
|
+
exists = os.path.exists(cls._DEFAULT_FILE)
|
|
79
|
+
if exists and not os.path.isfile(cls._DEFAULT_FILE):
|
|
80
|
+
raise Exception(f"configuration file: {cls._DEFAULT_FILE} is not a file")
|
|
81
|
+
|
|
82
|
+
if exists:
|
|
83
|
+
cls._INST = cls(cls._WRITER.load(cls._DEFAULT_FILE))
|
|
84
|
+
if not exists and cls._CREATE_FILE:
|
|
85
|
+
default = cls()
|
|
86
|
+
cls._WRITER.dump(cls._DEFAULT_FILE, default)
|
|
87
|
+
cls._INST = default
|
|
88
|
+
if not exists and not cls._CREATE_FILE:
|
|
89
|
+
raise FileNotFoundError(cls._DEFAULT_FILE)
|
|
90
|
+
|
|
91
|
+
def __init__(self, value: dict[str, Any] | None = None, /):
|
|
92
|
+
if value is None and not self._has_default:
|
|
93
|
+
raise Exception("Configuration does not have a full default value")
|
|
94
|
+
|
|
95
|
+
super().__init__(value or self._default_value)
|
|
96
|
+
|
|
97
|
+
@classmethod
|
|
98
|
+
def load(cls, file=None, writer=None, /) -> Self:
|
|
99
|
+
file = file or cls._DEFAULT_FILE
|
|
100
|
+
writer = writer or cls._WRITER
|
|
101
|
+
|
|
102
|
+
if writer is None:
|
|
103
|
+
raise ValueError("no writer specified")
|
|
104
|
+
if file is None:
|
|
105
|
+
raise Exception("No file specified")
|
|
106
|
+
|
|
107
|
+
return cls(writer.load(file))
|
|
108
|
+
|
|
109
|
+
def save(self, file=None, writer=None, /):
|
|
110
|
+
file = file or self._DEFAULT_FILE
|
|
111
|
+
writer = writer or self._WRITER
|
|
112
|
+
|
|
113
|
+
if writer is None:
|
|
114
|
+
raise ValueError("no writer specified")
|
|
115
|
+
if file is None:
|
|
116
|
+
raise Exception("No file specified")
|
|
117
|
+
|
|
118
|
+
writer.dump(file, self)
|
|
119
|
+
|
|
120
|
+
def reset(self):
|
|
121
|
+
"""This does not work on auto loaded config"""
|
|
122
|
+
if not self._has_default:
|
|
123
|
+
raise Exception("Configuration does not have a full default value")
|
|
124
|
+
self._value = {
|
|
125
|
+
self._FIELD_NAME_MAP[name]: self._ALL_FIELDS[self._FIELD_NAME_MAP[name]](
|
|
126
|
+
val
|
|
127
|
+
)
|
|
128
|
+
for name, val in self._default_value.items()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def reset_global(cls):
|
|
133
|
+
"""reset config on auto loaded config"""
|
|
134
|
+
if cls._INST is None:
|
|
135
|
+
return
|
|
136
|
+
|
|
137
|
+
if not cls._has_default:
|
|
138
|
+
raise Exception("Configuration does not have a full default value")
|
|
139
|
+
cls._INST._value = {
|
|
140
|
+
cls._FIELD_NAME_MAP[name]: cls._ALL_FIELDS[cls._FIELD_NAME_MAP[name]](val)
|
|
141
|
+
for name, val in cls._default_value.items()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
__all__ = [
|
|
146
|
+
"ConfigSpec",
|
|
147
|
+
"_ConfigSpecMeta",
|
|
148
|
+
"_ConfigSpecABCMeta",
|
|
149
|
+
"spec",
|
|
150
|
+
"configio",
|
|
151
|
+
"JsonWriter",
|
|
152
|
+
"TomlWriter",
|
|
153
|
+
]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from . import spec
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class ConfigurationWriter(ABC):
|
|
9
|
+
"""simple writer/reader base class that jus
|
|
10
|
+
expects a similar interface to python's builtin json module"""
|
|
11
|
+
|
|
12
|
+
@classmethod
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def dumps(cls, node: spec.AnyConfigField) -> str:
|
|
15
|
+
"""dump Configuration tree/node as a string"""
|
|
16
|
+
pass
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def dump(cls, file, node: spec.AnyConfigField):
|
|
21
|
+
if isinstance(file, (str, Path)):
|
|
22
|
+
with open(file, "w") as f:
|
|
23
|
+
f.write(cls.dumps(node))
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
file.write(cls.dumps(node))
|
|
27
|
+
|
|
28
|
+
@classmethod
|
|
29
|
+
@abstractmethod
|
|
30
|
+
def loads(cls, file) -> dict[str, Any]:
|
|
31
|
+
"""load a file by name"""
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
@classmethod
|
|
35
|
+
@abstractmethod
|
|
36
|
+
def load(cls, file) -> dict[str, Any]:
|
|
37
|
+
"""load a file by name"""
|
|
38
|
+
if isinstance(file, (str, Path)):
|
|
39
|
+
with open(file, "r") as f:
|
|
40
|
+
return cls.loads(f.read())
|
|
41
|
+
|
|
42
|
+
return cls.loads(file.read())
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from . import configio
|
|
3
|
+
from . import spec
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class JsonWriter(configio.ConfigurationWriter):
|
|
7
|
+
@classmethod
|
|
8
|
+
def dump_section(cls, node: spec.Section):
|
|
9
|
+
return {
|
|
10
|
+
node._FIELD_VAR_MAP[name]: (
|
|
11
|
+
cls.dump_value(node.get_field(name), value)
|
|
12
|
+
)
|
|
13
|
+
for name, value in node._value.items()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def dump_value(cls, node: spec.AnyConfigField, value):
|
|
18
|
+
match node:
|
|
19
|
+
case type() | spec.Section():
|
|
20
|
+
return cls.dump_section(value)
|
|
21
|
+
case spec.Table(_, type() | spec.Section() | spec.ConfigUnion()):
|
|
22
|
+
return {cls.dump_value(key, key): cls.dump_value(val, val) for key, val in value.items()}
|
|
23
|
+
case spec.ConfigEnum(_, True):
|
|
24
|
+
return value.name
|
|
25
|
+
case spec.ConfigEnum(_, False):
|
|
26
|
+
return value.value
|
|
27
|
+
case _:
|
|
28
|
+
return value
|
|
29
|
+
|
|
30
|
+
@classmethod
|
|
31
|
+
def dumps(cls, node) -> str:
|
|
32
|
+
match node:
|
|
33
|
+
case spec.Section():
|
|
34
|
+
return json.dumps(cls.dump_section(node), indent=4)
|
|
35
|
+
case _:
|
|
36
|
+
raise ValueError(node)
|
|
37
|
+
|
|
38
|
+
@classmethod
|
|
39
|
+
def dump(cls, file, node):
|
|
40
|
+
super().dump(file, node)
|
|
41
|
+
|
|
42
|
+
@classmethod
|
|
43
|
+
def load(cls, file):
|
|
44
|
+
if isinstance(file, str):
|
|
45
|
+
with open(file, "r") as f:
|
|
46
|
+
return json.load(f)
|
|
47
|
+
return json.load(file)
|
|
48
|
+
|
|
49
|
+
# just alias the name
|
|
50
|
+
loads = json.loads
|