From: Catalin Marinas Date: Mon, 10 Jul 2006 21:21:22 +0000 (+0100) Subject: Add a common function for reading template files X-Git-Tag: v0.14.3~473 X-Git-Url: https://git.distorted.org.uk/~mdw/stgit/commitdiff_plain/1f3bb0172efa4e5edf43f5407306f0cf3e1b717e Add a common function for reading template files Useful to search through various directories without duplicating the code. Signed-off-by: Catalin Marinas --- diff --git a/stgit/commands/export.py b/stgit/commands/export.py index fbf5690..4136b7f 100644 --- a/stgit/commands/export.py +++ b/stgit/commands/export.py @@ -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 (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: diff --git a/stgit/commands/mail.py b/stgit/commands/mail.py index 3928b81..3cc71f6 100644 --- a/stgit/commands/mail.py +++ b/stgit/commands/mail.py @@ -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') diff --git a/stgit/commands/new.py b/stgit/commands/new.py index b8e8446..2c1e94b 100644 --- a/stgit/commands/new.py +++ b/stgit/commands/new.py @@ -29,9 +29,11 @@ usage = """%prog [options] 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'), diff --git a/stgit/stack.py b/stgit/stack.py index 0217a7f..914c1e3 100644 --- a/stgit/stack.py +++ b/stgit/stack.py @@ -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 index 0000000..dc37b72 --- /dev/null +++ b/stgit/templates.py @@ -0,0 +1,42 @@ +"""Template files look-up +""" + +__copyright__ = """ +Copyright (C) 2006, Catalin Marinas + +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