|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class representing the highlighting styles JSON file. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import time |
|
12 |
|
13 from PyQt5.QtCore import QObject |
|
14 from PyQt5.QtGui import QColor, QFont |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 from E5Gui.E5OverrideCursor import E5OverridenCursor |
|
18 |
|
19 import Preferences |
|
20 |
|
21 |
|
22 class HighlightingStylesFile(QObject): |
|
23 """ |
|
24 Class representing the highlighting styles JSON file. |
|
25 """ |
|
26 def __init__(self, parent: QObject = None): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param parent reference to the parent object (defaults to None) |
|
31 @type QObject (optional) |
|
32 """ |
|
33 super(HighlightingStylesFile, self).__init__(parent) |
|
34 |
|
35 def writeFile(self, filename: str, lexers: list) -> bool: |
|
36 """ |
|
37 Public method to write the highlighting styles data to a highlighting |
|
38 styles JSON file. |
|
39 |
|
40 @param filename name of the highlighting styles file |
|
41 @type str |
|
42 @param lexers list of lexers for which to export the styles |
|
43 @type list of PreferencesLexer |
|
44 @return flag indicating a successful write |
|
45 @rtype bool |
|
46 """ |
|
47 stylesDict = {} |
|
48 # step 0: header |
|
49 stylesDict["header"] = { |
|
50 "comment": "eric highlighting styles file", |
|
51 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), |
|
52 "author": Preferences.getUser("Email"), |
|
53 } |
|
54 |
|
55 # step 1: add the lexer style data |
|
56 stylesDict["lexers"] = [] |
|
57 for lexer in lexers: |
|
58 lexerDict = { |
|
59 "name": lexer.language(), |
|
60 "styles": [], |
|
61 } |
|
62 for description, style, substyle in lexer.getStyles(): |
|
63 lexerDict["styles"].append({ |
|
64 "description": description, |
|
65 "style": style, |
|
66 "substyle": substyle, |
|
67 "color": lexer.color(style, substyle).name(), |
|
68 "paper": lexer.paper(style, substyle).name(), |
|
69 "font": lexer.font(style, substyle).toString(), |
|
70 "eolfill": lexer.eolFill(style, substyle), |
|
71 "words": lexer.words(style, substyle).strip(), |
|
72 }) |
|
73 stylesDict["lexers"].append(lexerDict) |
|
74 |
|
75 try: |
|
76 jsonString = json.dumps(stylesDict, indent=2) |
|
77 with open(filename, "w") as f: |
|
78 f.write(jsonString) |
|
79 except (TypeError, EnvironmentError) as err: |
|
80 with E5OverridenCursor(): |
|
81 E5MessageBox.critical( |
|
82 None, |
|
83 self.tr("Export Highlighting Styles"), |
|
84 self.tr( |
|
85 "<p>The highlighting styles file <b>{0}</b> could not" |
|
86 " be written.</p><p>Reason: {1}</p>" |
|
87 ).format(filename, str(err)) |
|
88 ) |
|
89 return False |
|
90 |
|
91 return True |
|
92 |
|
93 def readFile(self, filename: str, lexers: dict) -> bool: |
|
94 """ |
|
95 Public method to read the highlighting styles data from a highlighting |
|
96 styles JSON file. |
|
97 |
|
98 @param filename name of the highlighting styles file |
|
99 @type str |
|
100 @param lexers dictionary of lexer objects for which to import the |
|
101 styles |
|
102 @type dict of {str: PreferencesLexer} |
|
103 @return flag indicating a successful read |
|
104 @rtype bool |
|
105 """ |
|
106 try: |
|
107 with open(filename, "r") as f: |
|
108 jsonString = f.read() |
|
109 stylesDict = json.loads(jsonString) |
|
110 except (EnvironmentError, json.JSONDecodeError) as err: |
|
111 E5MessageBox.critical( |
|
112 None, |
|
113 self.tr("Import Highlighting Styles"), |
|
114 self.tr( |
|
115 "<p>The highlighting styles file <b>{0}</b> could not be" |
|
116 " read.</p><p>Reason: {1}</p>" |
|
117 ).format(filename, str(err)) |
|
118 ) |
|
119 return False |
|
120 |
|
121 for lexerDict in stylesDict["lexers"]: |
|
122 if lexerDict["name"] in lexers: |
|
123 lexer = lexers[lexerDict["name"]] |
|
124 for styleDict in lexerDict["styles"]: |
|
125 style = styleDict["style"] |
|
126 substyle = styleDict["substyle"] |
|
127 lexer.setColor(QColor(styleDict["color"]), style, substyle) |
|
128 lexer.setPaper(QColor(styleDict["paper"]), style, substyle) |
|
129 font = QFont() |
|
130 font.fromString(styleDict["font"]) |
|
131 lexer.setFont(font, style, substyle) |
|
132 lexer.setEolFill(styleDict["eolfill"], style, substyle) |
|
133 if substyle >= 0: |
|
134 # description and words can only be set for sub-styles |
|
135 lexer.setDescription(styleDict["description"], |
|
136 style, substyle) |
|
137 lexer.setWords(styleDict["words"], style, substyle) |
|
138 |
|
139 return True |