E5XML/ShortcutsHandler.py

changeset 50
a36eecf45b2e
parent 45
9a18f4dbb493
child 792
a13346916170
equal deleted inserted replaced
49:f991944e859c 50:a36eecf45b2e
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2003 - 2010 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the handler class for reading a keyboard shortcuts file.
8 """
9
10 from .Config import shortcutsFileFormatVersion
11 from .XMLHandlerBase import XMLHandlerBase
12
13 class ShortcutsHandler(XMLHandlerBase):
14 """
15 Class implementing a sax handler to read a keyboard shortcuts file.
16 """
17 def __init__(self):
18 """
19 Constructor
20 """
21 XMLHandlerBase.__init__(self)
22
23 self.startDocumentSpecific = self.startDocumentShortcuts
24
25 self.elements.update({
26 'Shortcuts' : (self.startShortcuts, self.defaultEndElement),
27 'Shortcut' : (self.startShortcut, self.endShortcut),
28 'Name' : (self.defaultStartElement, self.endName),
29 'Accel' : (self.defaultStartElement, self.endAccel),
30 'AltAccel' : (self.defaultStartElement, self.endAltAccel),
31 })
32
33 def startDocumentShortcuts(self):
34 """
35 Handler called, when the document parsing is started.
36 """
37 self.shortcuts = {} # dictionary for storing the shortcuts
38 self.version = ''
39
40 ###################################################
41 ## below follow the individual handler functions
42 ###################################################
43
44 def endName(self):
45 """
46 Handler method for the "Name" end tag.
47 """
48 self.name = self.buffer
49
50 def endAccel(self):
51 """
52 Handler method for the "Accel" end tag.
53 """
54 self.accel = self.unescape(self.buffer)
55
56 def endAltAccel(self):
57 """
58 Handler method for the "AltAccel" end tag.
59 """
60 self.altAccel = self.unescape(self.buffer)
61
62 def startShortcut(self, attrs):
63 """
64 Handler method for the "Shortcut" start tag.
65
66 @param attrs list of tag attributes
67 """
68 self.name = ''
69 self.accel = ''
70 self.altAccel = ''
71 self.category = attrs.get('category', '')
72
73 def endShortcut(self):
74 """
75 Handler method for the "Shortcut" end tag.
76 """
77 if self.category:
78 if self.category not in self.shortcuts:
79 self.shortcuts[self.category] = {}
80 self.shortcuts[self.category][self.name] = (self.accel, self.altAccel)
81
82 def startShortcuts(self, attrs):
83 """
84 Handler method for the "Shortcuts" start tag.
85
86 @param attrs list of tag attributes
87 """
88 self.version = attrs.get('version', shortcutsFileFormatVersion)
89
90 def getShortcuts(self):
91 """
92 Public method to retrieve the shortcuts.
93
94 @return Dictionary of dictionaries of shortcuts. The keys of the
95 dictionary are the categories, the values are dictionaries.
96 These dictionaries have the shortcut name as their key and
97 a tuple of accelerators as their value.
98 """
99 return self.shortcuts
100
101 def getVersion(self):
102 """
103 Public method to retrieve the version of the shortcuts.
104
105 @return String containing the version number.
106 """
107 return self.version

eric ide

mercurial