eric6/E5XML/HighlightingStylesReader.py

changeset 6942
2602857055c5
parent 6863
e900929889dd
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 """
8 Module implementing a class for reading a highlighting styles XML file.
9 """
10
11 from __future__ import unicode_literals
12
13 from PyQt5.QtGui import QColor, QFont
14
15 from .Config import highlightingStylesFileFormatVersion
16 from .XMLStreamReaderBase import XMLStreamReaderBase
17
18
19 class HighlightingStylesReader(XMLStreamReaderBase):
20 """
21 Class for reading a highlighting styles XML file.
22 """
23 supportedVersions = ["4.3", "6.0"]
24
25 def __init__(self, device, lexers):
26 """
27 Constructor
28
29 @param device reference to the I/O device to read from (QIODevice)
30 @param lexers list of lexer objects for which to export the styles
31 """
32 XMLStreamReaderBase.__init__(self, device)
33
34 self.lexers = lexers
35
36 self.version = ""
37
38 def readXML(self):
39 """
40 Public method to read and parse the XML document.
41 """
42 while not self.atEnd():
43 self.readNext()
44 if self.isStartElement():
45 if self.name() == "HighlightingStyles":
46 self.version = self.attribute(
47 "version",
48 highlightingStylesFileFormatVersion)
49 if self.version not in self.supportedVersions:
50 self.raiseUnsupportedFormatVersion(self.version)
51 elif self.name() == "Lexer":
52 self.__readLexer()
53 else:
54 self.raiseUnexpectedStartTag(self.name())
55
56 self.showErrorMessage()
57
58 def __readLexer(self):
59 """
60 Private method to read the lexer info.
61 """
62 language = self.attribute("name")
63 if language and language in self.lexers:
64 lexer = self.lexers[language]
65 else:
66 lexer = None
67
68 while not self.atEnd():
69 self.readNext()
70 if self.isEndElement() and self.name() == "Lexer":
71 break
72
73 if self.isStartElement():
74 if self.name() == "Style":
75 self.__readStyle(lexer)
76 else:
77 self.raiseUnexpectedStartTag(self.name())
78
79 def __readStyle(self, lexer):
80 """
81 Private method to read the style info.
82
83 @param lexer reference to the lexer object
84 """
85 if lexer is not None:
86 style = self.attribute("style")
87 if style:
88 style = int(style)
89 substyle = int(self.attribute("substyle", "-1"))
90 # -1 is default for base styles
91
92 # add sub-style if not already there
93 if not lexer.hasStyle(style, substyle):
94 substyle = lexer.addSubstyle(style)
95
96 color = self.attribute("color")
97 if color:
98 color = QColor(color)
99 else:
100 color = lexer.defaultColor(style, substyle)
101 lexer.setColor(color, style, substyle)
102
103 paper = self.attribute("paper")
104 if paper:
105 paper = QColor(paper)
106 else:
107 paper = lexer.defaultPaper(style, substyle)
108 lexer.setPaper(paper, style, substyle)
109
110 fontStr = self.attribute("font")
111 if fontStr:
112 font = QFont()
113 font.fromString(fontStr)
114 else:
115 font = lexer.defaultFont(style, substyle)
116 lexer.setFont(font, style, substyle)
117
118 eolfill = self.attribute("eolfill")
119 if eolfill:
120 eolfill = self.toBool(eolfill)
121 if eolfill is None:
122 eolfill = lexer.defaulEolFill(style, substyle)
123 else:
124 eolfill = lexer.defaulEolFill(style, substyle)
125 lexer.setEolFill(eolfill, style, substyle)
126
127 while not self.atEnd():
128 self.readNext()
129 if self.isStartElement():
130 if self.name() == "Description" and substyle >= 0:
131 # description can only be set for sub-styles
132 description = self.readElementText().strip()
133 if not description:
134 description = lexer.defaultDescription(
135 style, substyle)
136 lexer.setDescription(description, style, substyle)
137 elif self.name() == "Words" and substyle >= 0:
138 # words can only be set for sub-styles
139 words = self.readElementText().strip()
140 if not words:
141 words = lexer.defaultWords(style, substyle)
142 lexer.setWords(words, style, substyle)
143
144 if self.isEndElement() and self.name() == "Style":
145 return
146
147 while not self.atEnd():
148 self.readNext()
149 if self.isEndElement() and self.name() == "Style":
150 break

eric ide

mercurial