forked from simos/keyboardlayouteditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_xml_to_xkb.py
106 lines (94 loc) · 2.93 KB
/
parse_xml_to_xkb.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Version 0.8
import sys
import os.path
import pdb
import antlr3
from lxml import etree
xmlfilename = "xkbsample.xml"
if len(sys.argv) > 1:
xmlfilename = sys.argv[1]
try:
xmlfile = open(xmlfilename, 'r')
xmlfile.close
except OSError:
print "Could not open file ", xmlfilename, ". Aborting..."
sys.exit(-1)
doc = etree.parse(xmlfilename)
# for elem in doc.getiterator():
# print elem.tag, "has", len(elem), "children", elem.attrib, elem.text
TABS="\t\t\t\t\t\t\t\t\t\t\t\t"
xkbfilename = os.path.basename(xmlfilename)[:-4]
fout = open(xkbfilename, "w")
def recurse_tree(node, depth):
if node.tag == "layout":
print "Parsing", node.attrib["layoutname"]
for n in node:
recurse_tree(n, depth+1)
elif node.tag == "symbols":
for k in node:
if k.tag == "mapoption":
fout.write(k.text)
fout.write(" ")
elif k.tag == "mapname":
fout.write("\"%(s)s\"\n{\n" % { "s": k.text })
elif k.tag == "mapmaterial":
for t in k:
if t.tag == "tokenname":
fout.write("\tname = \"%(s)s\";\n" % { "s": t.attrib["name"] })
elif t.tag == "tokeninclude":
fout.write("\tinclude \"%(s)s\"\n" % { "s": t.text })
elif t.tag == "tokentype":
fout.write("\tkey.type = \"%(s)s\";\n" % { "s": t.text })
elif t.tag == "tokenmodifiermap":
fout.write("\tmodifier_map %(s)s { " % { "s": t.attrib['state'] })
count_mm = len(t)
for mm in t:
if mm.tag == "keycodex":
fout.write("<%(s)s>" % { "s": mm.attrib["value"] })
elif mm.tag == "keycode":
fout.write("%(s)s" % { "s": mm.attrib["value"] })
if count_mm > 1:
fout.write(", ")
count_mm -= 1
fout.write(" };\n")
elif t.tag == "tokenkey":
fout.write("\t")
if t.attrib["override"] == "True":
fout.write("override ")
for tk in t:
if tk.tag == "keycodename":
fout.write("key <%(s)s> { " % { "s": tk.text })
elif tk.tag == "keysymgroup":
gotitem = False
for ks in tk:
if ks.tag == "typegroup":
if gotitem:
fout.write(", ")
fout.write("type = \"%(s)s\" " % { "s": ks.attrib["value"] })
gotitem = True
elif ks.tag == "tokenvirtualmodifiers":
if gotitem:
fout.write(", ")
fout.write("virtualMods = %(s)s " % { "s": ks.attrib["value"] })
elif ks.tag == "symbolsgroup":
if gotitem:
fout.write(", ")
gotitem = True
fout.write("[ ")
count_sg = len(ks)
for sg in ks:
if sg.tag == "symbol":
if count_sg > 1:
fout.write("%(s)s, " % { "s": sg.text })
else:
fout.write("%(s)s " % { "s": sg.text })
count_sg -= 1
else:
print "ERROR"
sys.exit(-1)
fout.write("]")
fout.write(" };\n")
fout.write("};\n\n")
recurse_tree(doc.getroot(), 0)