-
Notifications
You must be signed in to change notification settings - Fork 0
/
modset_json.py
89 lines (56 loc) · 2.25 KB
/
modset_json.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
import json
import os
def validate_key(dict, key, value_default):
if (dict == value_default):
raise KeyError(f"Key {key} was not found.")
def read_json_return_dict(file_name, key="", value_default="default_return"):
# json.loads is a beautiful thing honestly.
# Can't believe it took me this long to try it out
with open(f'{os.getcwd()}/json/{file_name}.json', 'r') as file:
read_file = file.read()
dict = json.loads(read_file)
if (key != ""):
if (isinstance(key, list)): # if key is a list
if (len(key) > 2): exit() # exit early if list has more than 2 elements
dict = dict.get(key[0], value_default)
validate_key(dict, key[0], value_default)
try:
dict = dict[key[1]]
return dict
except KeyError:
raise KeyError(f"Key {key[1]} was not found. ( {key[0]} >> {key[1]} )")
except:
raise Exception("Something went wrong.")
dict = dict.get(key, value_default)
validate_key(dict, key, value_default)
else:
return dict
return dict
def save_json(data, file_name):
with open(f'{os.getcwd()}/json/{file_name}.json', 'w+') as file:
json.dump(data, file, indent=4)
def read_json(file_name):
with open(f'{os.getcwd()}/json/{file_name}.json', 'r') as file:
y = json.load(file)
return y
def read_json_return(file_name, data):
if ("/" in file_name):
with open(f'json/{file_name}', 'r') as file:
x = json.load(file)
else:
with open(f'{os.getcwd()}/json/{file_name}.json', 'r') as file:
x = json.load(file)
if (data not in x):
x.update({data: ""})
y = x[data]
return y
def update_json(data, file_name):
print(data)
try:
with open(f'{os.getcwd()}/json/{file_name}.json', 'r') as file:
y = json.load(file)
y.update(data)
save_json(y, file_name)
except:
print("Woops!")
# prompt.user_error("Error", "Something went wrong whilst saving settings. Some things may still work. Try creating an empty packer.json file")