src/eric7/Preferences/HighlightingStylesFile.py

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

eric ide

mercurial