eric6/Project/DebuggerPropertiesFile.py

branch
jsonfiles
changeset 8006
c4110b8b5931
child 8012
ecf45f723038
equal deleted inserted replaced
8003:59d1c0f80286 8006:c4110b8b5931
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 os
13 import time
14 import typing
15
16 from PyQt5.QtCore import QObject
17
18 from E5Gui import E5MessageBox
19 from E5Gui.E5OverrideCursor import E5OverridenCursor
20
21 import Preferences
22
23 Project = typing.Type["Project"]
24
25
26 class DebuggerPropertiesFile(QObject):
27 """
28 Class representing the project debugger properties 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
37 @type QObject (optional)
38 """
39 super(DebuggerPropertiesFile, self).__init__(parent)
40 self.__project = project
41
42 def writeFile(self, filename: str) -> bool:
43 """
44 Public method to write the project debugger properties data to a
45 project debugger properties JSON file.
46
47 @param filename name of the user project file
48 @type str
49 @return flag indicating a successful write
50 @rtype bool
51 """
52 name = os.path.splitext(os.path.basename(filename))[0]
53
54 debuggerPropertiesDict = {}
55 debuggerPropertiesDict["header"] = {
56 "comment": f"eric debugger properties file for project {name}",
57 "warning": "This file was generated automatically, do not edit.",
58 }
59
60 # TODO: replace 'XMLTimestamp' by 'Timestamp'
61 if Preferences.getProject("XMLTimestamp"):
62 debuggerPropertiesDict["header"]["saved"] = (
63 time.strftime('%Y-%m-%d, %H:%M:%S')
64 )
65
66 debuggerPropertiesDict["debug_properties"] = (
67 self.__project.debugProperties
68 )
69
70 try:
71 jsonString = json.dumps(debuggerPropertiesDict, indent=2)
72 with open(filename, "w") as f:
73 f.write(jsonString)
74 except (TypeError, EnvironmentError) as err:
75 with E5OverridenCursor():
76 E5MessageBox.critical(
77 None,
78 self.tr("Save Debugger Properties"),
79 self.tr(
80 "<p>The project debugger properties file"
81 " <b>{0}</b> could not be written.</p>"
82 "<p>Reason: {1}</p>"
83 ).format(filename, str(err))
84 )
85 return False
86
87 return True
88
89 def readFile(self, filename: str) -> bool:
90 """
91 Public method to read the project debugger properties data from a
92 project debugger properties JSON file.
93
94 @param filename name of the project file
95 @type str
96 @return flag indicating a successful read
97 @rtype bool
98 """
99 try:
100 with open(filename, "r") as f:
101 jsonString = f.read()
102 debuggerPropertiesDict = json.loads(jsonString)
103 except (EnvironmentError, json.JSONDecodeError) as err:
104 E5MessageBox.critical(
105 None,
106 self.tr("Read Debugger Properties"),
107 self.tr(
108 "<p>The project debugger properties file <b>{0}</b>"
109 " could not be read.</p><p>Reason: {1}</p>"
110 ).format(filename, str(err))
111 )
112 return False
113
114 self.__project.debugProperties = (
115 debuggerPropertiesDict["debug_properties"]
116 )
117
118 return True

eric ide

mercurial