eric6/E5XML/ShortcutsReader.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a class for reading an XML shortcuts file.
8 """
9
10 from __future__ import unicode_literals
11
12 from .Config import shortcutsFileFormatVersion
13 from .XMLStreamReaderBase import XMLStreamReaderBase
14
15
16 class ShortcutsReader(XMLStreamReaderBase):
17 """
18 Class for reading an XML shortcuts file.
19 """
20 supportedVersions = ["3.6"]
21
22 def __init__(self, device):
23 """
24 Constructor
25
26 @param device reference to the I/O device to read from (QIODevice)
27 """
28 XMLStreamReaderBase.__init__(self, device)
29
30 self.version = ""
31 self.shortcuts = {}
32
33 def readXML(self):
34 """
35 Public method to read and parse the XML document.
36 """
37 while not self.atEnd():
38 self.readNext()
39 if self.isStartElement():
40 if self.name() == "Shortcuts":
41 self.version = self.attribute(
42 "version", shortcutsFileFormatVersion)
43 if self.version not in self.supportedVersions:
44 self.raiseUnsupportedFormatVersion(self.version)
45 elif self.name() == "Shortcut":
46 self.__readShortCut()
47 else:
48 self.raiseUnexpectedStartTag(self.name())
49
50 self.showErrorMessage()
51
52 def __readShortCut(self):
53 """
54 Private method to read the shortcut data.
55 """
56 category = self.attribute("category")
57 name = ""
58 accel = ""
59 altAccel = ""
60
61 while not self.atEnd():
62 self.readNext()
63 if self.isEndElement() and self.name() == "Shortcut":
64 if category:
65 if category not in self.shortcuts:
66 self.shortcuts[category] = {}
67 self.shortcuts[category][name] = (accel, altAccel)
68 break
69
70 if self.isStartElement():
71 if self.name() == "Name":
72 name = self.readElementText()
73 elif self.name() == "Accel":
74 accel = self.readElementText()
75 elif self.name() == "AltAccel":
76 altAccel = self.readElementText()
77 else:
78 self.raiseUnexpectedStartTag(self.name())
79
80 def getShortcuts(self):
81 """
82 Public method to retrieve the shortcuts.
83
84 @return Dictionary of dictionaries of shortcuts. The keys of the
85 dictionary are the categories, the values are dictionaries.
86 These dictionaries have the shortcut name as their key and
87 a tuple of accelerators as their value.
88 """
89 return self.shortcuts

eric ide

mercurial