-
Notifications
You must be signed in to change notification settings - Fork 2
/
genKeymaps.py
executable file
·82 lines (73 loc) · 2.89 KB
/
genKeymaps.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
76
77
78
79
80
81
82
#!/usr/bin/env python3
import json
import os
files = []
docPath = './docs/'
rawFileBaseURL = 'https://raw.githubusercontent.com/mayankmetha/Rucky-KeyMap/main/'
pagesFileBaseURL = 'https://mayankmetha.github.io/Rucky-KeyMap/'
versionRecord = {}
def findFiles():
tmpList = [os.path.join(docPath,f) for f in os.listdir(docPath) if os.path.isfile(os.path.join(docPath,f))]
files.extend(list(set(tmpList).difference(set([os.path.join(docPath,f) for f in ['index.md','_config.yml']]))))
appendHIDDocs()
def appendHIDDocs():
header = '''## Rucky HID Keymaps\n\nSl. No.|HID Name|Rucky HID Config File\n:---:|:---:|:---:\n'''
for _ in range(len(files)):
line = str(int(_+1))+"|"
filename = files[_].split("/")[-1].replace(".md","")
line += "["+filename.replace("_"," ").upper()+"]("+pagesFileBaseURL+filename+")|["+filename+".json]("
line += pagesFileBaseURL+filename+".json)\n"
header += line
file = open(os.path.join(docPath,'index.md'),'w')
file.write(header)
file.close()
generateHIDKeymapFiles()
def generateHIDKeymapFiles():
for f in files:
mdFile = open(f,'r',encoding="utf-8")
jsonFile = open(f.split("/")[-1].replace(".md",".json"),'w')
jsonObj = {}
mapping = {}
for _ in mdFile.readlines():
if "Version" in _:
jsonObj['version'] = _.strip().split(" ")[2]
versionRecord[f.split("/")[-1].replace(".md","").upper()] = _.strip().split(" ")[2]
if "0x" in _:
if "\\|" in _:
_ = _.replace("\|","[pipe]")
line = _.replace("\n","").split("|")
if(len(line) != 8):
line.append("")
modifiers = {
'ctrl': line[3].replace(" ",""),
'shift': line[4].replace(" ",""),
'alt': line[5].replace(" ",""),
'meta': line[6].replace(" ","")
}
key = {
'name': line[7],
'keycode': line[2].replace("0x",""),
'modifier': modifiers
}
mapping[line[1]] = key
mdFile.close()
jsonObj['mapping'] = mapping
jsonFile.write(json.dumps(jsonObj, ensure_ascii=False).replace("\\\\","\\"))
jsonFile.close()
generateKeymapList()
def generateKeymapList():
jsonArray = []
for f in files:
name = f.split("/")[-1].replace(".md","").upper()
jsonObj = {
'name': name.replace("_"," "),
'revision': versionRecord[name],
'filename': f.split("/")[-1].replace(".md",".json"),
'url': rawFileBaseURL+f.split("/")[-1].replace(".md",".json")
}
jsonArray.append(jsonObj)
file = open('keymap.json','w')
file.write(json.dumps(jsonArray))
file.close()
if __name__ == '__main__':
findFiles()