-
Notifications
You must be signed in to change notification settings - Fork 221
/
gensimple.py
118 lines (108 loc) · 4.31 KB
/
gensimple.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
107
108
109
110
111
112
113
114
115
116
117
118
# vim:set et sts=4 sw=4:
#
# ibus - The Input Bus
#
# Copyright (c) 2007-2010 Peng Huang <shawn.p.huang@gmail.com>
# Copyright (c) 2007-2010 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA 02111-1307 USA
import cgi
import urllib2
from xml.dom import minidom
def simplfy_dom(node):
name = node.nodeName
children = {}
if len(node.childNodes) == 1 and node.childNodes[0].nodeType == node.TEXT_NODE:
return name, node.childNodes[0].nodeValue
for child in node.childNodes:
if child.nodeType != node.ELEMENT_NODE:
continue
child_name, child_value = simplfy_dom(child)
if child_name not in children:
children[child_name] = []
children[child_name].append(child_value)
return name, children
def parse_xml():
filename = "/usr/share/X11/xkb/rules/evdev.xml"
dom = minidom.parse(file(filename))
name, root = simplfy_dom(dom)
layouts = root['xkbConfigRegistry'][0]['layoutList'][0]['layout']
for layout in layouts:
config = layout['configItem'][0]
name = config['name'][0]
short_desc = config.get('shortDescription', [''])[0]
desc = config.get('description', [''])[0]
languages = config.get('languageList', [{}])[0].get('iso639Id', [])
variants = layout.get('variantList', [{}])[0].get('variant', [])
yield name, '', short_desc, desc, languages
for variant in variants:
variant_config = variant['configItem'][0]
variant_name = variant_config['name'][0]
variant_short_desc = variant_config.get('shortDescription', [''])[0]
variant_desc = variant_config.get('description', [''])[0]
variant_languages = variant_config.get('languageList', [{}])[0].get('iso639Id', [])
if not isinstance(variant_languages, list):
variant_languages = [variant_languages]
yield name, variant_name, variant_short_desc, variant_desc, languages + variant_languages
def gen_xml():
header = u"""<component>
<name>org.freedesktop.IBus.Simple</name>
<description>A table based simple engine</description>
<exec>${libexecdir}/ibus-engine-simple</exec>
<version>@VERSION@</version>
<author>Peng Huang <shawn.p.huang@gmail.com></author>
<license>GPL</license>
<homepage>http://code.google.com/p/ibus</homepage>
<textdomain>ibus</textdomain>
<engines>"""
engine = u"""\t\t<engine>
<name>%s</name>
<language>%s</language>
<license>GPL</license>
<author>Peng Huang <shawn.p.huang@gmail.com></author>
<layout>%s</layout>
<longname>%s</longname>
<description>%s</description>
<icon>ibus-keyboard</icon>
<rank>%d</rank>
</engine>"""
footer = u"""\t</engines>
</component>"""
print header
whitelist = parse_whitelist()
for name, vname, sdesc, desc, languages in parse_xml():
layout = "%s(%s)" % (name, vname) if vname else name
for lang in languages:
ibus_name = "xkb:%s:%s:%s" % (name, vname, lang)
if ibus_name not in whitelist:
continue
desc = cgi.escape(desc)
out = engine % (ibus_name, lang, layout, desc, desc, 99)
print out.encode("utf8")
print footer
def parse_whitelist():
url = "http://git.chromium.org/gitweb/?p=chromium/chromium.git;a=blob_plain;f=chrome/browser/chromeos/input_method/ibus_input_methods.txt;hb=HEAD"
whitelist = []
for line in urllib2.urlopen(url):
line = line.strip()
if not line:
continue
if line.startswith("#"):
continue
whitelist.append(line.split()[0])
return set(whitelist)
if __name__ == "__main__":
gen_xml()