--- a/src/eric7/Templates/TemplateViewer.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Templates/TemplateViewer.py Wed Jul 13 14:55:47 2022 +0200 @@ -13,9 +13,7 @@ import re from PyQt6.QtCore import QFile, QIODevice, Qt, QCoreApplication -from PyQt6.QtWidgets import ( - QTreeWidget, QDialog, QApplication, QMenu, QTreeWidgetItem -) +from PyQt6.QtWidgets import QTreeWidget, QDialog, QApplication, QMenu, QTreeWidgetItem from EricWidgets.EricApplication import ericApp from EricWidgets import EricMessageBox, EricFileDialog @@ -32,10 +30,11 @@ """ Class implementing a template group. """ + def __init__(self, parent, name, language="All"): """ Constructor - + @param parent parent widget of the template group (QWidget) @param name name of the group (string) @param language programming language for the group (string) @@ -43,16 +42,16 @@ self.name = name self.language = language self.entries = {} - + super().__init__(parent, [name]) - + if Preferences.getTemplates("ShowTooltip"): self.setToolTip(0, language) - + def setName(self, name): """ Public method to update the name of the group. - + @param name name of the group (string) """ self.name = name @@ -61,15 +60,15 @@ def getName(self): """ Public method to get the name of the group. - + @return name of the group (string) """ return self.name - + def setLanguage(self, language): """ Public method to update the name of the group. - + @param language programming language for the group (string) """ self.language = language @@ -79,15 +78,15 @@ def getLanguage(self): """ Public method to get the name of the group. - + @return language of the group (string) """ return self.language - + def addEntry(self, name, description, template, quiet=False): """ Public method to add a template entry to this group. - + @param name name of the entry (string) @param description description of the entry to add (string) @param template template text of the entry (string) @@ -97,41 +96,38 @@ if not quiet: EricMessageBox.critical( None, - QCoreApplication.translate("TemplateGroup", - "Add Template"), + QCoreApplication.translate("TemplateGroup", "Add Template"), QCoreApplication.translate( "TemplateGroup", """<p>The group <b>{0}</b> already contains a""" - """ template named <b>{1}</b>.</p>""") - .format(self.name, name)) + """ template named <b>{1}</b>.</p>""", + ).format(self.name, name), + ) return - + self.entries[name] = TemplateEntry(self, name, description, template) - - if ( - Preferences.getTemplates("AutoOpenGroups") and - not self.isExpanded() - ): + + if Preferences.getTemplates("AutoOpenGroups") and not self.isExpanded(): self.setExpanded(True) - + def removeEntry(self, name): """ Public method to remove a template entry from this group. - + @param name name of the entry to be removed (string) """ if name in self.entries: index = self.indexOfChild(self.entries[name]) self.takeChild(index) del self.entries[name] - + if ( - len(self.entries) == 0 and - Preferences.getTemplates("AutoOpenGroups") and - self.isExpanded() + len(self.entries) == 0 + and Preferences.getTemplates("AutoOpenGroups") + and self.isExpanded() ): self.setExpanded(False) - + def removeAllEntries(self): """ Public method to remove all template entries of this group. @@ -142,16 +138,16 @@ def hasEntry(self, name): """ Public method to check, if the group has an entry with the given name. - + @param name name of the entry to check for (string) @return flag indicating existence (boolean) """ return name in self.entries - + def getEntry(self, name): """ Public method to get an entry. - + @param name name of the entry to retrieve (string) @return reference to the entry (TemplateEntry) """ @@ -164,7 +160,7 @@ """ Public method to get the names of all entries, who's name starts with the given string. - + @param beginning string denoting the beginning of the template name (string) @return list of entry names found (list of strings) @@ -173,13 +169,13 @@ for name in self.entries: if name.startswith(beginning): names.append(name) - + return names def getAllEntries(self): """ Public method to retrieve all entries. - + @return list of all entries (list of TemplateEntry) """ return list(self.entries.values()) @@ -189,10 +185,11 @@ """ Class immplementing a template entry. """ + def __init__(self, parent, name, description, templateText): """ Constructor - + @param parent parent widget of the template entry (QWidget) @param name name of the entry (string) @param description descriptive text for the template (string) @@ -202,7 +199,7 @@ self.description = description self.template = templateText self.__extractVariables() - + super().__init__(parent, [self.__displayText()]) if Preferences.getTemplates("ShowTooltip"): self.setToolTip(0, self.template) @@ -210,20 +207,20 @@ def __displayText(self): """ Private method to generate the display text. - + @return display text (string) """ txt = ( "{0} - {1}".format(self.name, self.description) - if self.description else - self.name + if self.description + else self.name ) return txt - + def setName(self, name): """ Public method to update the name of the entry. - + @param name name of the entry (string) """ self.name = name @@ -232,7 +229,7 @@ def getName(self): """ Public method to get the name of the entry. - + @return name of the entry (string) """ return self.name @@ -240,7 +237,7 @@ def setDescription(self, description): """ Public method to update the description of the entry. - + @param description description of the entry (string) """ self.description = description @@ -249,7 +246,7 @@ def getDescription(self): """ Public method to get the description of the entry. - + @return description of the entry (string) """ return self.description @@ -257,15 +254,15 @@ def getGroupName(self): """ Public method to get the name of the group this entry belongs to. - + @return name of the group containing this entry (string) """ return self.parent().getName() - + def setTemplateText(self, templateText): """ Public method to update the template text. - + @param templateText text of the template entry (string) """ self.template = templateText @@ -276,7 +273,7 @@ def getTemplateText(self): """ Public method to get the template text. - + @return the template text (string) """ return self.template @@ -284,7 +281,7 @@ def getExpandedText(self, varDict, indent): """ Public method to get the template text with all variables expanded. - + @param varDict dictionary containing the texts of each variable with the variable name as key. @param indent indentation of the line receiving he expanded @@ -296,8 +293,8 @@ for var, val in list(varDict.items()): txt = ( self.__expandFormattedVariable(var, val, txt) - if var in self.formatedVariables else - txt.replace(var, val) + if var in self.formatedVariables + else txt.replace(var, val) ) sepchar = Preferences.getTemplates("SeparatorChar") txt = txt.replace("{0}{1}".format(sepchar, sepchar), sepchar) @@ -316,7 +313,7 @@ def __expandFormattedVariable(self, var, val, txt): """ Private method to expand a template variable with special formatting. - + @param var template variable name (string) @param val value of the template variable (string) @param txt template text (string) @@ -326,22 +323,21 @@ for line in txt.splitlines(): ind = line.find(var) if ind >= 0: - variableFormat = var[1:-1].split(':', 1)[1] - if variableFormat == 'rl': + variableFormat = var[1:-1].split(":", 1)[1] + if variableFormat == "rl": prefix = line[:ind] - postfix = line[ind + len(var):] + postfix = line[ind + len(var) :] for v in val.splitlines(): - t = "{0}{1}{2}{3}{4}".format( - t, os.linesep, prefix, v, postfix) - elif variableFormat == 'ml': + t = "{0}{1}{2}{3}{4}".format(t, os.linesep, prefix, v, postfix) + elif variableFormat == "ml": indent = line.replace(line.lstrip(), "") prefix = line[:ind] - postfix = line[ind + len(var):] + postfix = line[ind + len(var) :] for count, v in enumerate(val.splitlines()): t = ( "{0}{1}{2}{3}".format(t, os.linesep, indent, v) - if count else - "{0}{1}{2}{3}".format(t, os.linesep, prefix, v) + if count + else "{0}{1}{2}{3}".format(t, os.linesep, prefix, v) ) t = "{0}{1}".format(t, postfix) else: @@ -353,7 +349,7 @@ def getVariables(self): """ Public method to get the list of variables. - + @return list of variables (list of strings) """ return self.variables @@ -364,8 +360,7 @@ """ sepchar = Preferences.getTemplates("SeparatorChar") variablesPattern = re.compile( - r"""\{0}[a-zA-Z][a-zA-Z0-9_]*(?::(?:ml|rl))?\{1}""".format( - sepchar, sepchar) + r"""\{0}[a-zA-Z][a-zA-Z0-9_]*(?::(?:ml|rl))?\{1}""".format(sepchar, sepchar) ) variables = variablesPattern.findall(self.template) self.variables = [] @@ -373,7 +368,7 @@ for var in variables: if var not in self.variables: self.variables.append(var) - if var.find(':') >= 0 and var not in self.formatedVariables: + if var.find(":") >= 0 and var not in self.formatedVariables: self.formatedVariables.append(var) @@ -381,27 +376,29 @@ """ Class implementing the template viewer. """ + def __init__(self, parent, viewmanager): """ Constructor - + @param parent the parent (QWidget) @param viewmanager reference to the viewmanager object """ super().__init__(parent) - + self.viewmanager = viewmanager self.groups = {} - + self.setHeaderLabels(["Template"]) self.header().hide() self.header().setSortIndicator(0, Qt.SortOrder.AscendingOrder) self.setRootIsDecorated(True) self.setAlternatingRowColors(True) - + self.__menu = QMenu(self) self.applyAct = self.__menu.addAction( - self.tr("Apply"), self.__templateItemActivated) + self.tr("Apply"), self.__templateItemActivated + ) self.__menu.addSeparator() self.__menu.addAction(self.tr("Add entry..."), self.__addEntry) self.__menu.addAction(self.tr("Add group..."), self.__addGroup) @@ -413,47 +410,45 @@ self.__menu.addAction(self.tr("Export..."), self.__export) self.__menu.addAction(self.tr("Reload"), self.__reload) self.__menu.addSeparator() - self.__menu.addAction( - self.tr("Help about Templates..."), self.__showHelp) + self.__menu.addAction(self.tr("Help about Templates..."), self.__showHelp) self.__menu.addSeparator() self.__menu.addAction(self.tr("Configure..."), self.__configure) - + self.__backMenu = QMenu(self) self.__backMenu.addAction(self.tr("Add group..."), self.__addGroup) self.__backMenu.addSeparator() self.bmSaveAct = self.__backMenu.addAction(self.tr("Save"), self.save) self.__backMenu.addAction(self.tr("Import..."), self.__import) self.bmExportAct = self.__backMenu.addAction( - self.tr("Export..."), self.__export) + self.tr("Export..."), self.__export + ) self.__backMenu.addAction(self.tr("Reload"), self.__reload) self.__backMenu.addSeparator() - self.__backMenu.addAction( - self.tr("Help about Templates..."), self.__showHelp) + self.__backMenu.addAction(self.tr("Help about Templates..."), self.__showHelp) self.__backMenu.addSeparator() - self.__backMenu.addAction( - self.tr("Configure..."), self.__configure) - + self.__backMenu.addAction(self.tr("Configure..."), self.__configure) + self.__activating = False self.__dirty = False - + self.__templatesFile = TemplatesFile(self) - + self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) self.customContextMenuRequested.connect(self.__showContextMenu) self.itemActivated.connect(self.__templateItemActivated) - + self.setWindowIcon(UI.PixmapCache.getIcon("eric")) - + def __resort(self): """ Private method to resort the tree. """ self.sortItems(self.sortColumn(), self.header().sortIndicatorOrder()) - + def __templateItemActivated(self, itm=None, col=0): """ Private slot to handle the activation of an item. - + @param itm reference to the activated item (QTreeWidgetItem) @param col column the item was activated in (integer) """ @@ -463,11 +458,11 @@ if isinstance(itm, TemplateEntry): self.applyTemplate(itm) self.__activating = False - + def __showContextMenu(self, coord): """ Private slot to show the context menu of the list. - + @param coord the position of the mouse pointer (QPoint) """ itm = self.itemAt(coord) @@ -478,49 +473,51 @@ self.__backMenu.popup(coord) else: self.applyAct.setEnabled( - self.viewmanager.activeWindow() is not None and - isinstance(itm, TemplateEntry)) + self.viewmanager.activeWindow() is not None + and isinstance(itm, TemplateEntry) + ) self.saveAct.setEnabled(self.__dirty) self.__menu.popup(coord) - + def __addEntry(self): """ Private slot to handle the Add Entry context menu action. """ itm = self.currentItem() groupName = ( - itm.getName() - if isinstance(itm, TemplateGroup) else - itm.getGroupName() + itm.getName() if isinstance(itm, TemplateGroup) else itm.getGroupName() ) - + from .TemplatePropertiesDialog import TemplatePropertiesDialog + dlg = TemplatePropertiesDialog(self) dlg.setSelectedGroup(groupName) if dlg.exec() == QDialog.DialogCode.Accepted: name, description, groupName, template = dlg.getData() self.addEntry(groupName, name, description, template) self.__dirty = True - + def __addGroup(self): """ Private slot to handle the Add Group context menu action. """ from .TemplatePropertiesDialog import TemplatePropertiesDialog + dlg = TemplatePropertiesDialog(self, True) if dlg.exec() == QDialog.DialogCode.Accepted: name, language = dlg.getData() self.addGroup(name, language) self.__dirty = True - + def __edit(self): """ Private slot to handle the Edit context menu action. """ itm = self.currentItem() editGroup = not isinstance(itm, TemplateEntry) - + from .TemplatePropertiesDialog import TemplatePropertiesDialog + dlg = TemplatePropertiesDialog(self, editGroup, itm) if dlg.exec() == QDialog.DialogCode.Accepted: if editGroup: @@ -530,7 +527,7 @@ name, description, groupName, template = dlg.getData() self.changeEntry(itm, name, groupName, description, template) self.__dirty = True - + def __remove(self): """ Private slot to handle the Remove context menu action. @@ -539,8 +536,10 @@ res = EricMessageBox.yesNo( self, self.tr("Remove Template"), - self.tr("""<p>Do you really want to remove <b>{0}</b>?</p>""") - .format(itm.getName())) + self.tr("""<p>Do you really want to remove <b>{0}</b>?</p>""").format( + itm.getName() + ), + ) if not res: return @@ -567,10 +566,13 @@ self, self.tr("Import Templates"), "", - self.tr("Templates Files (*.ecj);;" - "XML Templates Files (*.e4c);;" - "All Files (*)")) - + self.tr( + "Templates Files (*.ecj);;" + "XML Templates Files (*.e4c);;" + "All Files (*)" + ), + ) + if fn: self.readTemplates(fn) self.__dirty = True @@ -583,11 +585,11 @@ self, self.tr("Export Templates"), "", - self.tr("Templates Files (*.ecj);;" - "All Files (*)"), + self.tr("Templates Files (*.ecj);;" "All Files (*)"), "", - EricFileDialog.DontConfirmOverwrite) - + EricFileDialog.DontConfirmOverwrite, + ) + if fn: fpath = pathlib.Path(fn) if not fpath.suffix: @@ -598,14 +600,17 @@ ok = EricMessageBox.yesNo( self, self.tr("Export Templates"), - self.tr("""<p>The templates file <b>{0}</b> exists""" - """ already. Overwrite it?</p>""").format(fpath)) + self.tr( + """<p>The templates file <b>{0}</b> exists""" + """ already. Overwrite it?</p>""" + ).format(fpath), + ) else: ok = True - + if ok: self.writeTemplates(str(fpath)) - + def __reload(self): """ Private slot to reload the templates. @@ -616,16 +621,18 @@ self.tr("Reload Templates"), self.tr( """The templates contain unsaved changes. Shall these""" - """ changes be discarded?"""), - icon=EricMessageBox.Warning) + """ changes be discarded?""" + ), + icon=EricMessageBox.Warning, + ) if not res: return - + self.clear() self.groups = {} - + self.readTemplates() - + def __showHelp(self): """ Private method to show some help. @@ -642,12 +649,14 @@ """<p><b>Template entries</b> are the actual templates.""" """ They are grouped by the template groups. Help about""" """ how to define them is available in the template edit""" - """ dialog.</p>""")) + """ dialog.</p>""" + ), + ) def __getPredefinedVars(self): """ Private method to return predefined variables. - + @return dictionary of predefined variables and their values """ project = ericApp().getObject("Project") @@ -656,16 +665,16 @@ sepchar = Preferences.getTemplates("SeparatorChar") keyfmt = sepchar + "{0}" + sepchar varValues = { - keyfmt.format('date'): now.date().isoformat(), - keyfmt.format('year'): str(now.date().year), - keyfmt.format('time'): now.time().strftime("%H:%M:%S"), + keyfmt.format("date"): now.date().isoformat(), + keyfmt.format("year"): str(now.date().year), + keyfmt.format("time"): now.time().strftime("%H:%M:%S"), } if project.name: - varValues[keyfmt.format('project_name')] = project.name + varValues[keyfmt.format("project_name")] = project.name if project.ppath: - varValues[keyfmt.format('project_path')] = project.ppath + varValues[keyfmt.format("project_path")] = project.ppath path_name = editor.getFileName() if path_name: @@ -675,68 +684,68 @@ ext = ext[1:] path_name_rel = project.getRelativePath(path_name) dir_name_rel = project.getRelativePath(dir_name) - varValues.update({ - keyfmt.format('path_name'): path_name, - keyfmt.format('path_name_rel'): path_name_rel, - keyfmt.format('dir_name'): dir_name, - keyfmt.format('dir_name_rel'): dir_name_rel, - keyfmt.format('file_name'): file_name, - keyfmt.format('base_name'): base_name, - keyfmt.format('ext'): ext - }) - - varValues[keyfmt.format('clipboard:ml')] = ( - QApplication.clipboard().text() - ) - varValues[keyfmt.format('clipboard')] = QApplication.clipboard().text() + varValues.update( + { + keyfmt.format("path_name"): path_name, + keyfmt.format("path_name_rel"): path_name_rel, + keyfmt.format("dir_name"): dir_name, + keyfmt.format("dir_name_rel"): dir_name_rel, + keyfmt.format("file_name"): file_name, + keyfmt.format("base_name"): base_name, + keyfmt.format("ext"): ext, + } + ) + + varValues[keyfmt.format("clipboard:ml")] = QApplication.clipboard().text() + varValues[keyfmt.format("clipboard")] = QApplication.clipboard().text() if editor.hasSelectedText(): - varValues[keyfmt.format('cur_select:ml')] = editor.selectedText() - varValues[keyfmt.format('cur_select')] = editor.selectedText() + varValues[keyfmt.format("cur_select:ml")] = editor.selectedText() + varValues[keyfmt.format("cur_select")] = editor.selectedText() else: - varValues[keyfmt.format('cur_select:ml')] = os.linesep - varValues[keyfmt.format('cur_select')] = "" + varValues[keyfmt.format("cur_select:ml")] = os.linesep + varValues[keyfmt.format("cur_select")] = "" - varValues[keyfmt.format('insertion')] = "i_n_s_e_r_t_i_o_n" - - varValues[keyfmt.format('select_start')] = "s_e_l_e_c_t_s_t_a_r_t" - varValues[keyfmt.format('select_end')] = "s_e_l_e_c_t_e_n_d" + varValues[keyfmt.format("insertion")] = "i_n_s_e_r_t_i_o_n" + + varValues[keyfmt.format("select_start")] = "s_e_l_e_c_t_s_t_a_r_t" + varValues[keyfmt.format("select_end")] = "s_e_l_e_c_t_e_n_d" return varValues def applyTemplate(self, itm): """ Public method to apply the template. - + @param itm reference to the template item to apply (TemplateEntry) """ editor = self.viewmanager.activeWindow() if editor is None: return - + ok = False variables = itm.getVariables() varValues = self.__getPredefinedVars() - + # Remove predefined variables from list so user doesn't have to fill # these values out in the dialog. for v in list(varValues.keys()): if v in variables: variables.remove(v) - + if variables: if Preferences.getTemplates("SingleDialog"): from .TemplateMultipleVariablesDialog import ( - TemplateMultipleVariablesDialog + TemplateMultipleVariablesDialog, ) + dlg = TemplateMultipleVariablesDialog(variables, self) if dlg.exec() == QDialog.DialogCode.Accepted: varValues.update(dlg.getVariables()) ok = True else: - from .TemplateSingleVariableDialog import ( - TemplateSingleVariableDialog - ) + from .TemplateSingleVariableDialog import TemplateSingleVariableDialog + for var in variables: dlg = TemplateSingleVariableDialog(var, self) if dlg.exec() == QDialog.DialogCode.Accepted: @@ -747,10 +756,9 @@ ok = True else: ok = True - + if ok: - line = editor.text( - editor.getCursorPosition()[0]).replace(os.linesep, "") + line = editor.text(editor.getCursorPosition()[0]).replace(os.linesep, "") indent = line.replace(line.lstrip(), "") txt, lines, count = itm.getExpandedText(varValues, indent) # It should be done in this way to allow undo @@ -760,19 +768,16 @@ editor.removeSelectedText() else: line, index = editor.getCursorPosition() - + if lines == 1: count += index else: if len(indent) > 0: count += len(indent) - + if "i_n_s_e_r_t_i_o_n" in txt and "s_e_l_e_c_t" in txt: - txt = ( - "'Insertion and selection can not be in" - " template together'" - ) - + txt = "'Insertion and selection can not be in" " template together'" + if "i_n_s_e_r_t_i_o_n" in txt: lines = 1 for aline in txt.splitlines(): @@ -787,7 +792,7 @@ break else: lines += 1 - + setselect = False if "s_e_l_e_c_t_s_t_a_r_t" in txt and "s_e_l_e_c_t_e_n_d" in txt: setselect = True @@ -807,22 +812,21 @@ break else: lineb += 1 - + editor.insert(txt) - + if setselect: - editor.setSelection(line + linea - 1, posa, - line + lineb - 1, posb) + editor.setSelection(line + linea - 1, posa, line + lineb - 1, posb) else: editor.setCursorPosition(line + lines - 1, count) - + editor.endUndoAction() editor.setFocus() def applyNamedTemplate(self, templateName, groupName=None): """ Public method to apply a template given a template name. - + @param templateName name of the template item to apply (string) @param groupName name of the group to get the entry from (string). None or empty means to apply the first template found with the @@ -840,34 +844,33 @@ if template is not None: self.applyTemplate(template) break - + def addEntry(self, groupName, name, description, template, quiet=False): """ Public method to add a template entry. - + @param groupName name of the group to add to (string) @param name name of the entry to add (string) @param description description of the entry to add (string) @param template template text of the entry (string) @param quiet flag indicating quiet operation (boolean) """ - self.groups[groupName].addEntry( - name, description, template, quiet=quiet) + self.groups[groupName].addEntry(name, description, template, quiet=quiet) self.__resort() - + def hasGroup(self, name): """ Public method to check, if a group with the given name exists. - + @param name name of the group to be checked for (string) @return flag indicating an existing group (boolean) """ return name in self.groups - + def addGroup(self, name, language="All"): """ Public method to add a group. - + @param name name of the group to be added (string) @param language programming language for the group (string) """ @@ -878,7 +881,7 @@ def changeGroup(self, oldname, newname, language="All"): """ Public method to rename a group. - + @param oldname old name of the group (string) @param newname new name of the group (string) @param language programming language for the group (string) @@ -888,30 +891,32 @@ EricMessageBox.warning( self, self.tr("Edit Template Group"), - self.tr("""<p>A template group with the name""" - """ <b>{0}</b> already exists.</p>""") - .format(newname)) + self.tr( + """<p>A template group with the name""" + """ <b>{0}</b> already exists.</p>""" + ).format(newname), + ) return - + self.groups[newname] = self.groups[oldname] del self.groups[oldname] self.groups[newname].setName(newname) - + self.groups[newname].setLanguage(language) self.__resort() def getAllGroups(self): """ Public method to get all groups. - + @return list of all groups (list of TemplateGroup) """ return list(self.groups.values()) - + def getGroupNames(self): """ Public method to get all group names. - + @return list of all group names (list of strings) """ groups = sorted(list(self.groups.keys())[:]) @@ -920,7 +925,7 @@ def removeGroup(self, itm): """ Public method to remove a group. - + @param itm template group to be removed (TemplateGroup) """ name = itm.getName() @@ -932,7 +937,7 @@ def removeEntry(self, itm): """ Public method to remove a template entry. - + @param itm template entry to be removed (TemplateEntry) """ groupName = itm.getGroupName() @@ -941,7 +946,7 @@ def changeEntry(self, itm, name, groupName, description, template): """ Public method to change a template entry. - + @param itm template entry to be changed (TemplateEntry) @param name new name for the entry (string) @param groupName name of the group the entry should belong to @@ -954,57 +959,55 @@ self.groups[itm.getGroupName()].removeEntry(itm.getName()) self.groups[groupName].addEntry(name, description, template) return - + if itm.getName() != name: # entry was renamed self.groups[groupName].removeEntry(itm.getName()) self.groups[groupName].addEntry(name, description, template) return - + tmpl = self.groups[groupName].getEntry(name) tmpl.setDescription(description) tmpl.setTemplateText(template) self.__resort() - + def writeTemplates(self, filename=None): """ Public method to write the templates data to a JSON file (.ecj). - + @param filename name of a templates file to write @type str @return flag indicating success @rtype bool """ if filename is None: - filename = os.path.join( - Utilities.getConfigDir(), "eric7templates.ecj") - + filename = os.path.join(Utilities.getConfigDir(), "eric7templates.ecj") + return self.__templatesFile.writeFile(filename) - + def readTemplates(self, filename=None): """ Public method to read in the templates file (.e4c). - + @param filename name of a templates file to read @type str """ if filename is None: # new JSON based file first - filename = os.path.join( - Utilities.getConfigDir(), "eric7templates.ecj") + filename = os.path.join(Utilities.getConfigDir(), "eric7templates.ecj") if not os.path.exists(filename): # old XML based file second - filename = os.path.join( - Utilities.getConfigDir(), "eric7templates.e4c") + filename = os.path.join(Utilities.getConfigDir(), "eric7templates.e4c") if not os.path.exists(filename): return - + if filename.endswith(".ecj"): self.__templatesFile.readFile(filename) else: f = QFile(filename) if f.open(QIODevice.OpenModeFlag.ReadOnly): from EricXML.TemplatesReader import TemplatesReader + reader = TemplatesReader(f, viewer=self) reader.readXML() f.close() @@ -1013,20 +1016,20 @@ self, self.tr("Read Templates"), self.tr( - "<p>The templates file <b>{0}</b> could not be read." - "</p>") - .format(filename)) - + "<p>The templates file <b>{0}</b> could not be read." "</p>" + ).format(filename), + ) + def __configure(self): """ Private method to open the configuration dialog. """ ericApp().getObject("UserInterface").showPreferences("templatesPage") - + def hasTemplate(self, entryName, groupName=None): """ Public method to check, if an entry of the given name exists. - + @param entryName name of the entry to check for (string) @param groupName name of the group to check for the entry (string). None or empty means to check all groups. @@ -1039,14 +1042,14 @@ groups = [] else: groups = list(self.groups.values()) - + return any(group.hasEntry(entryName) for group in groups) - + def getTemplateNames(self, start, groupName=None): """ Public method to get the names of templates starting with the given string. - + @param start start string of the name (string) @param groupName name of the group to get the entry from (string). None or empty means to look in all groups.