|
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 project JSON file. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import time |
|
12 import contextlib |
|
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 import Utilities |
|
22 |
|
23 Project = typing.TypeVar("Project") |
|
24 |
|
25 |
|
26 class ProjectFile(QObject): |
|
27 """ |
|
28 Class representing the project JSON file. |
|
29 """ |
|
30 def __init__(self, project: Project, parent: QObject = None): |
|
31 """ |
|
32 Constructor |
|
33 |
|
34 @param project reference to the project object |
|
35 @type Project |
|
36 @param parent reference to the parent object (defaults to None) |
|
37 @type QObject (optional) |
|
38 """ |
|
39 super().__init__(parent) |
|
40 self.__project = project |
|
41 |
|
42 def writeFile(self, filename: str) -> bool: |
|
43 """ |
|
44 Public method to write the project data to a project JSON file. |
|
45 |
|
46 @param filename name of the project file |
|
47 @type str |
|
48 @return flag indicating a successful write |
|
49 @rtype bool |
|
50 """ |
|
51 projectDict = {} |
|
52 projectDict["header"] = { |
|
53 "comment": "eric project file for project {0}".format( |
|
54 self.__project.getProjectName()), |
|
55 "copyright": "Copyright (C) {0} {1}, {2}".format( |
|
56 time.strftime('%Y'), |
|
57 self.__project.pdata["AUTHOR"], |
|
58 self.__project.pdata["EMAIL"]) |
|
59 } |
|
60 |
|
61 if Preferences.getProject("TimestampFile"): |
|
62 projectDict["header"]["saved"] = ( |
|
63 time.strftime('%Y-%m-%d, %H:%M:%S') |
|
64 ) |
|
65 |
|
66 projectDict["project"] = self.__project.pdata |
|
67 |
|
68 # modify paths to contain universal separators |
|
69 for key in ( |
|
70 "SOURCES", "FORMS", "TRANSLATIONS", "TRANSLATIONEXCEPTIONS", |
|
71 "RESOURCES", "INTERFACES", "PROTOCOLS", "OTHERS" |
|
72 ): |
|
73 with contextlib.suppress(KeyError): |
|
74 projectDict["project"][key] = [ |
|
75 Utilities.fromNativeSeparators(f) |
|
76 for f in projectDict["project"][key] |
|
77 ] |
|
78 for key in ( |
|
79 "SPELLWORDS", "SPELLEXCLUDES", "TRANSLATIONPATTERN", |
|
80 "TRANSLATIONSBINPATH", "MAINSCRIPT" |
|
81 ): |
|
82 with contextlib.suppress(KeyError): |
|
83 projectDict["project"][key] = Utilities.fromNativeSeparators( |
|
84 projectDict["project"][key]) |
|
85 |
|
86 try: |
|
87 jsonString = json.dumps(projectDict, indent=2, sort_keys=True) |
|
88 with open(filename, "w", newline="") as f: |
|
89 f.write(jsonString) |
|
90 except (TypeError, OSError) as err: |
|
91 with E5OverridenCursor(): |
|
92 E5MessageBox.critical( |
|
93 None, |
|
94 self.tr("Save Project File"), |
|
95 self.tr( |
|
96 "<p>The project file <b>{0}</b> could not be " |
|
97 "written.</p><p>Reason: {1}</p>" |
|
98 ).format(filename, str(err)) |
|
99 ) |
|
100 return False |
|
101 |
|
102 return True |
|
103 |
|
104 def readFile(self, filename: str) -> bool: |
|
105 """ |
|
106 Public method to read the project data from a project JSON file. |
|
107 |
|
108 @param filename name of the project file |
|
109 @type str |
|
110 @return flag indicating a successful read |
|
111 @rtype bool |
|
112 """ |
|
113 try: |
|
114 with open(filename, "r") as f: |
|
115 jsonString = f.read() |
|
116 projectDict = json.loads(jsonString) |
|
117 except (OSError, json.JSONDecodeError) as err: |
|
118 E5MessageBox.critical( |
|
119 None, |
|
120 self.tr("Read Project File"), |
|
121 self.tr( |
|
122 "<p>The project file <b>{0}</b> could not be " |
|
123 "read.</p><p>Reason: {1}</p>" |
|
124 ).format(filename, str(err)) |
|
125 ) |
|
126 return False |
|
127 |
|
128 # modify paths to contain native separators |
|
129 for key in ( |
|
130 "SOURCES", "FORMS", "TRANSLATIONS", "TRANSLATIONEXCEPTIONS", |
|
131 "RESOURCES", "INTERFACES", "PROTOCOLS", "OTHERS" |
|
132 ): |
|
133 with contextlib.suppress(KeyError): |
|
134 projectDict["project"][key] = [ |
|
135 Utilities.toNativeSeparators(f) |
|
136 for f in projectDict["project"][key] |
|
137 ] |
|
138 for key in ( |
|
139 "SPELLWORDS", "SPELLEXCLUDES", "TRANSLATIONPATTERN", |
|
140 "TRANSLATIONSBINPATH", "MAINSCRIPT" |
|
141 ): |
|
142 with contextlib.suppress(KeyError): |
|
143 projectDict["project"][key] = Utilities.toNativeSeparators( |
|
144 projectDict["project"][key]) |
|
145 |
|
146 self.__project.pdata = projectDict["project"] |
|
147 |
|
148 return True |