--- a/src/eric7/Templates/TemplatesFile.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Templates/TemplatesFile.py Wed Jul 13 14:55:47 2022 +0200 @@ -24,10 +24,11 @@ """ Class representing the templates JSON file. """ + def __init__(self, viewer: TemplateViewer, parent: QObject = None): """ Constructor - + @param viewer reference to the template viewer object @type TemplateViewer @param parent reference to the parent object (defaults to None) @@ -35,11 +36,11 @@ """ super().__init__(parent) self.__viewer = viewer - + def writeFile(self, filename: str) -> bool: """ Public method to write the templates data to a templates JSON file. - + @param filename name of the templates file @type str @return flag indicating a successful write @@ -49,29 +50,31 @@ # step 0: header templatesDict["header"] = { "comment": "eric templates file", - "saved": time.strftime('%Y-%m-%d, %H:%M:%S'), - "warning": ( - "This file was generated automatically, do not edit." - ), + "saved": time.strftime("%Y-%m-%d, %H:%M:%S"), + "warning": ("This file was generated automatically, do not edit."), } - + # step 1: template groups and templates templateGroups = [] for group in self.__viewer.getAllGroups(): templates = [] for template in group.getAllEntries(): - templates.append({ - "name": template.getName(), - "description": template.getDescription().strip(), - "text": template.getTemplateText() - }) - templateGroups.append({ - "name": group.getName(), - "language": group.getLanguage(), - "templates": templates, - }) + templates.append( + { + "name": template.getName(), + "description": template.getDescription().strip(), + "text": template.getTemplateText(), + } + ) + templateGroups.append( + { + "name": group.getName(), + "language": group.getLanguage(), + "templates": templates, + } + ) templatesDict["template_groups"] = templateGroups - + try: jsonString = json.dumps(templatesDict, indent=2) with open(filename, "w") as f: @@ -84,16 +87,16 @@ self.tr( "<p>The templates file <b>{0}</b> could not be" " written.</p><p>Reason: {1}</p>" - ).format(filename, str(err)) + ).format(filename, str(err)), ) return False - + return True - + def readFile(self, filename: str) -> bool: """ Public method to read the templates data from a templates JSON file. - + @param filename name of the templates file @type str @return flag indicating a successful read @@ -110,18 +113,19 @@ self.tr( "<p>The templates file <b>{0}</b> could not be read.</p>" "<p>Reason: {1}</p>" - ).format(filename, str(err)) + ).format(filename, str(err)), ) return False - + for templateGroup in templatesDict["template_groups"]: - self.__viewer.addGroup(templateGroup["name"], - templateGroup["language"]) + self.__viewer.addGroup(templateGroup["name"], templateGroup["language"]) for template in templateGroup["templates"]: - self.__viewer.addEntry(templateGroup["name"], - template["name"], - template["description"], - template["text"], - quiet=True) - + self.__viewer.addEntry( + templateGroup["name"], + template["name"], + template["description"], + template["text"], + quiet=True, + ) + return True