|
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 user project JSON file. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import time |
|
12 import typing |
|
13 |
|
14 from PyQt5.QtCore import QObject |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 from E5Gui.E5OverrideCursor import E5OverridenCursor |
|
18 |
|
19 import Preferences |
|
20 |
|
21 Project = typing.TypeVar("Project") |
|
22 |
|
23 |
|
24 class UserProjectFile(QObject): |
|
25 """ |
|
26 Class representing the user project JSON file. |
|
27 """ |
|
28 def __init__(self, project: Project, parent: QObject = None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param project reference to the project object |
|
33 @type Project |
|
34 @param parent reference to the parent object (defaults to None) |
|
35 @type QObject (optional) |
|
36 """ |
|
37 super().__init__(parent) |
|
38 self.__project = project |
|
39 |
|
40 def writeFile(self, filename: str) -> bool: |
|
41 """ |
|
42 Public method to write the user project data to a user project |
|
43 JSON file. |
|
44 |
|
45 @param filename name of the user project file |
|
46 @type str |
|
47 @return flag indicating a successful write |
|
48 @rtype bool |
|
49 """ |
|
50 userProjectDict = {} |
|
51 userProjectDict["header"] = { |
|
52 "comment": "eric user project file for project {0}".format( |
|
53 self.__project.getProjectName()), |
|
54 } |
|
55 |
|
56 if Preferences.getProject("TimestampFile"): |
|
57 userProjectDict["header"]["saved"] = ( |
|
58 time.strftime('%Y-%m-%d, %H:%M:%S') |
|
59 ) |
|
60 |
|
61 userProjectDict["user_data"] = self.__project.pudata |
|
62 |
|
63 try: |
|
64 jsonString = json.dumps(userProjectDict, indent=2) |
|
65 with open(filename, "w") as f: |
|
66 f.write(jsonString) |
|
67 except (TypeError, OSError) as err: |
|
68 with E5OverridenCursor(): |
|
69 E5MessageBox.critical( |
|
70 None, |
|
71 self.tr("Save User Project Properties"), |
|
72 self.tr( |
|
73 "<p>The user specific project properties file" |
|
74 " <b>{0}</b> could not be written.</p>" |
|
75 "<p>Reason: {1}</p>" |
|
76 ).format(filename, str(err)) |
|
77 ) |
|
78 return False |
|
79 |
|
80 return True |
|
81 |
|
82 def readFile(self, filename: str) -> bool: |
|
83 """ |
|
84 Public method to read the user project data from a user project |
|
85 JSON file. |
|
86 |
|
87 @param filename name of the project file |
|
88 @type str |
|
89 @return flag indicating a successful read |
|
90 @rtype bool |
|
91 """ |
|
92 try: |
|
93 with open(filename, "r") as f: |
|
94 jsonString = f.read() |
|
95 userProjectDict = json.loads(jsonString) |
|
96 except (OSError, json.JSONDecodeError) as err: |
|
97 E5MessageBox.critical( |
|
98 None, |
|
99 self.tr("Read User Project Properties"), |
|
100 self.tr( |
|
101 "<p>The user specific project properties file <b>{0}</b>" |
|
102 " could not be read.</p><p>Reason: {1}</p>" |
|
103 ).format(filename, str(err)) |
|
104 ) |
|
105 return False |
|
106 |
|
107 self.__project.pudata = userProjectDict["user_data"] |
|
108 |
|
109 return True |