14 from PyQt6.QtCore import QObject |
14 from PyQt6.QtCore import QObject |
15 |
15 |
16 from eric7 import Preferences |
16 from eric7 import Preferences |
17 from eric7.EricGui.EricOverrideCursor import EricOverridenCursor |
17 from eric7.EricGui.EricOverrideCursor import EricOverridenCursor |
18 from eric7.EricWidgets import EricMessageBox |
18 from eric7.EricWidgets import EricMessageBox |
|
19 from eric7.EricWidgets.EricApplication import ericApp |
|
20 from eric7.SystemUtilities import FileSystemUtilities |
19 |
21 |
20 Project = typing.TypeVar("Project") |
22 Project = typing.TypeVar("Project") |
21 |
23 |
22 |
24 |
23 class UserProjectFile(QObject): |
25 class UserProjectFile(QObject): |
45 @param filename name of the user project file |
47 @param filename name of the user project file |
46 @type str |
48 @type str |
47 @return flag indicating a successful write |
49 @return flag indicating a successful write |
48 @rtype bool |
50 @rtype bool |
49 """ |
51 """ |
|
52 fsInterface = ( |
|
53 ericApp().getObject("EricServer").getServiceInterface("FileSystem") |
|
54 ) |
|
55 |
50 userProjectDict = { |
56 userProjectDict = { |
51 "header": { |
57 "header": { |
52 "comment": "eric user project file for project {0}".format( |
58 "comment": "eric user project file for project {0}".format( |
53 self.__project.getProjectName() |
59 self.__project.getProjectName() |
54 ), |
60 ), |
60 |
66 |
61 userProjectDict["user_data"] = self.__project.pudata |
67 userProjectDict["user_data"] = self.__project.pudata |
62 |
68 |
63 try: |
69 try: |
64 jsonString = json.dumps(userProjectDict, indent=2) + "\n" |
70 jsonString = json.dumps(userProjectDict, indent=2) + "\n" |
65 with open(filename, "w") as f: |
71 if FileSystemUtilities.isRemoteFileName(filename): |
66 f.write(jsonString) |
72 title = self.tr("Save Remote User Project Properties") |
|
73 fsInterface.writeFile(filename, jsonString.encode("utf-8")) |
|
74 else: |
|
75 title = self.tr("Save User Project Properties") |
|
76 with open(filename, "w") as f: |
|
77 f.write(jsonString) |
67 except (OSError, TypeError) as err: |
78 except (OSError, TypeError) as err: |
68 with EricOverridenCursor(): |
79 with EricOverridenCursor(): |
69 EricMessageBox.critical( |
80 EricMessageBox.critical( |
70 None, |
81 None, |
71 self.tr("Save User Project Properties"), |
82 title, |
72 self.tr( |
83 self.tr( |
73 "<p>The user specific project properties file" |
84 "<p>The user specific project properties file" |
74 " <b>{0}</b> could not be written.</p>" |
85 " <b>{0}</b> could not be written.</p>" |
75 "<p>Reason: {1}</p>" |
86 "<p>Reason: {1}</p>" |
76 ).format(filename, str(err)), |
87 ).format(filename, str(err)), |
87 @param filename name of the project file |
98 @param filename name of the project file |
88 @type str |
99 @type str |
89 @return flag indicating a successful read |
100 @return flag indicating a successful read |
90 @rtype bool |
101 @rtype bool |
91 """ |
102 """ |
|
103 fsInterface = ( |
|
104 ericApp().getObject("EricServer").getServiceInterface("FileSystem") |
|
105 ) |
|
106 |
92 try: |
107 try: |
93 with open(filename, "r") as f: |
108 if FileSystemUtilities.isRemoteFileName(filename): |
94 jsonString = f.read() |
109 title = self.tr("Read Remote User Project Properties") |
|
110 jsonString = fsInterface.readFile(filename).decode("utf-8") |
|
111 else: |
|
112 title = self.tr("Read User Project Properties") |
|
113 with open(filename, "r") as f: |
|
114 jsonString = f.read() |
95 userProjectDict = json.loads(jsonString) |
115 userProjectDict = json.loads(jsonString) |
96 except (OSError, json.JSONDecodeError) as err: |
116 except (OSError, json.JSONDecodeError) as err: |
97 EricMessageBox.critical( |
117 EricMessageBox.critical( |
98 None, |
118 None, |
99 self.tr("Read User Project Properties"), |
119 title, |
100 self.tr( |
120 self.tr( |
101 "<p>The user specific project properties file <b>{0}</b>" |
121 "<p>The user specific project properties file <b>{0}</b>" |
102 " could not be read.</p><p>Reason: {1}</p>" |
122 " could not be read.</p><p>Reason: {1}</p>" |
103 ).format(filename, str(err)), |
123 ).format(filename, str(err)), |
104 ) |
124 ) |