- improved parser
[openblackhole/openblackhole-enigma2.git] / keymapparser.py
1 import xml.dom.minidom
2 import enigma
3
4 from keyids import KEYIDS;
5
6 def readKeymap():
7
8         p = enigma.eActionMapPtr()
9         enigma.eActionMap.getInstance(p)
10         assert p
11         
12         filename = "keymap.xml"
13         
14         try:
15                 source = open(filename)
16         except:
17                 raise "couldn't open keymap.xml!"
18         
19         try:
20                 dom = xml.dom.minidom.parse(source)
21         except:
22                 raise "keymap not well-formed."
23         
24         try:    
25                 keymap = dom.getElementsByTagName("keymap")[0]
26         except:
27                 raise "no keymap defined."
28         
29         maps = keymap.getElementsByTagName("map")
30         
31         for cmap in maps:
32                 context = str(cmap.getAttribute("context"))
33                 assert context != "", "map must have context"
34         
35                 def parseKeys(device, keys):
36                         for x in keys.getElementsByTagName("key"):
37                                 mapto = str(x.getAttribute("mapto"))
38                                 id = x.getAttribute("id")
39                                 flags = x.getAttribute("flags")
40                                 
41                                 flag_ascii_to_id = lambda x: {'m':1,'r':2,'b':4}[x]
42                                 
43 #                               try:
44                                 flags = sum(map(flag_ascii_to_id, flags))
45                                 print "-> " + str(flags)
46 #                               except:
47 #                                       raise str("%s: illegal flags '%s' specificed in context %s, id '%s'" % (filename, flags, context, id))
48                                 
49                                 assert mapto != "", "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
50                                 assert id != "", "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
51                                 assert flags != 0, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
52                                 
53                                 if len(id) == 1:
54                                         keyid = ord(id) | 0x8000
55                                 elif id[0] == '\\':
56                                         if id[1] == 'x':
57                                                 keyid = int(id[2:], 0x10) | 0x8000
58                                         elif id[1] == 'd':
59                                                 keyid = int(id[2:]) | 0x8000
60                                         else:
61                                                 raise "key id '" + str(id) + "' is neither hex nor dec"
62                                 else:
63                                         try:
64                                                 keyid = KEYIDS[id]
65                                         except:
66                                                 raise "key id '" + str(id) + "' is illegal"
67
68                                 print context + "::" + mapto + " -> " + device + "." + hex(keyid)
69                                 p.bindKey(device, keyid, 7, context, mapto)
70                 
71                 parseKeys("generic", cmap)
72                 
73                 for device in cmap.getElementsByTagName("device"):
74                         parseKeys(str(device.getAttribute("name")), device)
75