-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.py
More file actions
37 lines (31 loc) · 1.04 KB
/
storage.py
File metadata and controls
37 lines (31 loc) · 1.04 KB
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
import argparse
import os
import tempfile
import json
parser = argparse.ArgumentParser()
parser.add_argument("--key", help="key name")
parser.add_argument("--val", help="value")
args = parser.parse_args()
storage_path = os.path.join(tempfile.gettempdir(), 'storage.data')
try:
if os.path.exists(storage_path) and os.path.isfile(storage_path):
if args.val:
with open(storage_path, 'r') as f:
json_data = json.load(f)
if args.key in json_data:
json_data[args.key].append(args.val)
else:
json_data[args.key] = [args.val]
print(json_data)
with open(storage_path, "w") as f:
json.dump(json_data, f)
else:
with open(storage_path, 'r') as f:
json_data = json.load(f)
print(', '.join(json_data.get(args.key)))
else:
print("error")
except:
json_data = {}
with open(storage_path, 'w') as f:
json.dump(json_data, f)