5 |
5 |
6 """ |
6 """ |
7 Module implementing the Editor Highlighting Styles configuration page. |
7 Module implementing the Editor Highlighting Styles configuration page. |
8 """ |
8 """ |
9 |
9 |
10 import io |
10 from PyQt4.QtCore import pyqtSlot, QFileInfo, QFile, QIODevice |
11 |
|
12 from PyQt4.QtCore import pyqtSlot, QFileInfo |
|
13 from PyQt4.QtGui import QPalette, QFileDialog, QColorDialog, QFontDialog, \ |
11 from PyQt4.QtGui import QPalette, QFileDialog, QColorDialog, QFontDialog, \ |
14 QInputDialog, QFont, QMenu |
12 QInputDialog, QFont, QMenu |
15 |
13 |
16 from .ConfigurationPageBase import ConfigurationPageBase |
14 from .ConfigurationPageBase import ConfigurationPageBase |
17 from .Ui_EditorHighlightingStylesPage import Ui_EditorHighlightingStylesPage |
15 from .Ui_EditorHighlightingStylesPage import Ui_EditorHighlightingStylesPage |
18 |
16 |
19 from E5Gui import E5MessageBox |
17 from E5Gui import E5MessageBox |
20 |
18 |
21 from E5XML.XMLUtilities import make_parser |
|
22 from E5XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError |
|
23 from E5XML.XMLEntityResolver import XMLEntityResolver |
|
24 from E5XML.HighlightingStylesWriter import HighlightingStylesWriter |
19 from E5XML.HighlightingStylesWriter import HighlightingStylesWriter |
25 from E5XML.HighlightingStylesHandler import HighlightingStylesHandler |
20 from E5XML.HighlightingStylesReader import HighlightingStylesReader |
26 |
21 |
27 import Preferences |
22 import Preferences |
28 |
23 |
29 class EditorHighlightingStylesPage(ConfigurationPageBase, |
24 class EditorHighlightingStylesPage(ConfigurationPageBase, |
30 Ui_EditorHighlightingStylesPage): |
25 Ui_EditorHighlightingStylesPage): |
383 if not ext: |
378 if not ext: |
384 ex = selectedFilter.split("(*")[1].split(")")[0] |
379 ex = selectedFilter.split("(*")[1].split(")")[0] |
385 if ex: |
380 if ex: |
386 fn += ex |
381 fn += ex |
387 |
382 |
388 try: |
383 f = QFile(fn) |
389 f = open(fn, "w", encoding = "utf-8") |
384 if f.open(QIODevice.WriteOnly): |
390 HighlightingStylesWriter(f, lexers).writeXML() |
385 HighlightingStylesWriter(f, lexers).writeXML() |
391 f.close() |
386 f.close() |
392 except IOError as err: |
387 else: |
393 E5MessageBox.critical(self, |
388 E5MessageBox.critical(self, |
394 self.trUtf8("Export Highlighting Styles"), |
389 self.trUtf8("Export Highlighting Styles"), |
395 self.trUtf8("""<p>The highlighting styles could not be exported""" |
390 self.trUtf8("""<p>The highlighting styles could not be exported""" |
396 """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
391 """ to file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
397 .format(fn, str(err)) |
392 .format(fn, f.errorString()) |
398 ) |
393 ) |
399 |
394 |
400 def __importStyles(self, lexers): |
395 def __importStyles(self, lexers): |
401 """ |
396 """ |
402 Private method to import the styles of the given lexers. |
397 Private method to import the styles of the given lexers. |
410 self.trUtf8("Highlighting styles file (*.e4h)")) |
405 self.trUtf8("Highlighting styles file (*.e4h)")) |
411 |
406 |
412 if not fn: |
407 if not fn: |
413 return |
408 return |
414 |
409 |
415 try: |
410 f = QFile(fn) |
416 f = open(fn, "r", encoding = "utf-8") |
411 if f.open(QIODevice.ReadOnly): |
417 try: |
412 reader = HighlightingStylesReader(f, lexers) |
418 f.readline() |
413 reader.readXML() |
419 dtdLine = f.readline() |
414 else: |
420 finally: |
|
421 f.close() |
|
422 except IOError as err: |
|
423 E5MessageBox.critical(self, |
415 E5MessageBox.critical(self, |
424 self.trUtf8("Import Highlighting Styles"), |
416 self.trUtf8("Import Highlighting Styles"), |
425 self.trUtf8("""<p>The highlighting styles could not be read""" |
417 self.trUtf8("""<p>The highlighting styles could not be read""" |
426 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
418 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
427 .format(fn, str(err)) |
419 .format(fn, f.errorString()) |
428 ) |
420 ) |
429 return |
421 return |
430 |
|
431 validating = dtdLine.startswith("<!DOCTYPE") |
|
432 parser = make_parser(validating) |
|
433 handler = HighlightingStylesHandler(lexers) |
|
434 er = XMLEntityResolver() |
|
435 eh = XMLErrorHandler() |
|
436 |
|
437 parser.setContentHandler(handler) |
|
438 parser.setEntityResolver(er) |
|
439 parser.setErrorHandler(eh) |
|
440 |
|
441 try: |
|
442 f = open(fn, "r", encoding = "utf-8") |
|
443 try: |
|
444 try: |
|
445 parser.parse(f) |
|
446 except UnicodeEncodeError: |
|
447 f.seek(0) |
|
448 buf = io.StringIO(f.read()) |
|
449 parser.parse(buf) |
|
450 finally: |
|
451 f.close() |
|
452 except IOError as err: |
|
453 E5MessageBox.critical(self, |
|
454 self.trUtf8("Import Highlighting Styles"), |
|
455 self.trUtf8("""<p>The highlighting styles could not be read""" |
|
456 """ from file <b>{0}</b>.</p><p>Reason: {1}</p>""")\ |
|
457 .format(fn, str(err)) |
|
458 ) |
|
459 return |
|
460 except XMLFatalParseError: |
|
461 E5MessageBox.critical(self, |
|
462 self.trUtf8("Import Highlighting Styles"), |
|
463 self.trUtf8("""<p>The highlighting styles file <b>{0}</b>""" |
|
464 """ has invalid contents.</p>""").format(fn)) |
|
465 eh.showParseMessages() |
|
466 return |
|
467 |
|
468 eh.showParseMessages() |
|
469 |
422 |
470 if self.lexer: |
423 if self.lexer: |
471 colour = self.lexer.color(self.style) |
424 colour = self.lexer.color(self.style) |
472 paper = self.lexer.paper(self.style) |
425 paper = self.lexer.paper(self.style) |
473 eolfill = self.lexer.eolFill(self.style) |
426 eolfill = self.lexer.eolFill(self.style) |