-
Notifications
You must be signed in to change notification settings - Fork 15
/
protocols.py
83 lines (56 loc) · 2 KB
/
protocols.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
78
79
80
81
82
83
from __future__ import annotations
from abc import abstractmethod
from os import PathLike
from pathlib import Path
from typing import Any, Protocol, TypeVar
__all__ = ["XmlSerializable", "DictSerializable"]
X = TypeVar("X", bound="XmlSerializable")
class XmlSerializable(Protocol):
@classmethod
@abstractmethod
def from_xml_str(cls: type[X], xml_str: str, **kwargs) -> X:
"""Imports a utility function from a GENIUS XML string.
Args:
xml_str (str): The string containing GENIUS style XML utility function definition
Returns:
A utility function object (depending on the input file)
"""
@abstractmethod
def to_xml_str(self, **kwargs) -> str:
"""Exports a utility function to a well formatted string"""
@classmethod
def from_genius(cls: type[X], file_name: PathLike, **kwargs) -> X:
"""Imports a utility function from a GENIUS XML file.
Args:
file_name (str): File name to import from
Returns:
A utility function object (depending on the input file)
"""
with open(file_name) as f:
s = f.read()
return cls.from_xml_str(s, **kwargs)
def to_genius(self, file_name: PathLike, **kwargs) -> None:
"""
Exports a utility function to a GENIUS XML file.
Args:
file_name (str): File name to export to
Returns:
None
Remarks:
See ``to_xml_str`` for all the parameters
"""
file_name = Path(file_name).absolute()
if file_name.suffix == "":
file_name = file_name.parent / f"{file_name.stem}.xml"
with open(file_name, "w") as f:
f.write(self.to_xml_str(**kwargs))
D = TypeVar("D", bound="DictSerializable")
class DictSerializable(Protocol):
def to_dict(self) -> str:
...
@classmethod
def from_dict(cls: type[D], v: dict[str, Any]) -> D:
...
class HasMinMax(Protocol):
min_value: Any
max_value: Any