Mon, 07 Nov 2022 17:19:58 +0100
Corrected/acknowledged some bad import style and removed some obsolete code.
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 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 | |
9023
5c6e160faa32
Changed project file writer to sort the file lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8881
diff
changeset
|
10 | import contextlib |
5c6e160faa32
Changed project file writer to sort the file lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8881
diff
changeset
|
11 | import copy |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
12 | import json |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
13 | import time |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
14 | import typing |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
15 | |
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
|
16 | 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
|
17 | |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9426
diff
changeset
|
18 | from eric7 import Preferences, Utilities |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9323
diff
changeset
|
19 | from eric7.EricGui.EricOverrideCursor import EricOverridenCursor |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9426
diff
changeset
|
20 | from eric7.EricWidgets import EricMessageBox |
8006
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:
8012
diff
changeset
|
22 | 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
|
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 ProjectFile(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 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 | """ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
29 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
30 | 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
|
31 | """ |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
32 | Constructor |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
33 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
34 | @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
|
35 | @type Project |
8016
e484fc67677e
Corrected some code documentation strings.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8015
diff
changeset
|
36 | @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
|
37 | @type QObject (optional) |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
38 | """ |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8125
diff
changeset
|
39 | 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
|
40 | self.__project = project |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
41 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
42 | 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
|
43 | """ |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
44 | Public method to write the project data to a project JSON file. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
45 | |
8006
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 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 | """ |
9278
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
51 | projectDict = { |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
52 | "header": { |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
53 | "comment": "eric project file for project {0}".format( |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
54 | self.__project.getProjectName() |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
55 | ), |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
56 | "copyright": "Copyright (C) {0} {1}, {2}".format( |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
57 | time.strftime("%Y"), |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
58 | self.__project.pdata["AUTHOR"], |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
59 | self.__project.pdata["EMAIL"], |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
60 | ), |
36448ca469c2
Simplified some code iaw. recommendations of the extended style checker and reformatted the code with black.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
61 | } |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
62 | } |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
63 | |
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
|
64 | if Preferences.getProject("TimestampFile"): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | projectDict["header"]["saved"] = time.strftime("%Y-%m-%d, %H:%M:%S") |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
66 | |
9023
5c6e160faa32
Changed project file writer to sort the file lists.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8881
diff
changeset
|
67 | projectDict["project"] = copy.deepcopy(self.__project.pdata) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
68 | |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
69 | # modify paths to contain universal separators |
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
70 | for key in ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
71 | "SOURCES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
72 | "FORMS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
73 | "TRANSLATIONS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
74 | "TRANSLATIONEXCEPTIONS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | "RESOURCES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
76 | "INTERFACES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
77 | "PROTOCOLS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
78 | "OTHERS", |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
79 | ): |
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
|
80 | with contextlib.suppress(KeyError): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
81 | projectDict["project"][key] = sorted( |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
82 | [ |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
83 | Utilities.fromNativeSeparators(f) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
84 | for f in projectDict["project"][key] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
85 | ] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
86 | ) |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
87 | for key in ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
88 | "SPELLWORDS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
89 | "SPELLEXCLUDES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
90 | "TRANSLATIONPATTERN", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
91 | "TRANSLATIONSBINPATH", |
9426 | 92 | "TRANSLATIONSOURCESTARTPATH", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
93 | "MAINSCRIPT", |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
94 | ): |
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
|
95 | with contextlib.suppress(KeyError): |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
96 | projectDict["project"][key] = Utilities.fromNativeSeparators( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
97 | projectDict["project"][key] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
99 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
100 | try: |
8125
9e789fa8f38e
Saving the project file with sorted dictionary keys to get more stability into it.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8107
diff
changeset
|
101 | jsonString = json.dumps(projectDict, indent=2, sort_keys=True) |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
102 | with open(filename, "w", newline="") as f: |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
103 | 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
|
104 | 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
|
105 | 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
|
106 | EricMessageBox.critical( |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
107 | None, |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
108 | self.tr("Save Project File"), |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
109 | self.tr( |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
110 | "<p>The 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
|
111 | "written.</p><p>Reason: {1}</p>" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
112 | ).format(filename, str(err)), |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
113 | ) |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
114 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
115 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
116 | return True |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
117 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
118 | 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
|
119 | """ |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
120 | Public method to read the project data from a project JSON file. |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
121 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
122 | @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
|
123 | @type str |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
124 | @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
|
125 | @rtype bool |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
126 | """ |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
127 | try: |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
128 | 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
|
129 | jsonString = f.read() |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
130 | projectDict = 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
|
131 | 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
|
132 | EricMessageBox.critical( |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
133 | None, |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
134 | self.tr("Read Project File"), |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
135 | self.tr( |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
136 | "<p>The 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
|
137 | "read.</p><p>Reason: {1}</p>" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
138 | ).format(filename, str(err)), |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
139 | ) |
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
140 | return False |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
141 | |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
142 | # modify paths to contain native separators |
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
143 | for key in ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
144 | "SOURCES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
145 | "FORMS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
146 | "TRANSLATIONS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
147 | "TRANSLATIONEXCEPTIONS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | "RESOURCES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
149 | "INTERFACES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
150 | "PROTOCOLS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
151 | "OTHERS", |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
152 | ): |
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
|
153 | with contextlib.suppress(KeyError): |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
154 | projectDict["project"][key] = [ |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
155 | Utilities.toNativeSeparators(f) for f in projectDict["project"][key] |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
156 | ] |
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
157 | for key in ( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
158 | "SPELLWORDS", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
159 | "SPELLEXCLUDES", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
160 | "TRANSLATIONPATTERN", |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
161 | "TRANSLATIONSBINPATH", |
9426 | 162 | "TRANSLATIONSOURCESTARTPATH", |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
163 | "MAINSCRIPT", |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
164 | ): |
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
|
165 | with contextlib.suppress(KeyError): |
8107
7d3932bde11b
ProjectFile: changed paths handling to work with different platforms.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8031
diff
changeset
|
166 | projectDict["project"][key] = Utilities.toNativeSeparators( |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
167 | projectDict["project"][key] |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
168 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
169 | |
9323 | 170 | self.__project.pdata.update(projectDict["project"]) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | |
8006
c4110b8b5931
Started converting the various file generators to JSON format (from XML).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff
changeset
|
172 | return True |