-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_json_editor.py
72 lines (63 loc) · 2.11 KB
/
cli_json_editor.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
import json
import sys
def print_usage():
print("Command line JSON database editor")
print("Usage:")
print(" python cli_json_editor.py <filename> <<<-- do not append '.json'")
print("Commands:")
print(" view - View all keys and values")
print(" get <key> - Get a value for a key")
print(" set <key> <value> - Set a value for a key")
print(" delete <key> - Delete a key and its value")
print(" exit - Exit the editor")
def load_data(filename):
try:
with open(filename + '.json', 'r') as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_data(filename, data):
with open(filename + '.json', 'w') as file:
json.dump(data, file)
def main():
if len(sys.argv) != 2:
print("Error: Filename required")
print_usage()
sys.exit(1)
filename = sys.argv[1]
data = load_data(filename)
print("Opened database:", filename)
print_usage()
while True:
try:
cmd = input("Enter command: ").strip().split()
if not cmd:
continue
elif cmd[0] == 'exit':
break
elif cmd[0] == 'view':
for key, value in data.items():
print(f"{key}: {value}")
elif cmd[0] == 'get' and len(cmd) > 1:
key = cmd[1]
print(data.get(key, "Key not found"))
elif cmd[0] == 'set' and len(cmd) > 2:
key, value = cmd[1], ' '.join(cmd[2:])
data[key] = value
print(f"Set {key} to {value}")
elif cmd[0] == 'delete' and len(cmd) > 1:
key = cmd[1]
if key in data:
del data[key]
print(f"Deleted {key}")
else:
print("Key not found")
else:
print("Invalid command")
print_usage()
except Exception as e:
print("Error:", e)
save_data(filename, data)
print("Database saved and closed")
if __name__ == "__main__":
main()