15 from PyQt6.QtCore import QObject |
15 from PyQt6.QtCore import QObject |
16 |
16 |
17 from eric7 import Preferences |
17 from eric7 import Preferences |
18 from eric7.EricGui.EricOverrideCursor import EricOverridenCursor |
18 from eric7.EricGui.EricOverrideCursor import EricOverridenCursor |
19 from eric7.EricWidgets import EricMessageBox |
19 from eric7.EricWidgets import EricMessageBox |
|
20 from eric7.EricWidgets.EricApplication import ericApp |
20 from eric7.SystemUtilities import FileSystemUtilities |
21 from eric7.SystemUtilities import FileSystemUtilities |
21 |
22 |
22 Project = typing.TypeVar("Project") |
23 Project = typing.TypeVar("Project") |
23 |
24 |
24 |
25 |
83 "TRANSLATIONSOURCESTARTPATH", |
89 "TRANSLATIONSOURCESTARTPATH", |
84 "MAINSCRIPT", |
90 "MAINSCRIPT", |
85 "SOURCESDIR", |
91 "SOURCESDIR", |
86 ): |
92 ): |
87 with contextlib.suppress(KeyError): |
93 with contextlib.suppress(KeyError): |
88 projectDict["project"][key] = FileSystemUtilities.fromNativeSeparators( |
94 projectDict["project"][key] = ( |
89 projectDict["project"][key] |
95 fsInterface.fromNativeSeparators(projectDict["project"][key]) |
|
96 if isRemote |
|
97 else FileSystemUtilities.fromNativeSeparators( |
|
98 projectDict["project"][key] |
|
99 ) |
90 ) |
100 ) |
91 |
101 |
92 try: |
102 try: |
93 jsonString = json.dumps(projectDict, indent=2, sort_keys=True) + "\n" |
103 jsonString = json.dumps(projectDict, indent=2, sort_keys=True) + "\n" |
94 with open(filename, "w", newline="") as f: |
104 if isRemote: |
95 f.write(jsonString) |
105 title = self.tr("Save Remote Project File") |
|
106 fsInterface.writeFile(filename, jsonString.encode("utf-8")) |
|
107 else: |
|
108 title = self.tr("Save Project File") |
|
109 with open(filename, "w", newline="") as f: |
|
110 f.write(jsonString) |
96 except (OSError, TypeError) as err: |
111 except (OSError, TypeError) as err: |
97 with EricOverridenCursor(): |
112 with EricOverridenCursor(): |
98 EricMessageBox.critical( |
113 EricMessageBox.critical( |
99 None, |
114 None, |
100 self.tr("Save Project File"), |
115 title, |
101 self.tr( |
116 self.tr( |
102 "<p>The project file <b>{0}</b> could not be " |
117 "<p>The project file <b>{0}</b> could not be " |
103 "written.</p><p>Reason: {1}</p>" |
118 "written.</p><p>Reason: {1}</p>" |
104 ).format(filename, str(err)), |
119 ).format(filename, str(err)), |
105 ) |
120 ) |
114 @param filename name of the project file |
129 @param filename name of the project file |
115 @type str |
130 @type str |
116 @return flag indicating a successful read |
131 @return flag indicating a successful read |
117 @rtype bool |
132 @rtype bool |
118 """ |
133 """ |
|
134 fsInterface = ( |
|
135 ericApp().getObject("EricServer").getServiceInterface("FileSystem") |
|
136 ) |
|
137 |
|
138 isRemote = FileSystemUtilities.isRemoteFileName(filename) |
119 try: |
139 try: |
120 with open(filename, "r") as f: |
140 if isRemote: |
121 jsonString = f.read() |
141 title = self.tr("Read Remote Project File") |
|
142 jsonString = fsInterface.readFile(filename).decode("utf-8") |
|
143 else: |
|
144 title = self.tr("Read Project File") |
|
145 with open(filename, "r") as f: |
|
146 jsonString = f.read() |
122 projectDict = json.loads(jsonString) |
147 projectDict = json.loads(jsonString) |
123 except (OSError, json.JSONDecodeError) as err: |
148 except (OSError, json.JSONDecodeError) as err: |
124 EricMessageBox.critical( |
149 EricMessageBox.critical( |
125 None, |
150 None, |
126 self.tr("Read Project File"), |
151 title, |
127 self.tr( |
152 self.tr( |
128 "<p>The project file <b>{0}</b> could not be " |
153 "<p>The project file <b>{0}</b> could not be " |
129 "read.</p><p>Reason: {1}</p>" |
154 "read.</p><p>Reason: {1}</p>" |
130 ).format(filename, str(err)), |
155 ).format(filename, str(err)), |
131 ) |
156 ) |
132 return False |
157 return False |
133 |
158 |
134 # modify paths to contain native separators |
159 # modify paths to contain native separators |
135 for key in self.__project.getFileCategories() + ["TRANSLATIONEXCEPTIONS"]: |
160 for key in self.__project.getFileCategories() + ["TRANSLATIONEXCEPTIONS"]: |
136 with contextlib.suppress(KeyError): |
161 with contextlib.suppress(KeyError): |
137 projectDict["project"][key] = [ |
162 projectDict["project"][key] = ( |
138 FileSystemUtilities.toNativeSeparators(f) |
163 [ |
139 for f in projectDict["project"][key] |
164 fsInterface.toNativeSeparators(f) |
140 ] |
165 for f in projectDict["project"][key] |
|
166 ] |
|
167 if isRemote |
|
168 else [ |
|
169 FileSystemUtilities.toNativeSeparators(f) |
|
170 for f in projectDict["project"][key] |
|
171 ] |
|
172 ) |
141 for key in ( |
173 for key in ( |
142 "SPELLWORDS", |
174 "SPELLWORDS", |
143 "SPELLEXCLUDES", |
175 "SPELLEXCLUDES", |
144 "TRANSLATIONPATTERN", |
176 "TRANSLATIONPATTERN", |
145 "TRANSLATIONSBINPATH", |
177 "TRANSLATIONSBINPATH", |
146 "TRANSLATIONSOURCESTARTPATH", |
178 "TRANSLATIONSOURCESTARTPATH", |
147 "MAINSCRIPT", |
179 "MAINSCRIPT", |
148 "SOURCESDIR", |
180 "SOURCESDIR", |
149 ): |
181 ): |
150 with contextlib.suppress(KeyError): |
182 with contextlib.suppress(KeyError): |
151 projectDict["project"][key] = FileSystemUtilities.toNativeSeparators( |
183 projectDict["project"][key] = ( |
152 projectDict["project"][key] |
184 fsInterface.toNativeSeparators(projectDict["project"][key]) |
|
185 if isRemote |
|
186 else FileSystemUtilities.toNativeSeparators( |
|
187 projectDict["project"][key] |
|
188 ) |
153 ) |
189 ) |
154 |
190 |
155 self.__project.setProjectData(projectDict["project"]) |
191 self.__project.setProjectData(projectDict["project"]) |
156 |
192 |
157 return True |
193 return True |