I'm attaching a patch to eric4-4.2-snapshot-20080719 that implements
predefined variables in templates. Predefined variables are automatically
computed - the user does not have to enter them in the template dialog.
I've found it very useful. If there's interest, I'd be happy to expand on
this feature (make it easy for user's to define their own predefined vars).
The following variables are defined:
$date$ Today's date in iso format
$year$ The current year
$project_name$ The name of the project (if any)
The following variables represent different parts of the current file's name.
As an example, consider the file "/home/bob/test.py"
$path_name$ The full path name: "/home/bob/test.py"
$dir_name$ The parent directory: "/home/bob"
$file_name$ The file name: "test.py"
$base_name$ The base name: "test"
$ext$ The extension: "py"
-Dan
--- eric4-4.2-snapshot-20080719/eric/Templates/TemplateViewer.py 2008-05-31 03:04:29.000000000 -0500
+++ eric4-4.2-snapshot-20080719.local/eric/Templates/TemplateViewer.py 2008-07-24 14:03:56.000000000 -0500
@@ -7,6 +7,7 @@
Module implementing a template viewer and associated classes.
"""
+import datetime
import os
import sys
import re
@@ -31,6 +32,7 @@
import Utilities
from KdeQt import KQMessageBox, KQFileDialog
+from KdeQt.KQApplication import e4App
class TemplateGroup(QTreeWidgetItem):
"""
@@ -516,6 +518,34 @@
""" dialog. There is an example template available in the"""
""" Examples subdirectory of the eric4 distribution.</p>"""))
+ def __getPredefinedVars(self):
+ """
+ Private method to return predefined variables.
+ """
+ project = e4App().getObject("Project")
+ editor = self.viewmanager.activeWindow()
+ today = datetime.datetime.now().date()
+
+ varValues={'$date$': today.isoformat(),
+ '$year$': str(today.year)}
+
+ if project.name:
+ varValues['$project_name$']=project.name
+
+ path_name = editor.getFileName()
+ if path_name:
+ dir_name, file_name = os.path.split(path_name)
+ base_name, ext = os.path.splitext(file_name)
+ varValues.update({
+ '$path_name$': path_name,
+ '$dir_name$': dir_name,
+ '$file_name$': file_name,
+ '$base_name$': base_name,
+ '$ext$': ext
+ })
+ return varValues
+
+
def applyTemplate(self, itm):
"""
Public method to apply the template.
@@ -528,14 +558,19 @@
ok = False
vars = 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 varValues.keys():
+ if v in vars:
+ vars.remove(v)
if vars:
if Preferences.getTemplates("SingleDialog"):
dlg = TemplateMultipleVariablesDialog(vars, self)
if dlg.exec_() == QDialog.Accepted:
- varValues = dlg.getVariables()
+ varValues.update(dlg.getVariables())
ok = True
else:
- varValues = {}
for var in vars:
dlg = TemplateSingleVariableDialog(var, self)
if dlg.exec_() == QDialog.Accepted:
@@ -545,7 +580,6 @@
del dlg
ok = True
else:
- varValues = {}
ok = True
if ok:
_______________________________________________
Eric mailing list
[email protected]
http://www.riverbankcomputing.com/mailman/listinfo/eric