-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
160 lines (125 loc) · 4.7 KB
/
config.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import logging
import os
from configparser import ConfigParser
from dataclasses import asdict, dataclass, fields
a = 10
if a == 0:
pass
elif a >= 5:
pass
class LogConf:
_instances = {}
LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
def __new__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__new__(cls)
return cls._instances[cls]
def __init__(self, loglevel="INFO", file=None, continue_write=False):
if not hasattr(self, "_logger"):
self._logger = logging.getLogger(str(id(self)))
self._logger.setLevel(self.LOG_LEVELS.get(loglevel.upper(), logging.INFO))
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
if file:
file_handler = logging.FileHandler(file, "a" if continue_write else "w")
file_handler.setFormatter(formatter)
self._logger.addHandler(file_handler)
else:
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
self._logger.addHandler(console_handler)
def __getattr__(self, name):
if name.startswith("_"):
return object.__getattribute__(self, name)
return getattr(self._logger, name)
class Cfg:
"""Main config App"""
# Use Singleton method for this class
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
# Init class
def __init__(self, data_classes, file_path):
if not hasattr(self, "initialized"):
self.data_classes = data_classes
self.file_path = file_path
self.config = ConfigParser()
self.loaded_data = {}
self.initialized = True
self.__load()
# Save configuration in file
def save(self):
for cls in self.data_classes:
section = cls.__name__
if not self.config.has_section(section):
self.config.add_section(section)
if section in self.loaded_data:
instance = self.loaded_data[section]
else:
instance = cls()
obj_dict = asdict(instance)
for field, value in obj_dict.items():
if isinstance(value, bool):
self.config.set(section, field, str(value).lower())
else:
self.config.set(section, field, str(value))
with open(self.file_path, "w") as config_file:
self.config.write(config_file)
# Load data
def __load(self):
try:
self.config.read(self.file_path)
except Exception as e:
print(f"Error reading INI file: {e}")
print("Loading default values instead.")
self.loaded_data = {cls.__name__: cls() for cls in self.data_classes}
return
for cls in self.data_classes:
section = cls.__name__
if not self.config.has_section(section):
print(f"Section {section} not found in INI file. Loading default values.")
self.loaded_data[section] = cls()
continue
obj_dict = {}
for field in fields(cls):
field_name = field.name
if self.config.has_option(section, field_name):
value = self.config.get(section, field_name)
if field.type is bool:
value = value.lower() in ["true", "yes", "1"]
elif field.type is int:
value = int(value)
elif field.type is float:
value = float(value)
obj_dict[field_name] = value
obj = cls(**obj_dict)
self.loaded_data[section] = obj
def __getattr__(self, name):
if name == "loaded_data":
raise AttributeError(f"'Cfg' object has no attribute '{name}'")
if name in self.loaded_data:
return self.loaded_data[name]
else:
raise AttributeError(f"'Cfg' object has no attribute '{name}'")
@dataclass
class App:
name: str = ""
@dataclass
class Log:
fresh: bool = True
logfile: str = "practype.log"
level: str = "DEBUG"
# Init configuration
configurations = [App, Log]
cfg = Cfg(configurations, "config.ini")
# Init logger
log = LogConf(loglevel=cfg.Log.level, file=cfg.Log.logfile, continue_write=cfg.Log.fresh)
if __name__ == "__main__":
pass