src/eric7/Project/ProjectFile.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9278
36448ca469c2
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
26 26
27 class ProjectFile(QObject): 27 class ProjectFile(QObject):
28 """ 28 """
29 Class representing the project JSON file. 29 Class representing the project JSON file.
30 """ 30 """
31
31 def __init__(self, project: Project, parent: QObject = None): 32 def __init__(self, project: Project, parent: QObject = None):
32 """ 33 """
33 Constructor 34 Constructor
34 35
35 @param project reference to the project object 36 @param project reference to the project object
36 @type Project 37 @type Project
37 @param parent reference to the parent object (defaults to None) 38 @param parent reference to the parent object (defaults to None)
38 @type QObject (optional) 39 @type QObject (optional)
39 """ 40 """
40 super().__init__(parent) 41 super().__init__(parent)
41 self.__project = project 42 self.__project = project
42 43
43 def writeFile(self, filename: str) -> bool: 44 def writeFile(self, filename: str) -> bool:
44 """ 45 """
45 Public method to write the project data to a project JSON file. 46 Public method to write the project data to a project JSON file.
46 47
47 @param filename name of the project file 48 @param filename name of the project file
48 @type str 49 @type str
49 @return flag indicating a successful write 50 @return flag indicating a successful write
50 @rtype bool 51 @rtype bool
51 """ 52 """
52 projectDict = {} 53 projectDict = {}
53 projectDict["header"] = { 54 projectDict["header"] = {
54 "comment": "eric project file for project {0}".format( 55 "comment": "eric project file for project {0}".format(
55 self.__project.getProjectName()), 56 self.__project.getProjectName()
57 ),
56 "copyright": "Copyright (C) {0} {1}, {2}".format( 58 "copyright": "Copyright (C) {0} {1}, {2}".format(
57 time.strftime('%Y'), 59 time.strftime("%Y"),
58 self.__project.pdata["AUTHOR"], 60 self.__project.pdata["AUTHOR"],
59 self.__project.pdata["EMAIL"]) 61 self.__project.pdata["EMAIL"],
62 ),
60 } 63 }
61 64
62 if Preferences.getProject("TimestampFile"): 65 if Preferences.getProject("TimestampFile"):
63 projectDict["header"]["saved"] = ( 66 projectDict["header"]["saved"] = time.strftime("%Y-%m-%d, %H:%M:%S")
64 time.strftime('%Y-%m-%d, %H:%M:%S') 67
65 )
66
67 projectDict["project"] = copy.deepcopy(self.__project.pdata) 68 projectDict["project"] = copy.deepcopy(self.__project.pdata)
68 69
69 # modify paths to contain universal separators 70 # modify paths to contain universal separators
70 for key in ( 71 for key in (
71 "SOURCES", "FORMS", "TRANSLATIONS", "TRANSLATIONEXCEPTIONS", 72 "SOURCES",
72 "RESOURCES", "INTERFACES", "PROTOCOLS", "OTHERS" 73 "FORMS",
74 "TRANSLATIONS",
75 "TRANSLATIONEXCEPTIONS",
76 "RESOURCES",
77 "INTERFACES",
78 "PROTOCOLS",
79 "OTHERS",
73 ): 80 ):
74 with contextlib.suppress(KeyError): 81 with contextlib.suppress(KeyError):
75 projectDict["project"][key] = sorted([ 82 projectDict["project"][key] = sorted(
76 Utilities.fromNativeSeparators(f) 83 [
77 for f in projectDict["project"][key] 84 Utilities.fromNativeSeparators(f)
78 ]) 85 for f in projectDict["project"][key]
86 ]
87 )
79 for key in ( 88 for key in (
80 "SPELLWORDS", "SPELLEXCLUDES", "TRANSLATIONPATTERN", 89 "SPELLWORDS",
81 "TRANSLATIONSBINPATH", "MAINSCRIPT" 90 "SPELLEXCLUDES",
91 "TRANSLATIONPATTERN",
92 "TRANSLATIONSBINPATH",
93 "MAINSCRIPT",
82 ): 94 ):
83 with contextlib.suppress(KeyError): 95 with contextlib.suppress(KeyError):
84 projectDict["project"][key] = Utilities.fromNativeSeparators( 96 projectDict["project"][key] = Utilities.fromNativeSeparators(
85 projectDict["project"][key]) 97 projectDict["project"][key]
86 98 )
99
87 try: 100 try:
88 jsonString = json.dumps(projectDict, indent=2, sort_keys=True) 101 jsonString = json.dumps(projectDict, indent=2, sort_keys=True)
89 with open(filename, "w", newline="") as f: 102 with open(filename, "w", newline="") as f:
90 f.write(jsonString) 103 f.write(jsonString)
91 except (TypeError, OSError) as err: 104 except (TypeError, OSError) as err:
94 None, 107 None,
95 self.tr("Save Project File"), 108 self.tr("Save Project File"),
96 self.tr( 109 self.tr(
97 "<p>The project file <b>{0}</b> could not be " 110 "<p>The project file <b>{0}</b> could not be "
98 "written.</p><p>Reason: {1}</p>" 111 "written.</p><p>Reason: {1}</p>"
99 ).format(filename, str(err)) 112 ).format(filename, str(err)),
100 ) 113 )
101 return False 114 return False
102 115
103 return True 116 return True
104 117
105 def readFile(self, filename: str) -> bool: 118 def readFile(self, filename: str) -> bool:
106 """ 119 """
107 Public method to read the project data from a project JSON file. 120 Public method to read the project data from a project JSON file.
108 121
109 @param filename name of the project file 122 @param filename name of the project file
110 @type str 123 @type str
111 @return flag indicating a successful read 124 @return flag indicating a successful read
112 @rtype bool 125 @rtype bool
113 """ 126 """
120 None, 133 None,
121 self.tr("Read Project File"), 134 self.tr("Read Project File"),
122 self.tr( 135 self.tr(
123 "<p>The project file <b>{0}</b> could not be " 136 "<p>The project file <b>{0}</b> could not be "
124 "read.</p><p>Reason: {1}</p>" 137 "read.</p><p>Reason: {1}</p>"
125 ).format(filename, str(err)) 138 ).format(filename, str(err)),
126 ) 139 )
127 return False 140 return False
128 141
129 # modify paths to contain native separators 142 # modify paths to contain native separators
130 for key in ( 143 for key in (
131 "SOURCES", "FORMS", "TRANSLATIONS", "TRANSLATIONEXCEPTIONS", 144 "SOURCES",
132 "RESOURCES", "INTERFACES", "PROTOCOLS", "OTHERS" 145 "FORMS",
146 "TRANSLATIONS",
147 "TRANSLATIONEXCEPTIONS",
148 "RESOURCES",
149 "INTERFACES",
150 "PROTOCOLS",
151 "OTHERS",
133 ): 152 ):
134 with contextlib.suppress(KeyError): 153 with contextlib.suppress(KeyError):
135 projectDict["project"][key] = [ 154 projectDict["project"][key] = [
136 Utilities.toNativeSeparators(f) 155 Utilities.toNativeSeparators(f) for f in projectDict["project"][key]
137 for f in projectDict["project"][key]
138 ] 156 ]
139 for key in ( 157 for key in (
140 "SPELLWORDS", "SPELLEXCLUDES", "TRANSLATIONPATTERN", 158 "SPELLWORDS",
141 "TRANSLATIONSBINPATH", "MAINSCRIPT" 159 "SPELLEXCLUDES",
160 "TRANSLATIONPATTERN",
161 "TRANSLATIONSBINPATH",
162 "MAINSCRIPT",
142 ): 163 ):
143 with contextlib.suppress(KeyError): 164 with contextlib.suppress(KeyError):
144 projectDict["project"][key] = Utilities.toNativeSeparators( 165 projectDict["project"][key] = Utilities.toNativeSeparators(
145 projectDict["project"][key]) 166 projectDict["project"][key]
146 167 )
168
147 self.__project.pdata = projectDict["project"] 169 self.__project.pdata = projectDict["project"]
148 170
149 return True 171 return True

eric ide

mercurial