|
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 templates JSON file. |
|
8 """ |
|
9 |
|
10 import json |
|
11 import time |
|
12 import typing |
|
13 |
|
14 from PyQt6.QtCore import QObject |
|
15 |
|
16 from EricWidgets import EricMessageBox |
|
17 from EricGui.EricOverrideCursor import EricOverridenCursor |
|
18 |
|
19 |
|
20 TemplateViewer = typing.TypeVar("TemplateViewer") |
|
21 |
|
22 |
|
23 class TemplatesFile(QObject): |
|
24 """ |
|
25 Class representing the templates JSON file. |
|
26 """ |
|
27 def __init__(self, viewer: TemplateViewer, parent: QObject = None): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param viewer reference to the template viewer object |
|
32 @type TemplateViewer |
|
33 @param parent reference to the parent object (defaults to None) |
|
34 @type QObject (optional) |
|
35 """ |
|
36 super().__init__(parent) |
|
37 self.__viewer = viewer |
|
38 |
|
39 def writeFile(self, filename: str) -> bool: |
|
40 """ |
|
41 Public method to write the templates data to a templates JSON file. |
|
42 |
|
43 @param filename name of the templates file |
|
44 @type str |
|
45 @return flag indicating a successful write |
|
46 @rtype bool |
|
47 """ |
|
48 templatesDict = {} |
|
49 # step 0: header |
|
50 templatesDict["header"] = { |
|
51 "comment": "eric templates file", |
|
52 "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), |
|
53 "warning": ( |
|
54 "This file was generated automatically, do not edit." |
|
55 ), |
|
56 } |
|
57 |
|
58 # step 1: template groups and templates |
|
59 templateGroups = [] |
|
60 for group in self.__viewer.getAllGroups(): |
|
61 templates = [] |
|
62 for template in group.getAllEntries(): |
|
63 templates.append({ |
|
64 "name": template.getName(), |
|
65 "description": template.getDescription().strip(), |
|
66 "text": template.getTemplateText() |
|
67 }) |
|
68 templateGroups.append({ |
|
69 "name": group.getName(), |
|
70 "language": group.getLanguage(), |
|
71 "templates": templates, |
|
72 }) |
|
73 templatesDict["template_groups"] = templateGroups |
|
74 |
|
75 try: |
|
76 jsonString = json.dumps(templatesDict, indent=2) |
|
77 with open(filename, "w") as f: |
|
78 f.write(jsonString) |
|
79 except (TypeError, OSError) as err: |
|
80 with EricOverridenCursor(): |
|
81 EricMessageBox.critical( |
|
82 None, |
|
83 self.tr("Save Templates"), |
|
84 self.tr( |
|
85 "<p>The templates file <b>{0}</b> could not be" |
|
86 " written.</p><p>Reason: {1}</p>" |
|
87 ).format(filename, str(err)) |
|
88 ) |
|
89 return False |
|
90 |
|
91 return True |
|
92 |
|
93 def readFile(self, filename: str) -> bool: |
|
94 """ |
|
95 Public method to read the templates data from a templates JSON file. |
|
96 |
|
97 @param filename name of the templates file |
|
98 @type str |
|
99 @return flag indicating a successful read |
|
100 @rtype bool |
|
101 """ |
|
102 try: |
|
103 with open(filename, "r") as f: |
|
104 jsonString = f.read() |
|
105 templatesDict = json.loads(jsonString) |
|
106 except (OSError, json.JSONDecodeError) as err: |
|
107 EricMessageBox.critical( |
|
108 None, |
|
109 self.tr("Read Templates"), |
|
110 self.tr( |
|
111 "<p>The templates file <b>{0}</b> could not be read.</p>" |
|
112 "<p>Reason: {1}</p>" |
|
113 ).format(filename, str(err)) |
|
114 ) |
|
115 return False |
|
116 |
|
117 for templateGroup in templatesDict["template_groups"]: |
|
118 self.__viewer.addGroup(templateGroup["name"], |
|
119 templateGroup["language"]) |
|
120 for template in templateGroup["templates"]: |
|
121 self.__viewer.addEntry(templateGroup["name"], |
|
122 template["name"], |
|
123 template["description"], |
|
124 template["text"], |
|
125 quiet=True) |
|
126 |
|
127 return True |