9 |
9 |
10 import datetime |
10 import datetime |
11 import os |
11 import os |
12 import sys |
12 import sys |
13 import re |
13 import re |
14 import cStringIO |
14 import io |
15 |
15 |
16 from PyQt4.QtCore import * |
16 from PyQt4.QtCore import * |
17 from PyQt4.QtGui import * |
17 from PyQt4.QtGui import * |
18 |
18 |
19 from E4Gui.E4Application import e4App |
19 from E4Gui.E4Application import e4App |
20 |
20 |
21 from TemplatePropertiesDialog import TemplatePropertiesDialog |
21 from .TemplatePropertiesDialog import TemplatePropertiesDialog |
22 from TemplateMultipleVariablesDialog import TemplateMultipleVariablesDialog |
22 from .TemplateMultipleVariablesDialog import TemplateMultipleVariablesDialog |
23 from TemplateSingleVariableDialog import TemplateSingleVariableDialog |
23 from .TemplateSingleVariableDialog import TemplateSingleVariableDialog |
24 |
24 |
25 import Preferences |
25 import Preferences |
26 |
26 |
27 from E4XML.XMLUtilities import make_parser |
27 from E4XML.XMLUtilities import make_parser |
28 from E4XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError |
28 from E4XML.XMLErrorHandler import XMLErrorHandler, XMLFatalParseError |
132 |
132 |
133 def removeAllEntries(self): |
133 def removeAllEntries(self): |
134 """ |
134 """ |
135 Public method to remove all template entries of this group. |
135 Public method to remove all template entries of this group. |
136 """ |
136 """ |
137 for name in self.entries.keys()[:]: |
137 for name in list(self.entries.keys())[:]: |
138 self.removeEntry(name) |
138 self.removeEntry(name) |
139 |
139 |
140 def hasEntry(self, name): |
140 def hasEntry(self, name): |
141 """ |
141 """ |
142 Public method to check, if the group has an entry with the given name. |
142 Public method to check, if the group has an entry with the given name. |
178 """ |
178 """ |
179 Public method to retrieve all entries. |
179 Public method to retrieve all entries. |
180 |
180 |
181 @return list of all entries (list of TemplateEntry) |
181 @return list of all entries (list of TemplateEntry) |
182 """ |
182 """ |
183 return self.entries.values() |
183 return list(self.entries.values()) |
184 |
184 |
185 class TemplateEntry(QTreeWidgetItem): |
185 class TemplateEntry(QTreeWidgetItem): |
186 """ |
186 """ |
187 Class immplementing a template entry. |
187 Class immplementing a template entry. |
188 """ |
188 """ |
287 template text (string) |
287 template text (string) |
288 @return a tuple of the expanded template text (string), the |
288 @return a tuple of the expanded template text (string), the |
289 number of lines (integer) and the length of the last line (integer) |
289 number of lines (integer) and the length of the last line (integer) |
290 """ |
290 """ |
291 txt = self.template |
291 txt = self.template |
292 for var, val in varDict.items(): |
292 for var, val in list(varDict.items()): |
293 if var in self.formatedVariables: |
293 if var in self.formatedVariables: |
294 txt = self.__expandFormattedVariable(var, val, txt) |
294 txt = self.__expandFormattedVariable(var, val, txt) |
295 else: |
295 else: |
296 txt = txt.replace(var, val) |
296 txt = txt.replace(var, val) |
297 sepchar = Preferences.getTemplates("SeparatorChar") |
297 sepchar = Preferences.getTemplates("SeparatorChar") |
626 vars = itm.getVariables() |
626 vars = itm.getVariables() |
627 varValues = self.__getPredefinedVars() |
627 varValues = self.__getPredefinedVars() |
628 |
628 |
629 # Remove predefined variables from list so user doesn't have to fill |
629 # Remove predefined variables from list so user doesn't have to fill |
630 # these values out in the dialog. |
630 # these values out in the dialog. |
631 for v in varValues.keys(): |
631 for v in list(varValues.keys()): |
632 if v in vars: |
632 if v in vars: |
633 vars.remove(v) |
633 vars.remove(v) |
634 |
634 |
635 if vars: |
635 if vars: |
636 if Preferences.getTemplates("SingleDialog"): |
636 if Preferences.getTemplates("SingleDialog"): |
670 """ |
670 """ |
671 Public method to apply a template given a template name. |
671 Public method to apply a template given a template name. |
672 |
672 |
673 @param templateName name of the template item to apply (string) |
673 @param templateName name of the template item to apply (string) |
674 """ |
674 """ |
675 for group in self.groups.values(): |
675 for group in list(self.groups.values()): |
676 template = group.getEntry(templateName) |
676 template = group.getEntry(templateName) |
677 if template is not None: |
677 if template is not None: |
678 self.applyTemplate(template) |
678 self.applyTemplate(template) |
679 break |
679 break |
680 |
680 |
730 """ |
730 """ |
731 Public method to get all groups. |
731 Public method to get all groups. |
732 |
732 |
733 @return list of all groups (list of TemplateGroup) |
733 @return list of all groups (list of TemplateGroup) |
734 """ |
734 """ |
735 return self.groups.values() |
735 return list(self.groups.values()) |
736 |
736 |
737 def getGroupNames(self): |
737 def getGroupNames(self): |
738 """ |
738 """ |
739 Public method to get all group names. |
739 Public method to get all group names. |
740 |
740 |
741 @return list of all group names (list of strings) |
741 @return list of all group names (list of strings) |
742 """ |
742 """ |
743 groups = sorted(self.groups.keys()[:]) |
743 groups = sorted(list(self.groups.keys())[:]) |
744 return groups |
744 return groups |
745 |
745 |
746 def removeGroup(self, itm): |
746 def removeGroup(self, itm): |
747 """ |
747 """ |
748 Public method to remove a group. |
748 Public method to remove a group. |
799 @param filename name of a templates file to read (string) |
799 @param filename name of a templates file to read (string) |
800 """ |
800 """ |
801 try: |
801 try: |
802 if filename is None: |
802 if filename is None: |
803 filename = os.path.join(Utilities.getConfigDir(), "eric4templates.e4c") |
803 filename = os.path.join(Utilities.getConfigDir(), "eric4templates.e4c") |
804 f = open(filename, "wb") |
804 f = open(filename, "w") |
805 |
805 |
806 TemplatesWriter(f, self).writeXML() |
806 TemplatesWriter(f, self).writeXML() |
807 |
807 |
808 f.close() |
808 f.close() |
809 except IOError: |
809 except IOError: |
821 try: |
821 try: |
822 if filename is None: |
822 if filename is None: |
823 filename = os.path.join(Utilities.getConfigDir(), "eric4templates.e4c") |
823 filename = os.path.join(Utilities.getConfigDir(), "eric4templates.e4c") |
824 if not os.path.exists(filename): |
824 if not os.path.exists(filename): |
825 return |
825 return |
826 f = open(filename, "rb") |
826 f = open(filename, "r") |
827 line = f.readline() |
827 line = f.readline() |
828 dtdLine = f.readline() |
828 dtdLine = f.readline() |
829 f.close() |
829 f.close() |
830 except IOError: |
830 except IOError: |
831 QMessageBox.critical(None, |
831 QMessageBox.critical(None, |
844 parser.setContentHandler(handler) |
844 parser.setContentHandler(handler) |
845 parser.setEntityResolver(er) |
845 parser.setEntityResolver(er) |
846 parser.setErrorHandler(eh) |
846 parser.setErrorHandler(eh) |
847 |
847 |
848 try: |
848 try: |
849 f = open(filename, "rb") |
849 f = open(filename, "r") |
850 try: |
850 try: |
851 try: |
851 try: |
852 parser.parse(f) |
852 parser.parse(f) |
853 except UnicodeEncodeError: |
853 except UnicodeEncodeError: |
854 f.seek(0) |
854 f.seek(0) |
855 buf = cStringIO.StringIO(f.read()) |
855 buf = io.StringIO(f.read()) |
856 parser.parse(buf) |
856 parser.parse(buf) |
857 finally: |
857 finally: |
858 f.close() |
858 f.close() |
859 except IOError: |
859 except IOError: |
860 QMessageBox.critical(None, |
860 QMessageBox.critical(None, |
884 Public method to check, if an entry of the given name exists. |
884 Public method to check, if an entry of the given name exists. |
885 |
885 |
886 @param entryName name of the entry to check for (string) |
886 @param entryName name of the entry to check for (string) |
887 @return flag indicating the existence (boolean) |
887 @return flag indicating the existence (boolean) |
888 """ |
888 """ |
889 for group in self.groups.values(): |
889 for group in list(self.groups.values()): |
890 if group.hasEntry(entryName): |
890 if group.hasEntry(entryName): |
891 return True |
891 return True |
892 |
892 |
893 return False |
893 return False |
894 |
894 |