eric7/Project/UserProjectFile.py

Thu, 30 Dec 2021 11:17:58 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Thu, 30 Dec 2021 11:17:58 +0100
branch
eric7
changeset 8881
54e42bc2437a
parent 8358
144a6b854f70
permissions
-rw-r--r--

Updated copyright for 2022.

8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
8881
54e42bc2437a Updated copyright for 2022.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8358
diff changeset
3 # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing a class representing the user project JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import json
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 import time
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import typing
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
8318
962bce857696 Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8312
diff changeset
14 from PyQt6.QtCore import QObject
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
8358
144a6b854f70 Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8356
diff changeset
16 from EricWidgets import EricMessageBox
144a6b854f70 Sorted the eric specific extensions into packages named like the corresponding PyQt packages (i.e. EricCore,EricGui and EricWidgets).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8356
diff changeset
17 from EricGui.EricOverrideCursor import EricOverridenCursor
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 import Preferences
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20
8015
09b24828d787 Cprrected the use of the typing module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8012
diff changeset
21 Project = typing.TypeVar("Project")
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 class UserProjectFile(QObject):
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Class representing the user project JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 def __init__(self, project: Project, parent: QObject = None):
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 Constructor
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 @param project reference to the project object
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @type Project
8016
e484fc67677e Corrected some code documentation strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8015
diff changeset
34 @param parent reference to the parent object (defaults to None)
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @type QObject (optional)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
8218
7c09585bd960 Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8031
diff changeset
37 super().__init__(parent)
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.__project = project
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40 def writeFile(self, filename: str) -> bool:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 Public method to write the user project data to a user project
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 @param filename name of the user project file
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @type str
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @return flag indicating a successful write
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @rtype bool
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 userProjectDict = {}
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 userProjectDict["header"] = {
8012
ecf45f723038 Several corrections to the JSON file handlers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8006
diff changeset
52 "comment": "eric user project file for project {0}".format(
ecf45f723038 Several corrections to the JSON file handlers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8006
diff changeset
53 self.__project.getProjectName()),
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 }
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
8031
ce5858a237d2 Changed the Project and MultiProject configuration option "XMLTimestamp" to "TimestampFile". A configuration check is suggested.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8016
diff changeset
56 if Preferences.getProject("TimestampFile"):
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 userProjectDict["header"]["saved"] = (
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 time.strftime('%Y-%m-%d, %H:%M:%S')
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 )
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 userProjectDict["user_data"] = self.__project.pudata
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 try:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 jsonString = json.dumps(userProjectDict, indent=2)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 with open(filename, "w") as f:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 f.write(jsonString)
8240
93b8a353c4bf Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
67 except (TypeError, OSError) as err:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
68 with EricOverridenCursor():
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
69 EricMessageBox.critical(
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 None,
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 self.tr("Save User Project Properties"),
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 self.tr(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 "<p>The user specific project properties file"
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 " <b>{0}</b> could not be written.</p>"
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 "<p>Reason: {1}</p>"
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 ).format(filename, str(err))
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 )
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 return False
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 return True
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 def readFile(self, filename: str) -> bool:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 Public method to read the user project data from a user project
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 @param filename name of the project file
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 @type str
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 @return flag indicating a successful read
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 @rtype bool
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 try:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 with open(filename, "r") as f:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 jsonString = f.read()
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 userProjectDict = json.loads(jsonString)
8240
93b8a353c4bf Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 1).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8218
diff changeset
96 except (OSError, json.JSONDecodeError) as err:
8356
68ec9c3d4de5 Renamed the modules and classes of the E5Gui package to have the prefix 'Eric' instead of 'E5'.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8318
diff changeset
97 EricMessageBox.critical(
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 None,
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 self.tr("Read User Project Properties"),
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 self.tr(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 "<p>The user specific project properties file <b>{0}</b>"
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 " could not be read.</p><p>Reason: {1}</p>"
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 ).format(filename, str(err))
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 )
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 return False
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 self.__project.pudata = userProjectDict["user_data"]
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 return True

eric ide

mercurial