Sun, 12 Sep 2010 12:06:50 +0200
Migrated the highlighting styles handler to highlighting styles reader.
Documentation/Source/eric5.E5XML.HighlightingStylesHandler.html | file | annotate | diff | comparison | revisions | |
E5XML/HighlightingStylesHandler.py | file | annotate | diff | comparison | revisions | |
E5XML/HighlightingStylesReader.py | file | annotate | diff | comparison | revisions | |
E5XML/HighlightingStylesWriter.py | file | annotate | diff | comparison | revisions | |
Preferences/ConfigurationPages/EditorHighlightingStylesPage.py | file | annotate | diff | comparison | revisions | |
eric5.e4p | file | annotate | diff | comparison | revisions |
--- a/Documentation/Source/eric5.E5XML.HighlightingStylesHandler.html Sun Sep 12 12:02:39 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -<?xml version="1.0" encoding="utf-8"?> -<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' -'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'> -<html><head> -<title>eric5.E5XML.HighlightingStylesHandler</title> -<style> -body { - background: #EDECE6; - margin: 0em 1em 10em 1em; - color: black; -} - -h1 { color: white; background: #85774A; } -h2 { color: white; background: #85774A; } -h3 { color: white; background: #9D936E; } -h4 { color: white; background: #9D936E; } - -a { color: #BA6D36; } - -</style> -</head> -<body><a NAME="top" ID="top"></a> -<h1>eric5.E5XML.HighlightingStylesHandler</h1> -<p> -Module implementing the handler class for handling a highlighting styles XML file. -</p> -<h3>Global Attributes</h3> -<table> -<tr><td>None</td></tr> -</table> -<h3>Classes</h3> -<table> -<tr> -<td><a href="#HighlightingStylesHandler">HighlightingStylesHandler</a></td> -<td>Class implementing a sax handler to read a highlighting styles file.</td> -</tr> -</table> -<h3>Functions</h3> -<table> -<tr><td>None</td></tr> -</table> -<hr /><hr /> -<a NAME="HighlightingStylesHandler" ID="HighlightingStylesHandler"></a> -<h2>HighlightingStylesHandler</h2> -<p> - Class implementing a sax handler to read a highlighting styles file. -</p> -<h3>Derived from</h3> -XMLHandlerBase -<h3>Class Attributes</h3> -<table> -<tr><td>None</td></tr> -</table> -<h3>Methods</h3> -<table> -<tr> -<td><a href="#HighlightingStylesHandler.__init__">HighlightingStylesHandler</a></td> -<td>Constructor</td> -</tr><tr> -<td><a href="#HighlightingStylesHandler.getVersion">getVersion</a></td> -<td>Public method to retrieve the version of the shortcuts.</td> -</tr><tr> -<td><a href="#HighlightingStylesHandler.startDocumentHighlightingStyles">startDocumentHighlightingStyles</a></td> -<td>Handler called, when the document parsing is started.</td> -</tr><tr> -<td><a href="#HighlightingStylesHandler.startHighlightingStyles">startHighlightingStyles</a></td> -<td>Handler method for the "HighlightingStyles" start tag.</td> -</tr><tr> -<td><a href="#HighlightingStylesHandler.startLexer">startLexer</a></td> -<td>Handler method for the "Lexer" start tag.</td> -</tr><tr> -<td><a href="#HighlightingStylesHandler.startStyle">startStyle</a></td> -<td>Handler method for the "Style" start tag.</td> -</tr> -</table> -<a NAME="HighlightingStylesHandler.__init__" ID="HighlightingStylesHandler.__init__"></a> -<h4>HighlightingStylesHandler (Constructor)</h4> -<b>HighlightingStylesHandler</b>(<i>lexers</i>) -<p> - Constructor -</p><dl> -<dt><i>lexers</i></dt> -<dd> -dictionary of lexer objects for which to import the styles -</dd> -</dl><a NAME="HighlightingStylesHandler.getVersion" ID="HighlightingStylesHandler.getVersion"></a> -<h4>HighlightingStylesHandler.getVersion</h4> -<b>getVersion</b>(<i></i>) -<p> - Public method to retrieve the version of the shortcuts. -</p><dl> -<dt>Returns:</dt> -<dd> -String containing the version number. -</dd> -</dl><a NAME="HighlightingStylesHandler.startDocumentHighlightingStyles" ID="HighlightingStylesHandler.startDocumentHighlightingStyles"></a> -<h4>HighlightingStylesHandler.startDocumentHighlightingStyles</h4> -<b>startDocumentHighlightingStyles</b>(<i></i>) -<p> - Handler called, when the document parsing is started. -</p><a NAME="HighlightingStylesHandler.startHighlightingStyles" ID="HighlightingStylesHandler.startHighlightingStyles"></a> -<h4>HighlightingStylesHandler.startHighlightingStyles</h4> -<b>startHighlightingStyles</b>(<i>attrs</i>) -<p> - Handler method for the "HighlightingStyles" start tag. -</p><dl> -<dt><i>attrs</i></dt> -<dd> -list of tag attributes -</dd> -</dl><a NAME="HighlightingStylesHandler.startLexer" ID="HighlightingStylesHandler.startLexer"></a> -<h4>HighlightingStylesHandler.startLexer</h4> -<b>startLexer</b>(<i>attrs</i>) -<p> - Handler method for the "Lexer" start tag. -</p><dl> -<dt><i>attrs</i></dt> -<dd> -list of tag attributes -</dd> -</dl><a NAME="HighlightingStylesHandler.startStyle" ID="HighlightingStylesHandler.startStyle"></a> -<h4>HighlightingStylesHandler.startStyle</h4> -<b>startStyle</b>(<i>attrs</i>) -<p> - Handler method for the "Style" start tag. -</p><dl> -<dt><i>attrs</i></dt> -<dd> -list of tag attributes -</dd> -</dl> -<div align="right"><a href="#top">Up</a></div> -<hr /> -</body></html> \ No newline at end of file
--- a/E5XML/HighlightingStylesHandler.py Sun Sep 12 12:02:39 2010 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,116 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (c) 2008 - 2010 Detlev Offenbach <detlev@die-offenbachs.de> -# - -""" -Module implementing the handler class for handling a highlighting styles XML file. -""" - -from PyQt4.QtGui import QColor, QFont - -from .Config import highlightingStylesFileFormatVersion -from .XMLHandlerBase import XMLHandlerBase - -class HighlightingStylesHandler(XMLHandlerBase): - """ - Class implementing a sax handler to read a highlighting styles file. - """ - def __init__(self, lexers): - """ - Constructor - - @param lexers dictionary of lexer objects for which to import the styles - """ - XMLHandlerBase.__init__(self) - - self.lexers = lexers - self.lexer = None - - self.startDocumentSpecific = self.startDocumentHighlightingStyles - - self.elements.update({ - 'HighlightingStyles' : (self.startHighlightingStyles, self.defaultEndElement), - 'Lexer' : (self.startLexer, self.defaultEndElement), - 'Style' : (self.startStyle, self.defaultEndElement), - }) - - def startDocumentHighlightingStyles(self): - """ - Handler called, when the document parsing is started. - """ - self.version = '' - - ################################################### - ## below follow the individual handler functions - ################################################### - - def startHighlightingStyles(self, attrs): - """ - Handler method for the "HighlightingStyles" start tag. - - @param attrs list of tag attributes - """ - self.version = attrs.get('version', highlightingStylesFileFormatVersion) - - def startLexer(self, attrs): - """ - Handler method for the "Lexer" start tag. - - @param attrs list of tag attributes - """ - language = attrs.get("name", "") - if language and language in self.lexers: - self.lexer = self.lexers[language] - else: - self.lexer = None - - def startStyle(self, attrs): - """ - Handler method for the "Style" start tag. - - @param attrs list of tag attributes - """ - self.buffer = "" - - if self.lexer is not None: - style = attrs.get("style") - if style is not None: - style = int(style) - - color = attrs.get("color") - if color is None: - color = self.lexer.defaultColor(style) - else: - color = QColor(color) - self.lexer.setColor(color, style) - - paper = attrs.get("paper") - if paper is None: - paper = self.lexer.defaultPaper(style) - else: - paper = QColor(paper) - self.lexer.setPaper(paper, style) - - fontStr = attrs.get("font") - if fontStr is None: - font = self.lexer.defaultFont(style) - else: - font = QFont() - font.fromString(fontStr) - self.lexer.setFont(font, style) - - eolfill = attrs.get("eolfill") - if eolfill is None: - eolfill = self.lexer.defaulEolFill(style) - else: - eolfill = int(eolfill) - self.lexer.setEolFill(eolfill, style) - - def getVersion(self): - """ - Public method to retrieve the version of the shortcuts. - - @return String containing the version number. - """ - return self.version
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/E5XML/HighlightingStylesReader.py Sun Sep 12 12:06:50 2010 +0200 @@ -0,0 +1,123 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2010 Detlev Offenbach <detlev@die-offenbachs.de> +# + + +""" +Module implementing a class for reading a highlighting styles XML file. +""" + +from PyQt4.QtGui import QColor, QFont + +from .Config import highlightingStylesFileFormatVersion +from .XMLStreamReaderBase import XMLStreamReaderBase + +class HighlightingStylesReader(XMLStreamReaderBase): + """ + Class for reading a highlighting styles XML file. + """ + supportedVersions = ["4.3"] + + def __init__(self, device, lexers): + """ + Constructor + + @param device reference to the I/O device to read from (QIODevice) + @param lexers list of lexer objects for which to export the styles + """ + XMLStreamReaderBase.__init__(self, device) + + self.lexers = lexers + + self.version = "" + + def readXML(self): + """ + Public method to read and parse the XML document. + """ + while not self.atEnd(): + self.readNext() + if self.isStartElement(): + if self.name() == "HighlightingStyles": + self.version = self.attribute("version", + highlightingStylesFileFormatVersion) + if self.version not in self.supportedVersions: + self.raiseUnsupportedFormatVersion(self.version) + elif self.name() == "Lexer": + self.__readLexer() + else: + self.raiseUnexpectedStartTag(self.name()) + + self.showErrorMessage() + + def __readLexer(self): + """ + Private method to read the lexer info. + """ + language = self.attribute("name") + if language and language in self.lexers: + lexer = self.lexers[language] + else: + lexer = None + + while not self.atEnd(): + self.readNext() + if self.isEndElement() and self.name() == "Lexer": + break + + if self.isStartElement(): + if self.name() == "Style": + self.__readStyle(lexer) + else: + self.raiseUnexpectedStartTag(self.name()) + + def __readStyle(self, lexer): + """ + Private method to read the style info. + + @param lexer reference to the lexer object + """ + if lexer is not None: + style = self.attribute("style") + if style: + style = int(style) + + color = self.attribute("color") + if color: + color = QColor(color) + else: + color = lexer.defaultColor(style) + lexer.setColor(color, style) + + paper = self.attribute("paper") + if paper: + paper = QColor(paper) + else: + paper = lexer.defaultPaper(style) + lexer.setPaper(paper, style) + + fontStr = self.attribute("font") + if fontStr: + font = QFont() + font.fromString(fontStr) + else: + font = lexer.defaultFont(style) + lexer.setFont(font, style) + + eolfill = self.attribute("eolfill") + if eolfill: + eolfill = self.toBool(eolfill) + if eolfill is None: + eolfill = lexer.defaulEolFill(style) + else: + eolfill = lexer.defaulEolFill(style) + lexer.setEolFill(eolfill, style) + + while not self.atEnd(): + self.readNext() + if self.isEndElement() and self.name() == "Style": + break + + if self.isStartElement(): + self.raiseUnexpectedStartTag(self.name())
--- a/E5XML/HighlightingStylesWriter.py Sun Sep 12 12:02:39 2010 +0200 +++ b/E5XML/HighlightingStylesWriter.py Sun Sep 12 12:06:50 2010 +0200 @@ -9,54 +9,58 @@ import time -from .XMLWriterBase import XMLWriterBase +from .XMLStreamWriterBase import XMLStreamWriterBase from .Config import highlightingStylesFileFormatVersion import Preferences -class HighlightingStylesWriter(XMLWriterBase): +class HighlightingStylesWriter(XMLStreamWriterBase): """ Class implementing the writer class for writing a highlighting styles XML file. """ - def __init__(self, file, lexers): + def __init__(self, device, lexers): """ Constructor - @param file open file (like) object for writing + @param device reference to the I/O device to write to (QIODevice) @param lexers list of lexer objects for which to export the styles """ - XMLWriterBase.__init__(self, file) + XMLStreamWriterBase.__init__(self, device) self.lexers = lexers self.email = Preferences.getUser("Email") - + def writeXML(self): """ Public method to write the XML to the file. """ - XMLWriterBase.writeXML(self) + XMLStreamWriterBase.writeXML(self) - self._write('<!DOCTYPE HighlightingStyles SYSTEM "HighlightingStyles-{0}.dtd">'\ + self.writeDTD('<!DOCTYPE HighlightingStyles SYSTEM "HighlightingStyles-{0}.dtd">'\ .format(highlightingStylesFileFormatVersion)) # add some generation comments - self._write("<!-- Eric5 highlighting styles -->") - self._write("<!-- Saved: {0} -->".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) - self._write("<!-- Author: {0} -->".format(self.escape("{0}".format(self.email)))) + self.writeComment(" Eric5 highlighting styles ") + self.writeComment(" Saved: {0}".format(time.strftime('%Y-%m-%d, %H:%M:%S'))) + self.writeComment(" Author: {0} ".format(self.email)) # add the main tag - self._write('<HighlightingStyles version="{0}">'.format( - highlightingStylesFileFormatVersion)) + self.writeStartElement("HighlightingStyles") + self.writeAttribute("version", highlightingStylesFileFormatVersion) for lexer in self.lexers: - self._write(' <Lexer name="{0}">'.format(lexer.language())) + self.writeStartElement("Lexer") + self.writeAttribute("name", lexer.language()) for style in lexer.descriptions: - self._write(' <Style style="{0:d}" ' - 'color="{1}" paper="{2}" font="{3}" eolfill="{4:d}">{5}</Style>'\ - .format(style, lexer.color(style).name(), lexer.paper(style).name(), - lexer.font(style).toString(), lexer.eolFill(style), - self.escape(lexer.description(style))) - ) - self._write(' </Lexer>') + self.writeStartElement("Style") + self.writeAttribute("style", str(style)) + self.writeAttribute("color", lexer.color(style).name()) + self.writeAttribute("paper", lexer.paper(style).name()) + self.writeAttribute("font", lexer.font(style).toString()) + self.writeAttribute("eolfill", str(lexer.eolFill(style))) + self.writeCharacters(lexer.description(style)) + self.writeEndElement() + self.writeEndElement() - self._write("</HighlightingStyles>", newline = False) + self.writeEndElement() + self.writeEndDocument()
--- a/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Sun Sep 12 12:02:39 2010 +0200 +++ b/Preferences/ConfigurationPages/EditorHighlightingStylesPage.py Sun Sep 12 12:06:50 2010 +0200 @@ -7,9 +7,7 @@ Module implementing the Editor Highlighting Styles configuration page. """ -import io - -from PyQt4.QtCore import pyqtSlot, QFileInfo +from PyQt4.QtCore import pyqtSlot, QFileInfo, QFile, QIODevice from PyQt4.QtGui import QPalette, QFileDialog, QColorDialog, QFontDialog, \ QInputDialog, QFont, QMenu @@ -18,11 +16,8 @@ from E5Gui import E5MessageBox -from E5XML.XMLUtilities import make_parser -from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError -from E5XML.XMLEntityResolver import XMLEntityResolver from E5XML.HighlightingStylesWriter import HighlightingStylesWriter -from E5XML.HighlightingStylesHandler import HighlightingStylesHandler +from E5XML.HighlightingStylesReader import HighlightingStylesReader import Preferences @@ -385,16 +380,16 @@ if ex: fn += ex - try: - f = open(fn, "w", encoding = "utf-8") + f = QFile(fn) + if f.open(QIODevice.WriteOnly): HighlightingStylesWriter(f, lexers).writeXML() f.close() - except IOError as err: + else: E5MessageBox.critical(self, self.trUtf8("Export Highlighting Styles"), self.trUtf8("""<p>The highlighting styles could not be exported""" """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ - .format(fn, str(err)) + .format(fn, f.errorString()) ) def __importStyles(self, lexers): @@ -412,61 +407,19 @@ if not fn: return - try: - f = open(fn, "r", encoding = "utf-8") - try: - f.readline() - dtdLine = f.readline() - finally: - f.close() - except IOError as err: + f = QFile(fn) + if f.open(QIODevice.ReadOnly): + reader = HighlightingStylesReader(f, lexers) + reader.readXML() + else: E5MessageBox.critical(self, self.trUtf8("Import Highlighting Styles"), self.trUtf8("""<p>The highlighting styles could not be read""" """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ - .format(fn, str(err)) + .format(fn, f.errorString()) ) return - validating = dtdLine.startswith("<!DOCTYPE") - parser = make_parser(validating) - handler = HighlightingStylesHandler(lexers) - er = XMLEntityResolver() - eh = XMLErrorHandler() - - parser.setContentHandler(handler) - parser.setEntityResolver(er) - parser.setErrorHandler(eh) - - try: - f = open(fn, "r", encoding = "utf-8") - try: - try: - parser.parse(f) - except UnicodeEncodeError: - f.seek(0) - buf = io.StringIO(f.read()) - parser.parse(buf) - finally: - f.close() - except IOError as err: - E5MessageBox.critical(self, - self.trUtf8("Import Highlighting Styles"), - self.trUtf8("""<p>The highlighting styles could not be read""" - """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ - .format(fn, str(err)) - ) - return - except XMLFatalParseError: - E5MessageBox.critical(self, - self.trUtf8("Import Highlighting Styles"), - self.trUtf8("""<p>The highlighting styles file <b>{0}</b>""" - """ has invalid contents.</p>""").format(fn)) - eh.showParseMessages() - return - - eh.showParseMessages() - if self.lexer: colour = self.lexer.color(self.style) paper = self.lexer.paper(self.style) @@ -511,4 +464,4 @@ @param dlg reference to the configuration dialog """ page = EditorHighlightingStylesPage(dlg.getLexers()) - return page \ No newline at end of file + return page
--- a/eric5.e4p Sun Sep 12 12:02:39 2010 +0200 +++ b/eric5.e4p Sun Sep 12 12:06:50 2010 +0200 @@ -725,7 +725,6 @@ <Source>E5XML/MultiProjectWriter.py</Source> <Source>E5XML/MultiProjectHandler.py</Source> <Source>E5XML/HighlightingStylesWriter.py</Source> - <Source>E5XML/HighlightingStylesHandler.py</Source> <Source>E5XML/XMLUtilities.py</Source> <Source>E5XML/XMLHandlerBase.py</Source> <Source>E5XML/XMLWriterBase.py</Source> @@ -821,6 +820,7 @@ <Source>E5XML/PluginRepositoryReader.py</Source> <Source>E5XML/TasksReader.py</Source> <Source>E5XML/TemplatesReader.py</Source> + <Source>E5XML/HighlightingStylesReader.py</Source> </Sources> <Forms> <Form>PyUnit/UnittestDialog.ui</Form>