From 3ebfbe1020edbb32f0bd22026d161f3e4423c2bb Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Sun, 30 Sep 2007 17:06:33 +0100 Subject: [PATCH] fix startup without config --- mailplate | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/mailplate b/mailplate index 4d0b28c..e815be9 100755 --- 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' : None , '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,8 @@ 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) ### ### COMMAND LINE PARSING @@ -325,12 +329,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 -- 2.39.5