-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_parser.py
77 lines (63 loc) · 2.84 KB
/
format_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import io
import json
from pathlib import Path
from typing import Any, TypeVar
from pydantic import BaseModel
import yaml
class MyDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(MyDumper, self).increase_indent(flow, False)
T = TypeVar("T", bound=BaseModel)
def load_yaml(model: type[T], source: str | io.StringIO | Path) -> T:
if isinstance(source, str) or isinstance(source, Path):
with open(source, "r", encoding="utf8") as fd:
return model(**yaml.load(fd, Loader=yaml.CLoader))
else:
return model(**yaml.load(source, Loader=yaml.CLoader))
def load_json(model: type[T], source: str | io.StringIO | Path) -> T:
if isinstance(source, str) or isinstance(source, Path):
with open(source, "r", encoding="utf8") as fd:
return model(**json.load(fd))
else:
return model(**json.load(source))
def save_yaml(model: BaseModel, distinction: str | io.StringIO | Path, dumper: str | Any = MyDumper):
if isinstance(dumper, str):
if dumper.lower() == 'cdumper':
dumper = yaml.CDumper
elif dumper.lower() == 'mydumper':
dumper = MyDumper
else:
if hasattr(yaml, dumper):
dumper = getattr(yaml, dumper)
if isinstance(distinction, str) or isinstance(distinction, Path):
with open(distinction, "w", encoding="utf8") as fd:
yaml.dump(model.dict(), fd, sort_keys=False, allow_unicode=True, Dumper=dumper, encoding="utf8")
else:
yaml.dump(model.dict(), distinction, sort_keys=False, allow_unicode=True, Dumper=dumper, encoding="utf8")
def save_json(model: BaseModel, distinction: str | io.StringIO | Path):
if isinstance(distinction, str) or isinstance(distinction, Path):
with open(distinction, "w", encoding="utf8") as fd:
json.dump(model.dict(), fd, sort_keys=False, ensure_ascii=False, indent=2)
else:
json.dump(model.dict(), distinction, sort_keys=False, ensure_ascii=False, indent=2)
class FormatParser:
@staticmethod
def load(model: type[T], source: str | io.StringIO | Path) -> T:
raise NotImplementedError()
@staticmethod
def save(model: T, distinction: str | io.StringIO | Path) -> T:
raise NotImplementedError()
class JsonParser(FormatParser):
@staticmethod
def load(model: type[T], source: str | io.StringIO | Path) -> T:
return load_json(model, source)
@staticmethod
def save(model: T, distinction: str | io.StringIO | Path) -> T:
return save_json(model, distinction)
class YamlParser(FormatParser):
@staticmethod
def load(model: type[T], source: str | io.StringIO | Path) -> T:
return load_yaml(model, source)
@staticmethod
def save(model: T, distinction: str | io.StringIO | Path) -> T:
return save_yaml(model, distinction)