-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathez_config.py
79 lines (60 loc) · 2.26 KB
/
ez_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
#######################
# Lainupcomputer #
# ez_config v1.0 #
#######################
import json
def save(file_path, file): # Save data to settings
with open(file_path, "w") as f:
json.dump(file, f, indent=2)
def read_file(file_path): # Read all settings to process
with open(file_path, "r") as f:
file = json.load(f)
return file
class ez_config:
file_path = ""
separator = ""
debug = False
def send_debug(self, message):
if self.debug:
print(message)
def initialise(self, file_path, separator="#", debug=False):
self.file_path = file_path
self.separator = separator
self.debug = debug
there = True
while there:
if self.file_path != "":
try:
with open(self.file_path, "r"):
self.send_debug("[*] Data found...")
break
except FileNotFoundError:
with open(self.file_path, "w") as f:
f.write("{\n\n}")
self.send_debug("[*] Data created...")
else:
print("Insert: file_path!")
def get(self, cfg="Configuration", data=""): # get data from settings
try:
file = read_file(self.file_path)
value = file[str(cfg)][data]
self.send_debug(f"got {value} from {cfg}>{data}")
return value
except KeyError:
self.send_debug(f"{cfg}>{data} Not Found")
def edit(self, cfg="Configuration", data="", value=None): # edit in settings
try:
file = read_file(self.file_path)
file[str(cfg)][data] = value
save(self.file_path, file)
self.send_debug(f"edited {value} from {cfg}>{data}")
except KeyError:
self.send_debug(f"{value} for {cfg}>{data} Not Found")
def add(self, cfg="Configuration", data=""): # Add Data to settings
file = read_file(self.file_path)
file[str(cfg)] = {}
for entry in data.split(self.separator):
split_value = entry.split(":")
file[str(cfg)][str(split_value[0])] = split_value[1]
save(self.file_path, file)
self.send_debug(f"added {cfg}>{data}")