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