|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a class representing the multi project JSON file. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import os |
|
12 import time |
|
13 import typing |
|
14 |
|
15 from PyQt5.QtCore import QObject |
|
16 |
|
17 from E5Gui import E5MessageBox |
|
18 from E5Gui.E5OverrideCursor import E5OverridenCursor |
|
19 |
|
20 import Preferences |
|
21 |
|
22 MultiProject = typing.Type["MultiProject"] |
|
23 |
|
24 |
|
25 class MultiProjectFile(QObject): |
|
26 """ |
|
27 Class representing the multi project JSON file. |
|
28 """ |
|
29 def __init__(self, multiProject: MultiProject, parent: QObject = None): |
|
30 """ |
|
31 Constructor |
|
32 |
|
33 @param multiProject reference to the multi project object |
|
34 @type MultiProject |
|
35 @param parent reference to the parent object |
|
36 @type QObject (optional) |
|
37 """ |
|
38 super(MultiProjectFile, self).__init__(parent) |
|
39 self.__multiProject = multiProject |
|
40 |
|
41 def writeFile(self, filename: str) -> bool: |
|
42 """ |
|
43 Public method to write the multi project data to a multi project |
|
44 JSON file. |
|
45 |
|
46 @param filename name of the multi project file |
|
47 @type str |
|
48 @return flag indicating a successful write |
|
49 @rtype bool |
|
50 """ |
|
51 name = os.path.splitext(os.path.basename(filename))[0] |
|
52 |
|
53 multiProjectDict = {} |
|
54 multiProjectDict["header"] = { |
|
55 "comment": f"eric multi project file for multi project {name}", |
|
56 } |
|
57 |
|
58 # TODO: replace 'XMLTimestamp' by 'Timestamp' |
|
59 if Preferences.getMultiProject("XMLTimestamp"): |
|
60 multiProjectDict["header"]["saved"] = ( |
|
61 time.strftime('%Y-%m-%d, %H:%M:%S') |
|
62 ) |
|
63 |
|
64 multiProjectDict["description"] = self.__multiProject.description |
|
65 multiProjectDict["projects"] = list(self.__multiProject.getProjects()) |
|
66 |
|
67 try: |
|
68 jsonString = json.dumps(multiProjectDict, indent=2) |
|
69 with open(filename, "w") as f: |
|
70 f.write(jsonString) |
|
71 except (TypeError, EnvironmentError) as err: |
|
72 with E5OverridenCursor(): |
|
73 E5MessageBox.critical( |
|
74 None, |
|
75 self.tr("Save Multi Project File"), |
|
76 self.tr( |
|
77 "<p>The multi project file <b>{0}</b> could not be " |
|
78 "written.</p><p>Reason: {1}</p>" |
|
79 ).format(filename, str(err)) |
|
80 ) |
|
81 return False |
|
82 |
|
83 return True |
|
84 |
|
85 def readFile(self, filename: str) -> bool: |
|
86 """ |
|
87 Public method to read the multi project data from a multi project |
|
88 JSON file. |
|
89 |
|
90 @param filename name of the multi project file |
|
91 @type str |
|
92 @return flag indicating a successful read |
|
93 @rtype bool |
|
94 """ |
|
95 try: |
|
96 with open(filename, "r") as f: |
|
97 jsonString = f.read() |
|
98 multiProjectDict = json.loads(jsonString) |
|
99 except (EnvironmentError, json.JSONDecodeError) as err: |
|
100 E5MessageBox.critical( |
|
101 None, |
|
102 self.tr("Read Multi Project File"), |
|
103 self.tr( |
|
104 "<p>The multi project file <b>{0}</b> could not be " |
|
105 "read.</p><p>Reason: {1}</p>" |
|
106 ).format(filename, str(err)) |
|
107 ) |
|
108 return False |
|
109 |
|
110 self.__multiProject.description = multiProjectDict["description"] |
|
111 for project in multiProjectDict["projects"]: |
|
112 self.__multiProject.addProject(project) |
|
113 |
|
114 return True |