-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeltool.py
51 lines (45 loc) · 1.64 KB
/
labeltool.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
import random
label_names = []
def add_label():
label = input('Enter label name: ')
label_names.append(label)
def view_labels():
for i, label in enumerate(label_names):
print(f'{i+1}: {label}')
def generate_label_code():
label_indices = input('Enter label numbers separated by commas: ')
label_indices = [int(i) for i in label_indices.split(',')]
label_code = '<View>\n <Text name="text" value="$text" maxLength="5000"/>\n <Labels name="labels" toName="text">\n'
for i in label_indices:
label = label_names[i-1]
color = f'#{random.randint(0, 0xFFFFFF):06x}'
shortcut = chr(i+64)
label_code += f' <Label value="{label}" style="background: {color};" shortcut="{shortcut}"/>\n'
label_code += ' </Labels>\n</View>\n'
print(label_code)
with open('labels.txt', 'w') as f:
f.write('Labels:\n')
for label in label_names:
f.write(f'{label}\n')
f.write('\nCode:\n')
f.write(label_code)
def import_labels():
with open('labels.txt', 'r') as f:
lines = f.readlines()
for line in lines:
if line.startswith('Labels:'):
continue
elif line.startswith('\n'):
break
else:
label_names.append(line.strip())
while True:
action = input('Enter action (1: add label, 2: view labels, 3: generate code, 4: import labels): ')
if action == '1':
add_label()
elif action == '2':
view_labels()
elif action == '3':
generate_label_code()
elif action == '4':
import_labels()