Thu, 28 Jan 2021 18:29:00 +0100
Implemented the JSON based highlighting styles files.
# -*- coding: utf-8 -*- # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a class representing the highlighting styles JSON file. """ import json import time from PyQt5.QtCore import QObject from PyQt5.QtGui import QColor, QFont from E5Gui import E5MessageBox from E5Gui.E5OverrideCursor import E5OverridenCursor import Preferences class HighlightingStylesFile(QObject): """ Class representing the highlighting styles JSON file. """ def __init__(self, parent: QObject = None): """ Constructor @param parent reference to the parent object (defaults to None) @type QObject (optional) """ super(HighlightingStylesFile, self).__init__(parent) def writeFile(self, filename: str, lexers: list) -> bool: """ Public method to write the highlighting styles data to a highlighting styles JSON file. @param filename name of the highlighting styles file @type str @param lexers list of lexers for which to export the styles @type list of PreferencesLexer @return flag indicating a successful write @rtype bool """ stylesDict = {} # step 0: header stylesDict["header"] = { "comment": "eric highlighting styles file", "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), "author": Preferences.getUser("Email"), } # step 1: add the lexer style data stylesDict["lexers"] = [] for lexer in lexers: lexerDict = { "name": lexer.language(), "styles": [], } for description, style, substyle in lexer.getStyles(): lexerDict["styles"].append({ "description": description, "style": style, "substyle": substyle, "color": lexer.color(style, substyle).name(), "paper": lexer.paper(style, substyle).name(), "font": lexer.font(style, substyle).toString(), "eolfill": lexer.eolFill(style, substyle), "words": lexer.words(style, substyle).strip(), }) stylesDict["lexers"].append(lexerDict) try: jsonString = json.dumps(stylesDict, indent=2) with open(filename, "w") as f: f.write(jsonString) except (TypeError, EnvironmentError) as err: with E5OverridenCursor(): E5MessageBox.critical( None, self.tr("Export Highlighting Styles"), self.tr( "<p>The highlighting styles file <b>{0}</b> could not" " be written.</p><p>Reason: {1}</p>" ).format(filename, str(err)) ) return False return True def readFile(self, filename: str, lexers: dict) -> bool: """ Public method to read the highlighting styles data from a highlighting styles JSON file. @param filename name of the highlighting styles file @type str @param lexers dictionary of lexer objects for which to import the styles @type dict of {str: PreferencesLexer} @return flag indicating a successful read @rtype bool """ try: with open(filename, "r") as f: jsonString = f.read() stylesDict = json.loads(jsonString) except (EnvironmentError, json.JSONDecodeError) as err: E5MessageBox.critical( None, self.tr("Import Highlighting Styles"), self.tr( "<p>The highlighting styles file <b>{0}</b> could not be" " read.</p><p>Reason: {1}</p>" ).format(filename, str(err)) ) return False for lexerDict in stylesDict["lexers"]: if lexerDict["name"] in lexers: lexer = lexers[lexerDict["name"]] for styleDict in lexerDict["styles"]: style = styleDict["style"] substyle = styleDict["substyle"] lexer.setColor(QColor(styleDict["color"]), style, substyle) lexer.setPaper(QColor(styleDict["paper"]), style, substyle) font = QFont() font.fromString(styleDict["font"]) lexer.setFont(font, style, substyle) lexer.setEolFill(styleDict["eolfill"], style, substyle) if substyle >= 0: # description and words can only be set for sub-styles lexer.setDescription(styleDict["description"], style, substyle) lexer.setWords(styleDict["words"], style, substyle) return True