]> git.madduck.net Git - code/mailplate.git/blobdiff - mailplate

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

create default template if none exists
[code/mailplate.git] / mailplate
index 7e1a4d498f34ad88519a720c16ea41cd902ea893..6410670f73dd4833424578f3d3343bf0896a5336 100755 (executable)
--- a/mailplate
+++ b/mailplate
@@ -170,7 +170,7 @@ CONFFILE = os.path.expanduser(CONFFILE)
 MAILPLATEDIR = os.path.expanduser(MAILPLATEDIR)
 
 # defaults
-config = { 'default_template' : ''
+config = { 'default_template' : 'default'
          , 'template_path' : TEMPLATEDIR
          }
 helpers = { 'get_quote' : 'fortune -s' }
@@ -185,7 +185,7 @@ if not os.path.exists(CONFFILE):
         f = file(CONFFILE, 'w')
         f.write('# mailplate configuration\n[%s]\n' % SECTION_GENERAL)
         for kvpair in config.iteritems():
-            if len(kvpair[1]) > 0:
+            if kvpair[1] and len(kvpair[1]) > 0:
                 f.write('%s = %s\n' % kvpair)
 
         if len(helpers) > 0:
@@ -210,6 +210,8 @@ for key in config.keys():
     except ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError:
         err("E: no section '%s' in %s" % (SECTION_GENERAL, CONFFILE))
         sys.exit(posix.EX_CONFIG)
+    except ConfigParser.NoOptionError:
+        continue
     except ConfigParser.DuplicateSectionError, ConfigParser.ParseError:
         err('E: parse error on %s' % CONFFILE)
         sys.exit(posix.EX_CONFIG)
@@ -218,6 +220,16 @@ for key in config.keys():
 helpers.update(parser.items(SECTION_HELPERS))
 
 TPATH = os.path.expanduser(config['template_path'])
+if not os.path.isdir(TPATH):
+    os.mkdir(TPATH, 0700)
+
+default_templname = config['default_template']
+if default_templname is not None:
+    default_templpath = os.path.join(TPATH, default_templname)
+    if not os.path.isfile(default_templpath):
+        f = file(default_templpath, 'w')
+        f.write('@KEEP_STD_HEADERS\n\n@KEEP_BODY\n')
+        f.close()
 
 ###
 ### COMMAND LINE PARSING
@@ -295,12 +307,12 @@ if infname is not None:
 
 # read message into buffer, or preinitialise the buffer if --new is given
 if options.new:
-    rawmsg = '\n'.join((header + ': ' for k in STD_HEADERS)) + '\n'
+    rawmsg = '\n'.join((header + ': ' for header in STD_HEADERS)) + '\n'
 else:
     rawmsg = ''.join(inf.readlines())
 
 if options.auto:
-    best_score = (0, config['default_template'], {})
+    best_score = (0, default_templname, {})
     for tf in os.listdir(TPATH):
         tp = os.path.join(TPATH, tf)
         if not os.path.isfile(tp) or not os.access(tp, os.R_OK): continue
@@ -325,12 +337,28 @@ if options.auto:
             best_score = (score, tf, vars)
 
     templname = best_score[1]
+
+    if templname is None:
+        err('E: could not determine a template to use and no default is set')
+        sys.exit(posix.EX_CONFIG)
+
     print >>sys.stderr, \
             'I: Chose profile %s with score %d.' % (templname, best_score[0])
     vars = best_score[2]
 
 # now read in the template
-templ = file(os.path.join(TPATH, templname), 'r', 1)
+templpath = os.path.join(TPATH, templname)
+
+if not os.path.isfile(templpath):
+    err('E: not a template: ' + templpath)
+    sys.exit(posix.EX_OSFILE)
+
+elif not os.access(templpath, os.R_OK):
+    err('E: template ' + templpath + ' could not be read.')
+    sys.exit(posix.EX_OSFILE)
+
+templ = file(templpath, 'r', 1)
+
 for line in templ:
     if not options.auto and line[0] == REGEXPCHAR:
         # obtain variables from the regexps