use another function to initial import mytest.py (this fixes python
[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 def parseKeys(context, filename, actionmap, device, keys):
10         for x in keys.findall("key"):
11                 get_attr = x.attrib.get
12                 mapto = get_attr("mapto")
13                 id = get_attr("id")
14                 flags = get_attr("flags")
15
16                 flag_ascii_to_id = lambda x: {'m':1,'b':2,'r':4,'l':8}[x]
17
18                 flags = sum(map(flag_ascii_to_id, flags))
19
20                 assert mapto, "%s: must specify mapto in context %s, id '%s'" % (filename, context, id)
21                 assert id, "%s: must specify id in context %s, mapto '%s'" % (filename, context, mapto)
22                 assert flags, "%s: must specify at least one flag in context %s, id '%s'" % (filename, context, id)
23
24                 if len(id) == 1:
25                         keyid = ord(id) | 0x8000
26                 elif id[0] == '\\':
27                         if id[1] == 'x':
28                                 keyid = int(id[2:], 0x10) | 0x8000
29                         elif id[1] == 'd':
30                                 keyid = int(id[2:]) | 0x8000
31                         else:
32                                 raise "key id '" + str(id) + "' is neither hex nor dec"
33                 else:
34                         try:
35                                 keyid = KEYIDS[id]
36                         except:
37                                 raise "key id '" + str(id) + "' is illegal"
38 #                               print context + "::" + mapto + " -> " + device + "." + hex(keyid)
39                 actionmap.bindKey(filename, device, keyid, flags, context, mapto)
40                 addKeyBinding(filename, keyid, context, mapto, flags)
41
42 def readKeymap(filename):
43         p = enigma.eActionMap.getInstance()
44         assert p
45
46         source = open(filename)
47
48         try:
49                 dom = xml.etree.cElementTree.parse(source)
50         except:
51                 raise "keymap %s not well-formed." % filename
52
53         keymap = dom.getroot()
54
55         for cmap in keymap.findall("map"):
56                 context = cmap.attrib.get("context")
57                 assert context, "map must have context"
58
59                 for device in cmap.findall("device"):
60                         parseKeys(context, filename, p, device.attrib.get("name"), device)
61
62                 parseKeys(context, filename, p, "generic", cmap)
63
64 def removeKeymap(filename):
65         p = enigma.eActionMap.getInstance()
66         p.unbindKeyDomain(filename)