-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
53 lines (47 loc) · 1.86 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
from utils.dataIO import dataIO
import sys
args = sys.argv
config = {} # Only to satisfy CodeQL requirements
if len(args) == 1:
print("config.py: small utility to edit the configuration (for use with CLI)")
print(f"- ./{args[0]} <file>: show config for file")
print(f"- ./{args[0]} <file> <key>: show config for a specific key")
print(f"- ./{args[0]} <file> <key> <value>: set config for a specific key")
print(f"Example: ./{args[0]} config owner 481038581032403850")
print("To set value lists please edit the config manually (there isn't that much lists)")
exit(0)
try:
config = dataIO.load_json("data/" + str(args[1]) + ".json")
except FileNotFoundError:
print(f"Missing data/{args[1]}.json file. Aborting.")
exit(1)
if len(args) == 2:
print(f"Current configuration for data/{args[1]}.json is:")
for i in config:
print(f"{i}: {config[i]}")
exit(0)
if len(args) == 3:
try:
print(f"{args[2]}: {config[args[2]]}")
except KeyError:
print(f"'{args[2]}' is not present in the data/{args[1]}.json file.")
print("Hint: Values are case-sensitive, please make sure you typed the value correctly.")
exit(2)
if len(args) > 3:
try:
config[args[2]]
except KeyError:
print(f"'{args[2]}' is not present in the data/{args[1]}.json file.")
print("Hint: Values are case-sensitive, please make sure you typed the value correctly.")
exit(2)
if isinstance(config[args[2]], (list, dict)):
print("Cannot edit lists or dicts, please open the config file yourself.")
exit(3)
if isinstance(config[args[2]], int):
config[args[2]] = int(args[3])
else:
value = " ".join(args[3:])
config[args[2]] = value
dataIO.save_json(str("data/" + str(args[1]) + ".json"), config)
print("Successfully set and saved value.")
exit(0)