24 |
24 |
25 class MultiProjectFile(QObject): |
25 class MultiProjectFile(QObject): |
26 """ |
26 """ |
27 Class representing the multi project JSON file. |
27 Class representing the multi project JSON file. |
28 """ |
28 """ |
|
29 |
29 def __init__(self, multiProject: MultiProject, parent: QObject = None): |
30 def __init__(self, multiProject: MultiProject, parent: QObject = None): |
30 """ |
31 """ |
31 Constructor |
32 Constructor |
32 |
33 |
33 @param multiProject reference to the multi project object |
34 @param multiProject reference to the multi project object |
34 @type MultiProject |
35 @type MultiProject |
35 @param parent reference to the parent object (defaults to None) |
36 @param parent reference to the parent object (defaults to None) |
36 @type QObject (optional) |
37 @type QObject (optional) |
37 """ |
38 """ |
38 super().__init__(parent) |
39 super().__init__(parent) |
39 self.__multiProject = multiProject |
40 self.__multiProject = multiProject |
40 |
41 |
41 def writeFile(self, filename: str) -> bool: |
42 def writeFile(self, filename: str) -> bool: |
42 """ |
43 """ |
43 Public method to write the multi project data to a multi project |
44 Public method to write the multi project data to a multi project |
44 JSON file. |
45 JSON file. |
45 |
46 |
46 @param filename name of the multi project file |
47 @param filename name of the multi project file |
47 @type str |
48 @type str |
48 @return flag indicating a successful write |
49 @return flag indicating a successful write |
49 @rtype bool |
50 @rtype bool |
50 """ |
51 """ |
51 name = os.path.splitext(os.path.basename(filename))[0] |
52 name = os.path.splitext(os.path.basename(filename))[0] |
52 |
53 |
53 multiProjectDict = {} |
54 multiProjectDict = {} |
54 multiProjectDict["header"] = { |
55 multiProjectDict["header"] = { |
55 "comment": f"eric multi project file for multi project {name}", |
56 "comment": f"eric multi project file for multi project {name}", |
56 } |
57 } |
57 |
58 |
58 if Preferences.getMultiProject("TimestampFile"): |
59 if Preferences.getMultiProject("TimestampFile"): |
59 multiProjectDict["header"]["saved"] = ( |
60 multiProjectDict["header"]["saved"] = time.strftime("%Y-%m-%d, %H:%M:%S") |
60 time.strftime('%Y-%m-%d, %H:%M:%S') |
61 |
61 ) |
|
62 |
|
63 multiProjectDict["description"] = self.__multiProject.description |
62 multiProjectDict["description"] = self.__multiProject.description |
64 multiProjectDict["projects"] = list(self.__multiProject.getProjects()) |
63 multiProjectDict["projects"] = list(self.__multiProject.getProjects()) |
65 |
64 |
66 try: |
65 try: |
67 jsonString = json.dumps(multiProjectDict, indent=2) |
66 jsonString = json.dumps(multiProjectDict, indent=2) |
68 with open(filename, "w") as f: |
67 with open(filename, "w") as f: |
69 f.write(jsonString) |
68 f.write(jsonString) |
70 except (TypeError, OSError) as err: |
69 except (TypeError, OSError) as err: |
73 None, |
72 None, |
74 self.tr("Save Multi Project File"), |
73 self.tr("Save Multi Project File"), |
75 self.tr( |
74 self.tr( |
76 "<p>The multi project file <b>{0}</b> could not be " |
75 "<p>The multi project file <b>{0}</b> could not be " |
77 "written.</p><p>Reason: {1}</p>" |
76 "written.</p><p>Reason: {1}</p>" |
78 ).format(filename, str(err)) |
77 ).format(filename, str(err)), |
79 ) |
78 ) |
80 return False |
79 return False |
81 |
80 |
82 return True |
81 return True |
83 |
82 |
84 def readFile(self, filename: str) -> bool: |
83 def readFile(self, filename: str) -> bool: |
85 """ |
84 """ |
86 Public method to read the multi project data from a multi project |
85 Public method to read the multi project data from a multi project |
87 JSON file. |
86 JSON file. |
88 |
87 |
89 @param filename name of the multi project file |
88 @param filename name of the multi project file |
90 @type str |
89 @type str |
91 @return flag indicating a successful read |
90 @return flag indicating a successful read |
92 @rtype bool |
91 @rtype bool |
93 """ |
92 """ |
100 None, |
99 None, |
101 self.tr("Read Multi Project File"), |
100 self.tr("Read Multi Project File"), |
102 self.tr( |
101 self.tr( |
103 "<p>The multi project file <b>{0}</b> could not be " |
102 "<p>The multi project file <b>{0}</b> could not be " |
104 "read.</p><p>Reason: {1}</p>" |
103 "read.</p><p>Reason: {1}</p>" |
105 ).format(filename, str(err)) |
104 ).format(filename, str(err)), |
106 ) |
105 ) |
107 return False |
106 return False |
108 |
107 |
109 self.__multiProject.description = multiProjectDict["description"] |
108 self.__multiProject.description = multiProjectDict["description"] |
110 for project in multiProjectDict["projects"]: |
109 for project in multiProjectDict["projects"]: |
111 self.__multiProject.addProject(project) |
110 self.__multiProject.addProject(project) |
112 |
111 |
113 return True |
112 return True |