-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
67 lines (50 loc) · 1.68 KB
/
logic.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
from __future__ import annotations
import json
from typing import Any
from pydantic import BaseModel
class Note(BaseModel):
text: str
class Property(BaseModel):
name: str
generator: str
icon: str
value: str
generator_arguments: dict[str, Any]
@property
def str_arguments(self) -> str:
return json.dumps(self.generator_arguments, ensure_ascii=False, indent=4)
@str_arguments.setter
def str_arguments(self, s: str) -> None:
self.generator_arguments: dict[str, Any] = json.loads(s)
class Person(BaseModel):
properties: list[tuple[str, list[Property]]]
def __post_init__(self) -> None:
self.properties = [] if self.properties is None else self.properties
class XNWPProfile(BaseModel):
notes: list[tuple[str, list[Note]]]
persons: list[tuple[str, list[Person]]]
sample_properties: list[tuple[str, list[Property]]]
sample_persons: list[tuple[str, list[Person]]]
def saves(self) -> str:
return json.dumps(
self.dict(),
sort_keys=True,
indent=4,
ensure_ascii=False,
)
def savefile(self, path: str) -> None:
with open(path, "w", encoding="utf_8") as wf:
json.dump(
self.dict(),
wf,
sort_keys=True,
indent=4,
ensure_ascii=False,
)
@staticmethod
def loads(s: str) -> XNWPProfile:
return XNWPProfile.parse_obj(json.loads(s)) # type: ignore
@staticmethod
def loadfile(path: str) -> XNWPProfile:
with open(path, "r", encoding="utf_8") as rf:
return XNWPProfile.parse_obj(json.load(rf)) # type: ignore