-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresets_add_delete_modify.py
55 lines (52 loc) · 1.84 KB
/
presets_add_delete_modify.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
import os
import sys
import json
import secrets
from utils import presets_file, display_notification, presets_file_path, custom_logger
try:
query = sys.argv[1].split(';')
_type, _input = query[0], query[1]
except IndexError as e:
display_notification('🚨 Error !', 'Something went wrong, check the logs and create a GitHub issue')
custom_logger('error', e)
exit()
data = presets_file() if presets_file().get('items') else {'items':[]}
try:
if _type == '_delete':
for item in data['items']:
if item.get('id') == _input:
data['items'].remove(item)
break
elif _type == '_new':
value = os.environ['_new_preset']
title, subtitle = _input.split('/')[0], _input.split('/')[1]
json_obj = {
'title': title,
'subtitle': subtitle,
'arg': value,
'id': secrets.token_hex(16)
}
data['items'].append(json_obj)
elif _type == '_modify':
_subtype = os.environ['modif2']
_key = os.environ['modif5']
for item in data['items']:
if item.get('id') == _key:
title = item.get('title')
break
if _subtype == 'tl&sb':
title, subtitle = _input.split('/')[0], _input.split('/')[1]
for item in data['items']:
if item.get('id') == _key:
item['title'] = title
item['subtitle'] = subtitle
elif _subtype == 'arg':
for item in data['items']:
if item.get('id') == _key:
item['arg'] = _input
except Exception as e:
display_notification('🚨 Error !', 'Invalid input, check the logs')
custom_logger('error', e)
exit()
with open(presets_file_path, 'w') as file:
json.dump(data, file, indent=4, ensure_ascii=False)