-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_Handler.py
73 lines (62 loc) · 2.12 KB
/
dict_Handler.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
import yaml,json,configparser
class Dict_Handler():
def __init__(self,file:str):
self.__dict = {}
self.__file = file
self.__config = configparser.ConfigParser()
self.__Check()
def __Check(self):
file = self.__file
if (file.lower().endswith(('.yml','yaml'))):
self.__read_yml()
if (file.lower().endswith('.ini')):
self.__read_ini()
if (file.lower().endswith('.json')):
self.__read_json()
self.__deType()
def __deType(self):
types = [".yml",".yaml",".json",".ini"]
de_Typed=list(self.__file.replace(fType,'')
for fType in types
if fType in self.__file
)
self.__file = ''.join(de_Typed)
def __read_yml(self):
file = open(self.__file,"r")#Safer than "with open as:"
self.__dict = yaml.safe_load(file)
file.close
def __read_json(self):
file = open(self.__file,'r')
self.__dict = json.load(file)
file.close()
def __read_ini(self):
config = self.__config
config.read(self.__file)
for section in config.sections():
self.__dict[section] = {}
for name,value in config.items(section):
self.__dict[section].update({name:value})
def to_yaml(self, mk_file=True):
fileName= self.__file+(".yaml")
if not(mk_file):
return yaml.dump(self.__dict)
with open(fileName,'w') as file:
yaml.dump(self.__dict, file)
file.close()
def to_json(self, mk_file=True):
fileName= self.__file+(".json")
if not(mk_file):
return json.dumps(self.__dict)
with open(fileName,'w') as file:
data = json.dumps(self.__dict,indent=True)
file.writelines(data)
file.close
def to_ini(self, mk_file=True):
fileName= self.__file+(".ini")
config = self.__config
config.read_dict(self.__dict)
if not(mk_file):
return config
with open(fileName,'w') as file:
config.write(file)
file.close