eric6/Preferences/HighlightingStylesFile.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 8103
338fe0064e5a
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class representing the highlighting styles JSON file.
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import json
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import time
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 from PyQt5.QtCore import QObject
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from E5Gui import E5MessageBox
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from E5Gui.E5OverrideCursor import E5OverridenCursor
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 import Preferences
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 class HighlightingStylesFile(QObject):
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 Class representing the highlighting styles JSON file.
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 def __init__(self, parent: QObject = None):
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Constructor
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 @param parent reference to the parent object (defaults to None)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 @type QObject (optional)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 super(HighlightingStylesFile, self).__init__(parent)
8103
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
33
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
34 self.__lexerAliases = {
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
35 "PO": "Gettext",
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
36 "POV": "Povray",
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
37 }
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 def writeFile(self, filename: str, lexers: list) -> bool:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 Public method to write the highlighting styles data to a highlighting
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 styles JSON file.
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 @param filename name of the highlighting styles file
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @type str
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @param lexers list of lexers for which to export the styles
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @type list of PreferencesLexer
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @return flag indicating a successful write
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @rtype bool
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 stylesDict = {}
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 # step 0: header
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 stylesDict["header"] = {
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 "comment": "eric highlighting styles file",
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 "author": Preferences.getUser("Email"),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 }
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 # step 1: add the lexer style data
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 stylesDict["lexers"] = []
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 for lexer in lexers:
8103
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
62 name = lexer.language()
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
63 if name in self.__lexerAliases:
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
64 name = self.__lexerAliases[name]
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 lexerDict = {
8103
338fe0064e5a HighlightingStylesFile: added code for some aliased lexer names where the QScintilla name and the one used in eric are not identical.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8028
diff changeset
66 "name": name,
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 "styles": [],
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 }
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 for description, style, substyle in lexer.getStyles():
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 lexerDict["styles"].append({
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 "description": description,
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 "style": style,
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 "substyle": substyle,
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 "color": lexer.color(style, substyle).name(),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 "paper": lexer.paper(style, substyle).name(),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 "font": lexer.font(style, substyle).toString(),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 "eolfill": lexer.eolFill(style, substyle),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 "words": lexer.words(style, substyle).strip(),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 })
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 stylesDict["lexers"].append(lexerDict)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 try:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 jsonString = json.dumps(stylesDict, indent=2)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 with open(filename, "w") as f:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 f.write(jsonString)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 except (TypeError, EnvironmentError) as err:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 with E5OverridenCursor():
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 E5MessageBox.critical(
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 None,
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 self.tr("Export Highlighting Styles"),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 self.tr(
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 "<p>The highlighting styles file <b>{0}</b> could not"
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 " be written.</p><p>Reason: {1}</p>"
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 ).format(filename, str(err))
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 )
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 return False
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 return True
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
8028
a4f1b68c0737 Highlighting Styles: changed the import/export logic to allow the selection of lexers to be imported/exported.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8026
diff changeset
100 def readFile(self, filename: str) -> list:
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 Public method to read the highlighting styles data from a highlighting
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 styles JSON file.
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 @param filename name of the highlighting styles file
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 @type str
8026
d3eacdbcb18b Changed the highlighting styles import logic thus, that the imported styles are applied in the configuration page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8022
diff changeset
107 @return list of read lexer style definitions
d3eacdbcb18b Changed the highlighting styles import logic thus, that the imported styles are applied in the configuration page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8022
diff changeset
108 @rtype list of dict
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 """
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 try:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 with open(filename, "r") as f:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 jsonString = f.read()
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 stylesDict = json.loads(jsonString)
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 except (EnvironmentError, json.JSONDecodeError) as err:
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 E5MessageBox.critical(
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 None,
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 self.tr("Import Highlighting Styles"),
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 self.tr(
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 "<p>The highlighting styles file <b>{0}</b> could not be"
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 " read.</p><p>Reason: {1}</p>"
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 ).format(filename, str(err))
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 )
8026
d3eacdbcb18b Changed the highlighting styles import logic thus, that the imported styles are applied in the configuration page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8022
diff changeset
123 return []
8022
2da0139f4f91 Implemented the JSON based highlighting styles files.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124
8026
d3eacdbcb18b Changed the highlighting styles import logic thus, that the imported styles are applied in the configuration page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8022
diff changeset
125 return stylesDict["lexers"]

eric ide

mercurial