RecordTimer.py: fixed typo (thx to dr.best)
[enigma2.git] / keymapparser.py
1 import enigma
2 import xml.etree.cElementTree
3
4 from keyids import KEYIDS
5
6 # these are only informational (for help)...
7 from Tools.KeyBindings import addKeyBinding
8
9 class KeymapError(Exception):
10     def __init__(self, message):
11         self.msg = message
12
13     def __str__(self):
14         return self.msg
15
16 def parseKeys(context, filename, actionmap, device, keys):
17         for x in keys.findall("key"):
18                 get_attr = x.attrib.get
19                 mapto = get_attr("mapto")
20                 id = get_attr("id")
21                 flags = get_attr("flags")
22
23                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4,'l':8}[x]
24
25                 flags = sum(map(flag_ascii_to_id, flags))
26
27                 assert mapto, "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
28                 assert id, "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
29                 assert flags, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
30
31                 if len(id) == 1:
32                         keyid = ord(id) | 0x8000
33                 elif id[0] == '\\':
34                         if id[1] == 'x':
35                                 keyid = int(id[2:], 0x10) | 0x8000
36                         elif id[1] == 'd':
37                                 keyid = int(id[2:]) | 0x8000
38                         else:
39                                 raise KeymapError("key id '" + str(id) + "' is neither hex nor dec")
40                 else:
41                         try:
42                                 keyid = KEYIDS[id]
43                         except:
44                                 raise KeymapError("key id '" + str(id) + "' is illegal")
45 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
46                 actionmap.bindKey(filename, device, keyid, flags, context, mapto)
47                 addKeyBinding(filename, keyid, context, mapto, flags)
48
49 def readKeymap(filename):
50         p = enigma.eActionMap.getInstance()
51         assert p
52
53         source = open(filename)
54
55         try:
56                 dom = xml.etree.cElementTree.parse(source)
57         except:
58                 raise KeymapError("keymap %s not well-formed." % filename)
59
60         keymap = dom.getroot()
61
62         for cmap in keymap.findall("map"):
63                 context = cmap.attrib.get("context")
64                 assert context, "map must have context"
65
66                 for device in cmap.findall("device"):
67                         parseKeys(context, filename, p, device.attrib.get("name"), device)
68
69                 parseKeys(context, filename, p, "generic", cmap)
70
71 def removeKeymap(filename):
72         p = enigma.eActionMap.getInstance()
73         p.unbindKeyDomain(filename)