|
6 | 6 | from typing import get_origin
|
7 | 7 | from typing import Union
|
8 | 8 |
|
| 9 | +from dataconf.exceptions import EnvListOrderException |
9 | 10 | from dataconf.exceptions import MalformedConfigException
|
10 | 11 | from dataconf.exceptions import MissingTypeException
|
11 | 12 | from dataconf.exceptions import TypeConfigException
|
12 | 13 | from dataconf.exceptions import UnexpectedKeysException
|
13 | 14 | from dateutil.relativedelta import relativedelta
|
14 | 15 | from pyhocon import ConfigFactory
|
15 |
| -from pyhocon import HOCONConverter |
16 | 16 | from pyhocon.config_tree import ConfigList
|
17 | 17 | from pyhocon.config_tree import ConfigTree
|
18 | 18 | import pyparsing
|
19 | 19 |
|
| 20 | + |
20 | 21 | NoneType = type(None)
|
21 | 22 |
|
22 | 23 |
|
@@ -181,42 +182,74 @@ def __generate(value: object, path):
|
181 | 182 | return value
|
182 | 183 |
|
183 | 184 |
|
184 |
| -def load(file: str, clazz): |
185 |
| - try: |
186 |
| - conf = ConfigFactory.parse_file(file) |
187 |
| - return __parse(conf, clazz, "") |
188 |
| - except pyparsing.ParseSyntaxException as e: |
189 |
| - raise MalformedConfigException( |
190 |
| - f'parsing failure line {e.lineno} character {e.col}, got "{e.line}"' |
191 |
| - ) |
| 185 | +def __dict_list_parsing(prefix: str, obj): |
| 186 | + ret = {} |
192 | 187 |
|
| 188 | + def set_lens(p, focus, v): |
193 | 189 |
|
194 |
| -def loads(string: str, clazz): |
195 |
| - try: |
196 |
| - conf = ConfigFactory.parse_string(string) |
197 |
| - return __parse(conf, clazz, "") |
198 |
| - except pyparsing.ParseSyntaxException as e: |
199 |
| - raise MalformedConfigException( |
200 |
| - f'parsing failure line {e.lineno} character {e.col}, got "{e.line}"' |
201 |
| - ) |
| 190 | + # value |
| 191 | + if len(p) == 1: |
| 192 | + # []x |
| 193 | + if isinstance(focus, list): |
| 194 | + if p[0] != len(focus): |
| 195 | + raise EnvListOrderException |
| 196 | + focus.append(v) |
| 197 | + # {}x |
| 198 | + else: |
| 199 | + focus[p[0]] = v |
| 200 | + return |
| 201 | + |
| 202 | + # dict |
| 203 | + if p[1] == "": |
| 204 | + |
| 205 | + if p[0] not in focus: |
| 206 | + # []{x} |
| 207 | + if isinstance(focus, list): |
| 208 | + if p[0] != len(focus): |
| 209 | + raise EnvListOrderException |
| 210 | + focus.append({}) |
| 211 | + # {}{x} |
| 212 | + else: |
| 213 | + focus[p[0]] = {} |
202 | 214 |
|
| 215 | + return set_lens(p[2:], focus[p[0]], v) |
203 | 216 |
|
204 |
| -def dump(file: str, instance: object, out: str): |
205 |
| - with open(file, "w") as f: |
206 |
| - f.write(dumps(instance, out=out)) |
| 217 | + # list |
| 218 | + if isinstance(p[1], int): |
207 | 219 |
|
| 220 | + if p[0] not in focus: |
| 221 | + # [][x] |
| 222 | + if isinstance(focus, list): |
| 223 | + if p[1] != len(focus): |
| 224 | + raise EnvListOrderException |
| 225 | + focus.append([]) |
| 226 | + # {}[x] |
| 227 | + else: |
| 228 | + focus[p[0]] = [] |
208 | 229 |
|
209 |
| -def dumps(instance: object, out: str): |
210 |
| - conf = __generate(instance, "") |
| 230 | + return set_lens(p[1:], focus[p[0]], v) |
| 231 | + |
| 232 | + # compose path |
| 233 | + return set_lens([f"{p[0]}_{p[1]}"] + p[2:], focus, v) |
| 234 | + |
| 235 | + def int_or_string(v): |
| 236 | + try: |
| 237 | + return int(v) |
| 238 | + except ValueError: |
| 239 | + return v |
| 240 | + |
| 241 | + if not prefix.endswith("_"): |
| 242 | + prefix = f"{prefix}_" |
| 243 | + |
| 244 | + for k, v in sorted(obj.items(), key=lambda x: x[0]): |
| 245 | + if k.startswith(prefix): |
| 246 | + |
| 247 | + path = [int_or_string(e) for e in k[len(prefix) :].lower().split("_")] |
| 248 | + try: |
| 249 | + value = ConfigFactory.parse_string(v) |
| 250 | + except pyparsing.ParseSyntaxException: |
| 251 | + value = v |
211 | 252 |
|
212 |
| - if out: |
213 |
| - if out.lower() == "hocon": |
214 |
| - return HOCONConverter.to_hocon(conf) |
215 |
| - if out.lower() == "yaml": |
216 |
| - return HOCONConverter.to_yaml(conf) |
217 |
| - if out.lower() == "json": |
218 |
| - return HOCONConverter.to_json(conf) |
219 |
| - if out.lower() == "properties": |
220 |
| - return HOCONConverter.to_properties(conf) |
| 253 | + set_lens(path, ret, value) |
221 | 254 |
|
222 |
| - return conf |
| 255 | + return ret |
0 commit comments