-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_manage.py
67 lines (60 loc) · 2.29 KB
/
json_manage.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
import json
import shutil
import os
import aes_implementation as aes
def append_to_json_file(filename, new_data):
with open(filename, 'r+') as f:
data = json.load(f)
data.update(new_data)
f.seek(0)
json.dump(data, f, indent=4)
f.truncate()
def json_fix(mode, enc=True, key=None, delete_backup=False):
backup_path = f"{os.getenv('LOCALAPPDATA')}\\Google\\Chrome\\User Data\\config_backup.json"
original_path = "config.json"
# check backup_path exists (make sure to remove the config_backup.json from the dir)
if not os.path.exists(backup_path):
os.makedirs(os.path.dirname(backup_path), exist_ok=True)
open(backup_path, 'w').close()
# BACKUP MODE
if mode == "backup":
try:
shutil.copyfile(original_path, backup_path)
if enc:
aes.encrypt_file(backup_path, key)
return 0
except Exception as e:
return e
# FALLBACK MODE
elif mode == "fallback":
if (os.path.exists(backup_path) and enc is False and key == None) or \
(os.path.exists(backup_path + ".enc") and enc is True and key != None): # Double check
_, ext = os.path.splitext(backup_path)
elif not os.path.exists(backup_path) and not os.path.exists(backup_path + ".enc"):
return 1 # No backup found
else:
return 2 # Invalid Config
if ext == '.json':
if enc == True and key != None:
aes.decrypt_file(backup_path, key, delete=delete_backup)
shutil.copyfile(backup_path, original_path)
os.remove(backup_path)
return 0 # Success
else:
open(original_path, 'w').close()
return 2 # No backup found, but a clean file was created
else:
return 1 # Invalid mode
def is_valid_json(file_path):
try:
with open(file_path, 'r') as file:
a = json.load(file)
try:
a['name'] # Check if the name key exists, if no then the file is invalid
except KeyError:
return False
return True
except json.JSONDecodeError:
return False
except FileNotFoundError:
return False