hyper-bot 0.0.1__py3-none-any.whl
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.
- cfgr/__init__.py +3 -0
- cfgr/manager.py +93 -0
- cfgr/reader.py +37 -0
- hyper_bot-0.0.1.dist-info/METADATA +8 -0
- hyper_bot-0.0.1.dist-info/RECORD +7 -0
- hyper_bot-0.0.1.dist-info/WHEEL +5 -0
- hyper_bot-0.0.1.dist-info/top_level.txt +1 -0
cfgr/__init__.py
ADDED
cfgr/manager.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
from abc import ABC
|
|
2
|
+
from typing import Self, Optional
|
|
3
|
+
|
|
4
|
+
from .utils import ProInjector, BaseMappingV, Mapping
|
|
5
|
+
from .reader import JsonReader, YamlReader, BaseReader
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Serializers(Mapping):
|
|
9
|
+
JSON: BaseMappingV = JsonReader
|
|
10
|
+
YAML: BaseMappingV = YamlReader
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class BaseConfig(ABC):
|
|
14
|
+
_loaded_cfgs: dict = {}
|
|
15
|
+
_anns: dict
|
|
16
|
+
reader: BaseReader = None
|
|
17
|
+
|
|
18
|
+
def __init__(self, **kwargs):
|
|
19
|
+
self.__custom_pre(**kwargs)
|
|
20
|
+
|
|
21
|
+
injector = ProInjector(self)
|
|
22
|
+
for i in kwargs:
|
|
23
|
+
if i in list(self._anns.keys()):
|
|
24
|
+
injector.inject(i, kwargs[i], self._anns[i])
|
|
25
|
+
|
|
26
|
+
self.__custom_post(**kwargs)
|
|
27
|
+
|
|
28
|
+
def __init_subclass__(cls, **kwargs):
|
|
29
|
+
cls._anns = cls.__annotations__
|
|
30
|
+
|
|
31
|
+
def __custom_pre(self, **kwargs):
|
|
32
|
+
pass
|
|
33
|
+
|
|
34
|
+
def __custom_post(self, **kwargs):
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
def __dump_pre(self):
|
|
38
|
+
pass
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def load_from(cls, file_name: str, serializer: BaseMappingV, key: Optional[str] = None) -> Self:
|
|
42
|
+
cls.reader: BaseReader = serializer.value(file_name)
|
|
43
|
+
cfg = cls(**(cls.reader.read()))
|
|
44
|
+
cls._loaded_cfgs[key] = cfg
|
|
45
|
+
return cfg
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def create(cls) -> dict:
|
|
49
|
+
cfg = {}
|
|
50
|
+
for i in cls._anns:
|
|
51
|
+
if cls._anns.get(i) is str:
|
|
52
|
+
cfg[i] = ""
|
|
53
|
+
elif cls._anns.get(i) in [int, float]:
|
|
54
|
+
cfg[i] = 0
|
|
55
|
+
elif cls._anns.get(i) is list:
|
|
56
|
+
cfg[i] = []
|
|
57
|
+
elif cls._anns.get(i) is dict:
|
|
58
|
+
cfg[i] = {}
|
|
59
|
+
else:
|
|
60
|
+
if issubclass(cls._anns.get(i), BaseConfig):
|
|
61
|
+
cfg[i] = cls._anns.get(i).create()
|
|
62
|
+
|
|
63
|
+
return cfg
|
|
64
|
+
|
|
65
|
+
@classmethod
|
|
66
|
+
def create_and_write(cls, file_name: str, serializer: BaseMappingV) -> None:
|
|
67
|
+
reader: BaseReader = serializer.value(file_name)
|
|
68
|
+
reader.write(cls.create())
|
|
69
|
+
|
|
70
|
+
@classmethod
|
|
71
|
+
def get(cls, key: str):
|
|
72
|
+
if key in cls._loaded_cfgs:
|
|
73
|
+
return cls._loaded_cfgs[key]
|
|
74
|
+
else:
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
def dump(self) -> dict:
|
|
78
|
+
cfg = {}
|
|
79
|
+
for i in self._anns:
|
|
80
|
+
if self._anns.get(i) in [str, int, float, list, dict]:
|
|
81
|
+
cfg[i] = self.__dict__.get(i)
|
|
82
|
+
else:
|
|
83
|
+
if issubclass(self._anns.get(i), BaseConfig):
|
|
84
|
+
cfg[i] = self.__dict__.get(i).dump()
|
|
85
|
+
|
|
86
|
+
return cfg
|
|
87
|
+
|
|
88
|
+
def write(self, file_name: Optional[str] = None, serializer: Optional[BaseMappingV] = None) -> None:
|
|
89
|
+
if self.reader is not None:
|
|
90
|
+
self.reader.write(self.dump())
|
|
91
|
+
else:
|
|
92
|
+
reader: BaseReader = serializer.value(file_name)
|
|
93
|
+
reader.write(self.dump())
|
cfgr/reader.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import yaml
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BaseReader(ABC):
|
|
8
|
+
def __init__(self, file: str):
|
|
9
|
+
self.file = file
|
|
10
|
+
|
|
11
|
+
@abstractmethod
|
|
12
|
+
def read(self) -> Any:
|
|
13
|
+
...
|
|
14
|
+
|
|
15
|
+
@abstractmethod
|
|
16
|
+
def write(self, data: Any) -> None:
|
|
17
|
+
...
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class JsonReader(BaseReader):
|
|
21
|
+
def read(self) -> dict:
|
|
22
|
+
with open(self.file, "r", encoding="utf-8") as f:
|
|
23
|
+
return json.load(f)
|
|
24
|
+
|
|
25
|
+
def write(self, data: Any) -> None:
|
|
26
|
+
with open(self.file, "w", encoding="utf-8") as f:
|
|
27
|
+
json.dump(data, f)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class YamlReader(BaseReader):
|
|
31
|
+
def read(self) -> dict:
|
|
32
|
+
with open(self.file, "r", encoding="utf-8") as f:
|
|
33
|
+
return yaml.load(f, Loader=yaml.FullLoader)
|
|
34
|
+
|
|
35
|
+
def write(self, data: Any) -> None:
|
|
36
|
+
with open(self.file, "w", encoding="utf-8") as f:
|
|
37
|
+
yaml.dump(data, f)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
cfgr/__init__.py,sha256=hOpxkECmVyqUi1Cy9P7nuhZC5jh17oBtvEJ-unMMyEw,20
|
|
2
|
+
cfgr/manager.py,sha256=3giRlNwJHouQYMG5ybq1R4xIgx3DTYJlsi8_nhEcgMw,2676
|
|
3
|
+
cfgr/reader.py,sha256=gvTImkV146fPHoOp1Cxsx7jiLkKImKpu8D2Eo_394MM,885
|
|
4
|
+
hyper_bot-0.0.1.dist-info/METADATA,sha256=vO3uvElmc2mmcJ225yo0k05s4a41xTfGb0zBy6PUfEk,212
|
|
5
|
+
hyper_bot-0.0.1.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
|
6
|
+
hyper_bot-0.0.1.dist-info/top_level.txt,sha256=nKIDEYIJvkb33IoYNRJfg34i9yOldAmmOCz3Ru9mBhA,5
|
|
7
|
+
hyper_bot-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cfgr
|