Add a common function for reading template files
authorCatalin Marinas <catalin.marinas@gmail.com>
Mon, 10 Jul 2006 21:21:22 +0000 (22:21 +0100)
committerCatalin Marinas <catalin.marinas@gmail.com>
Mon, 10 Jul 2006 21:23:11 +0000 (22:23 +0100)
Useful to search through various directories without duplicating the code.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
stgit/commands/export.py
stgit/commands/mail.py
stgit/commands/new.py
stgit/stack.py
stgit/templates.py [new file with mode: 0644]

index fbf5690..4136b7f 100644 (file)
@@ -23,7 +23,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git, basedir
+from stgit import stack, git, templates
 
 
 help = 'exports a series of patches to <dir> (or patches)'
@@ -141,20 +141,11 @@ def func(parser, options, args):
 
     # get the template
     if options.template:
-        patch_tmpl_list = [options.template]
+        tmpl = file(options.template).read()
     else:
-        patch_tmpl_list = []
-
-    patch_tmpl_list += [os.path.join(basedir.get(), 'patchexport.tmpl'),
-                        os.path.join(os.path.expanduser('~'), '.stgit', 'templates',
-                                     'patchexport.tmpl'),
-                        os.path.join(sys.prefix,
-                                     'share', 'stgit', 'templates', 'patchexport.tmpl')]
-    tmpl = ''
-    for patch_tmpl in patch_tmpl_list:
-        if os.path.isfile(patch_tmpl):
-            tmpl = file(patch_tmpl).read()
-            break
+        tmpl = templates.get_template('patchexport.tmpl')
+        if not tmpl:
+            tmpl = ''
 
     # note the base commit for this series
     if not options.stdout:
index 3928b81..3cc71f6 100644 (file)
@@ -20,7 +20,7 @@ from optparse import OptionParser, make_option
 
 from stgit.commands.common import *
 from stgit.utils import *
-from stgit import stack, git, basedir, version
+from stgit import stack, git, version, templates
 from stgit.config import config
 
 
@@ -443,21 +443,11 @@ def func(parser, options, args):
     if options.cover or options.edit:
         # find the template file
         if options.cover:
-            tfile_list = [options.cover]
+            tmpl = file(options.template).read()
         else:
-            tfile_list = [os.path.join(basedir.get(), 'covermail.tmpl'),
-                          os.path.join(os.path.expanduser('~'), '.stgit', 'templates',
-                                       'covermail.tmpl'),
-                          os.path.join(sys.prefix,
-                                       'share', 'stgit', 'templates', 'covermail.tmpl')]
-
-        tmpl = None
-        for tfile in tfile_list:
-            if os.path.isfile(tfile):
-                tmpl = file(tfile).read()
-                break
-        if not tmpl:
-            raise CmdException, 'No cover message template file found'
+            tmpl = templates.get_template('covermail.tmpl')
+            if not tmpl:
+                raise CmdException, 'No cover message template file found'
 
         msg_id = email.Utils.make_msgid('stgit')
         msg = __build_cover(tmpl, total_nr, msg_id, options)
@@ -477,20 +467,11 @@ def func(parser, options, args):
 
     # send the patches
     if options.template:
-        tfile_list = [options.template]
+        tmpl = file(options.template).read()
     else:
-        tfile_list = [os.path.join(basedir.get(), 'patchmail.tmpl'),
-                      os.path.join(os.path.expanduser('~'), '.stgit', 'templates',
-                                   'patchmail.tmpl'),
-                      os.path.join(sys.prefix,
-                                   'share', 'stgit', 'templates', 'patchmail.tmpl')]
-    tmpl = None
-    for tfile in tfile_list:
-        if os.path.isfile(tfile):
-            tmpl = file(tfile).read()
-            break
-    if not tmpl:
-        raise CmdException, 'No e-mail template file found'
+        tmpl = templates.get_template('patchmail.tmpl')
+        if not tmpl:
+            raise CmdException, 'No e-mail template file found'
 
     for (p, patch_nr) in zip(patches, range(1, len(patches) + 1)):
         msg_id = email.Utils.make_msgid('stgit')
index b8e8446..2c1e94b 100644 (file)
@@ -29,9 +29,11 @@ usage = """%prog [options] <name>
 
 Create a new, empty patch and make it the topmost one. If the
 '--message' option is not passed, an editor is invoked with the
-.git/patchdescr.tmpl file used a as template, together with generated
-lines. By default, the local changes in the working tree are not included
-in the patch. A 'refresh' command is needed for this."""
+.git/patchdescr.tmpl, ~/.stgit/templates/patchdescr.tmpl or
+/usr/share/stgit/templates/patchdescr.tmpl file used a as template,
+together with generated lines. By default, the local changes in the
+working tree are not included in the patch. A 'refresh' command is
+needed for this."""
 
 options = [make_option('-m', '--message',
                        help = 'use MESSAGE as the patch description'),
index 0217a7f..914c1e3 100644 (file)
@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 import sys, os
 
 from stgit.utils import *
-from stgit import git, basedir
+from stgit import git, basedir, templates
 from stgit.config import config
 
 
@@ -66,13 +66,13 @@ def __clean_comments(f):
 
 def edit_file(series, line, comment, show_patch = True):
     fname = '.stgit.msg'
-    tmpl = os.path.join(basedir.get(), 'patchdescr.tmpl')
+    tmpl = templates.get_template('patchdescr.tmpl')
 
     f = file(fname, 'w+')
     if line:
         print >> f, line
-    elif os.path.isfile(tmpl):
-        print >> f, file(tmpl).read().rstrip()
+    elif tmpl:
+        print >> f, tmpl,
     else:
         print >> f
     print >> f, __comment_prefix, comment
diff --git a/stgit/templates.py b/stgit/templates.py
new file mode 100644 (file)
index 0000000..dc37b72
--- /dev/null
@@ -0,0 +1,42 @@
+"""Template files look-up
+"""
+
+__copyright__ = """
+Copyright (C) 2006, Catalin Marinas <catalin.marinas@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License version 2 as
+published by the Free Software Foundation.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""
+
+import sys, os
+
+from stgit import basedir
+
+
+def get_template(tfile):
+    """Return the string in the template file passed as argument or
+    None if the file wasn't found.
+    """
+    tmpl_list = [ os.path.join(basedir.get(), tfile),
+                  os.path.join(os.path.expanduser('~'), '.stgit', 'templates',
+                               tfile),
+                  os.path.join(sys.prefix, 'share', 'stgit', 'templates',
+                               tfile) ]
+
+    tmpl = None
+    for t in tmpl_list:
+        if os.path.isfile(t):
+            tmpl = file(t).read()
+            break
+
+    return tmpl