src/eric7/MultiProject/MultiProjectFile.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2021 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a class representing the multi project JSON file.
8 """
9
10 import json
11 import os
12 import time
13 import typing
14
15 from PyQt6.QtCore import QObject
16
17 from EricWidgets import EricMessageBox
18 from EricGui.EricOverrideCursor import EricOverridenCursor
19
20 import Preferences
21
22 MultiProject = typing.TypeVar("MultiProject")
23
24
25 class MultiProjectFile(QObject):
26 """
27 Class representing the multi project JSON file.
28 """
29 def __init__(self, multiProject: MultiProject, parent: QObject = None):
30 """
31 Constructor
32
33 @param multiProject reference to the multi project object
34 @type MultiProject
35 @param parent reference to the parent object (defaults to None)
36 @type QObject (optional)
37 """
38 super().__init__(parent)
39 self.__multiProject = multiProject
40
41 def writeFile(self, filename: str) -> bool:
42 """
43 Public method to write the multi project data to a multi project
44 JSON file.
45
46 @param filename name of the multi project file
47 @type str
48 @return flag indicating a successful write
49 @rtype bool
50 """
51 name = os.path.splitext(os.path.basename(filename))[0]
52
53 multiProjectDict = {}
54 multiProjectDict["header"] = {
55 "comment": f"eric multi project file for multi project {name}",
56 }
57
58 if Preferences.getMultiProject("TimestampFile"):
59 multiProjectDict["header"]["saved"] = (
60 time.strftime('%Y-%m-%d, %H:%M:%S')
61 )
62
63 multiProjectDict["description"] = self.__multiProject.description
64 multiProjectDict["projects"] = list(self.__multiProject.getProjects())
65
66 try:
67 jsonString = json.dumps(multiProjectDict, indent=2)
68 with open(filename, "w") as f:
69 f.write(jsonString)
70 except (TypeError, OSError) as err:
71 with EricOverridenCursor():
72 EricMessageBox.critical(
73 None,
74 self.tr("Save Multi Project File"),
75 self.tr(
76 "<p>The multi project file <b>{0}</b> could not be "
77 "written.</p><p>Reason: {1}</p>"
78 ).format(filename, str(err))
79 )
80 return False
81
82 return True
83
84 def readFile(self, filename: str) -> bool:
85 """
86 Public method to read the multi project data from a multi project
87 JSON file.
88
89 @param filename name of the multi project file
90 @type str
91 @return flag indicating a successful read
92 @rtype bool
93 """
94 try:
95 with open(filename, "r") as f:
96 jsonString = f.read()
97 multiProjectDict = json.loads(jsonString)
98 except (OSError, json.JSONDecodeError) as err:
99 EricMessageBox.critical(
100 None,
101 self.tr("Read Multi Project File"),
102 self.tr(
103 "<p>The multi project file <b>{0}</b> could not be "
104 "read.</p><p>Reason: {1}</p>"
105 ).format(filename, str(err))
106 )
107 return False
108
109 self.__multiProject.description = multiProjectDict["description"]
110 for project in multiProjectDict["projects"]:
111 self.__multiProject.addProject(project)
112
113 return True

eric ide

mercurial