eric6/MultiProject/MultiProjectFile.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 8031
ce5858a237d2
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

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
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2021 Detlev Offenbach <detlev@die-offenbachs.de>
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 multi 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 os
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import time
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import typing
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 from PyQt5.QtCore import QObject
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 from E5Gui import E5MessageBox
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 from E5Gui.E5OverrideCursor import E5OverridenCursor
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 import Preferences
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
8015
09b24828d787 Cprrected the use of the typing module.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8006
diff changeset
22 MultiProject = typing.TypeVar("MultiProject")
8006
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
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 class MultiProjectFile(QObject):
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 Class representing the multi project JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 def __init__(self, multiProject: MultiProject, parent: QObject = None):
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 Constructor
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 @param multiProject reference to the multi project object
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @type MultiProject
8016
e484fc67677e Corrected some code documentation strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 8015
diff changeset
35 @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
36 @type QObject (optional)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 super(MultiProjectFile, self).__init__(parent)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__multiProject = multiProject
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 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
42 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 Public method to write the multi project data to a multi project
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 @param filename name of the multi project file
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 @type str
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @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
49 @rtype bool
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 name = os.path.splitext(os.path.basename(filename))[0]
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 multiProjectDict = {}
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 multiProjectDict["header"] = {
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 "comment": f"eric multi project file for multi project {name}",
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 }
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57
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
58 if Preferences.getMultiProject("TimestampFile"):
8006
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 multiProjectDict["header"]["saved"] = (
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 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
61 )
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 multiProjectDict["description"] = self.__multiProject.description
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 multiProjectDict["projects"] = list(self.__multiProject.getProjects())
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 try:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 jsonString = json.dumps(multiProjectDict, indent=2)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 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
69 f.write(jsonString)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 except (TypeError, EnvironmentError) as err:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 with E5OverridenCursor():
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 E5MessageBox.critical(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 None,
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 self.tr("Save Multi Project File"),
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 self.tr(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 "<p>The multi project file <b>{0}</b> could not be "
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 "written.</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
78 ).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
79 )
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 return False
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 return True
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 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
85 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 Public method to read the multi project data from a multi project
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 JSON file.
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 @param filename name of the multi project file
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 @type str
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 @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
92 @rtype bool
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 """
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 try:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 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
96 jsonString = f.read()
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 multiProjectDict = json.loads(jsonString)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 except (EnvironmentError, json.JSONDecodeError) as err:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 E5MessageBox.critical(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 None,
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 self.tr("Read Multi Project File"),
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.tr(
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 "<p>The multi project file <b>{0}</b> could not be "
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 "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
105 ).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
106 )
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 return False
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 self.__multiProject.description = multiProjectDict["description"]
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 for project in multiProjectDict["projects"]:
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 self.__multiProject.addProject(project)
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
c4110b8b5931 Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 return True

eric ide

mercurial