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