|
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 |
|
15 from E5Gui import E5MessageBox |
|
16 from E5Gui.E5OverrideCursor import E5OverridenCursor |
|
17 |
|
18 import Preferences |
|
19 |
|
20 |
|
21 class HighlightingStylesFile(QObject): |
|
22 """ |
|
23 Class representing the highlighting styles JSON file. |
|
24 """ |
|
25 def __init__(self, parent: QObject = None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent object (defaults to None) |
|
30 @type QObject (optional) |
|
31 """ |
|
32 super().__init__(parent) |
|
33 |
|
34 self.__lexerAliases = { |
|
35 "PO": "Gettext", |
|
36 "POV": "Povray", |
|
37 } |
|
38 |
|
39 def writeFile(self, filename: str, lexers: list) -> bool: |
|
40 """ |
|
41 Public method to write the highlighting styles data to a highlighting |
|
42 styles JSON file. |
|
43 |
|
44 @param filename name of the highlighting styles file |
|
45 @type str |
|
46 @param lexers list of lexers for which to export the styles |
|
47 @type list of PreferencesLexer |
|
48 @return flag indicating a successful write |
|
49 @rtype bool |
|
50 """ |
|
51 stylesDict = {} |
|
52 # step 0: header |
|
53 stylesDict["header"] = { |
|
54 "comment": "eric highlighting styles file", |
|
55 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), |
|
56 "author": Preferences.getUser("Email"), |
|
57 } |
|
58 |
|
59 # step 1: add the lexer style data |
|
60 stylesDict["lexers"] = [] |
|
61 for lexer in lexers: |
|
62 name = lexer.language() |
|
63 if name in self.__lexerAliases: |
|
64 name = self.__lexerAliases[name] |
|
65 lexerDict = { |
|
66 "name": name, |
|
67 "styles": [], |
|
68 } |
|
69 for description, style, substyle in lexer.getStyles(): |
|
70 lexerDict["styles"].append({ |
|
71 "description": description, |
|
72 "style": style, |
|
73 "substyle": substyle, |
|
74 "color": lexer.color(style, substyle).name(), |
|
75 "paper": lexer.paper(style, substyle).name(), |
|
76 "font": lexer.font(style, substyle).toString(), |
|
77 "eolfill": lexer.eolFill(style, substyle), |
|
78 "words": lexer.words(style, substyle).strip(), |
|
79 }) |
|
80 stylesDict["lexers"].append(lexerDict) |
|
81 |
|
82 try: |
|
83 jsonString = json.dumps(stylesDict, indent=2) |
|
84 with open(filename, "w") as f: |
|
85 f.write(jsonString) |
|
86 except (TypeError, OSError) as err: |
|
87 with E5OverridenCursor(): |
|
88 E5MessageBox.critical( |
|
89 None, |
|
90 self.tr("Export Highlighting Styles"), |
|
91 self.tr( |
|
92 "<p>The highlighting styles file <b>{0}</b> could not" |
|
93 " be written.</p><p>Reason: {1}</p>" |
|
94 ).format(filename, str(err)) |
|
95 ) |
|
96 return False |
|
97 |
|
98 return True |
|
99 |
|
100 def readFile(self, filename: str) -> list: |
|
101 """ |
|
102 Public method to read the highlighting styles data from a highlighting |
|
103 styles JSON file. |
|
104 |
|
105 @param filename name of the highlighting styles file |
|
106 @type str |
|
107 @return list of read lexer style definitions |
|
108 @rtype list of dict |
|
109 """ |
|
110 try: |
|
111 with open(filename, "r") as f: |
|
112 jsonString = f.read() |
|
113 stylesDict = json.loads(jsonString) |
|
114 except (OSError, json.JSONDecodeError) as err: |
|
115 E5MessageBox.critical( |
|
116 None, |
|
117 self.tr("Import Highlighting Styles"), |
|
118 self.tr( |
|
119 "<p>The highlighting styles file <b>{0}</b> could not be" |
|
120 " read.</p><p>Reason: {1}</p>" |
|
121 ).format(filename, str(err)) |
|
122 ) |
|
123 return [] |
|
124 |
|
125 return stylesDict["lexers"] |