-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
75 lines (54 loc) · 1.87 KB
/
parse.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
73
74
75
import csv
from dataclasses import dataclass
@dataclass
class KeyData:
raw: str
# isNice determines whether you use raw or combination
isNice: bool
combination: list
@dataclass
class Hotkey():
name: str
key: KeyData
@dataclass
class HotkeyCategory():
name: str
hotkeys: list
position: int
def gen_keydata(raw):
raw = raw.strip().replace("CTRL", "Ctrl").replace("ALT", "Alt").replace("SHIFT", "Shift").replace("DEL", "Delete")
def strip_array(arr):
return [x.strip() for x in arr]
if not " " in raw:
return KeyData(raw, True, [raw.strip()])
elif "+" in raw or "then" in raw:
return KeyData(raw, True, strip_array(raw.replace("+", "DELIMITERPY").replace("then", "DELIMITERPY").split("DELIMITERPY")))
else:
return KeyData(raw, False, [raw.strip()])
def build_hotkeys(csv_path):
categories = []
with open(csv_path, "r", encoding="utf8") as file:
reader = csv.reader(file)
# skip header
next(reader)
# get categories
indexes = []
skip = False
for index, column in enumerate(next(reader)):
if column:
if skip:
skip = False
continue
categories.append(HotkeyCategory(column, [], index))
indexes.append({"name": index, "data": index + 2})
skip = True
# get hotkeys
for row in reader:
for index in indexes:
if row[index["name"]] and row[index["data"]]:
for hotkeyCategory in categories:
if hotkeyCategory.position == index["name"]:
hotkeyCategory.hotkeys.append(Hotkey(row[index["name"]], gen_keydata(row[index["data"]])))
return categories
if __name__ == "__main__":
print(str(build_hotkeys("shortcuts.csv")))