]>
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:
 
 # -*- coding: utf-8 -*-
 #
 # mailplate — reformat mail drafts according to templates
 # -*- coding: utf-8 -*-
 #
 # mailplate — reformat mail drafts according to templates
 
 __name__ = 'mailplate'
 __description__ = 'reformat mail drafts according to templates'
 
 __name__ = 'mailplate'
 __description__ = 'reformat mail drafts according to templates'
 __author__ = 'martin f. krafft <madduck@madduck.net>'
 __copyright__ = 'Copyright © ' + __author__
 __licence__ = 'Artistic Licence 2.0'
 __author__ = 'martin f. krafft <madduck@madduck.net>'
 __copyright__ = 'Copyright © ' + __author__
 __licence__ = 'Artistic Licence 2.0'
 import re
 import sys
 import subprocess
 import re
 import sys
 import subprocess
 from optparse import OptionParser
 
 ###
 from optparse import OptionParser
 
 ###
             proc = subprocess.Popen(helpers[helper], shell=True,
                     stdout=subprocess.PIPE, stderr=sys.stderr)
             out = proc.communicate()[0]
             proc = subprocess.Popen(helpers[helper], shell=True,
                     stdout=subprocess.PIPE, stderr=sys.stderr)
             out = proc.communicate()[0]
-            s = s[:helper_begin] + out.strip() + s[helper_end+1:]
+            s = s[:helper_begin] + out.strip().decode()  + s[helper_end+1:]
         except KeyError:
             err('unknown helper: ' + helper)
             sys.exit(posix.EX_DATAERR)
         except KeyError:
             err('unknown helper: ' + helper)
             sys.exit(posix.EX_DATAERR)
 options, args = parser.parse_args()
 
 if options.version:
 options, args = parser.parse_args()
 
 if options.version:
-    print __name__, __version__ + ' — ' + __description__ 
-    print
-    print 'Written by ' + __author__ 
-    print __copyright__ 
-    print 'Released under the ' + __licence__ 
+    print(__name__, __version__ + ' — ' + __description__) 
+    print('') 
+    print('Written by ' + __author__) 
+    print(__copyright__) 
+    print('Released under the ' + __licence__) 
     sys.exit(posix.EX_OK)
 
 ###
     sys.exit(posix.EX_OK)
 
 ###
 
     if not os.path.isdir(MAILPLATEDIR):
         info('configuration directory not found, creating: ' + MAILPLATEDIR)
 
     if not os.path.isdir(MAILPLATEDIR):
         info('configuration directory not found, creating: ' + MAILPLATEDIR)
-        os.mkdir(MAILPLATEDIR, 0700)
+        os.mkdir(MAILPLATEDIR, o 0700)
 
     if not os.path.isfile(CONFFILE):
         info('creating a default configuration file: ' + CONFFILE)
 
     if not os.path.isfile(CONFFILE):
         info('creating a default configuration file: ' + CONFFILE)
     sys.exit(posix.EX_OSFILE)
 
 # now parse
     sys.exit(posix.EX_OSFILE)
 
 # now parse
-parser = ConfigParser.Safe ConfigParser()
+parser = configparser. ConfigParser()
 parser.read(CONFFILE)
 
 # first the GENERAL section into the config dict for all keys with defaults
 for key in config.keys():
     try:
         config[key] = parser.get(SECTION_GENERAL, key)
 parser.read(CONFFILE)
 
 # first the GENERAL section into the config dict for all keys with defaults
 for key in config.keys():
     try:
         config[key] = parser.get(SECTION_GENERAL, key)
-    except ConfigParser.NoSectionError, ConfigParser.MissingSectionHeaderError:
+    except (configparser.NoSectionError,
+            configparser.MissingSectionHeaderError):
         err("no section '%s' in %s" % (SECTION_GENERAL, CONFFILE))
         sys.exit(posix.EX_CONFIG)
         err("no section '%s' in %s" % (SECTION_GENERAL, CONFFILE))
         sys.exit(posix.EX_CONFIG)
-    except ConfigP arser.NoOptionError:
+    except configp arser.NoOptionError:
-    except ConfigParser.DuplicateSectionError, ConfigParser.ParseError:
+    except (configparser.DuplicateSectionError, 
+            configparser.ParseError):
         err('parse error on %s' % CONFFILE)
         sys.exit(posix.EX_CONFIG)
 
         err('parse error on %s' % CONFFILE)
         sys.exit(posix.EX_CONFIG)
 
 TPATH = os.path.expanduser(config['template_path'])
 if not os.path.isdir(TPATH):
     info('creating template directory: ' + TPATH)
 TPATH = os.path.expanduser(config['template_path'])
 if not os.path.isdir(TPATH):
     info('creating template directory: ' + TPATH)
 
 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):
         info('creating the default template: ' + default_templpath)
 
 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):
         info('creating the default template: ' + default_templpath)
-        f = file (default_templpath, 'w')
+        f = open (default_templpath, 'w')
         f.write('@KEEP_STD_HEADERS\n\n@KEEP_BODY\n')
         f.close()
 
         f.write('@KEEP_STD_HEADERS\n\n@KEEP_BODY\n')
         f.close()
 
 
 # read in the message from a file, if a filename is given.
 if infname is not None:
 
 # read in the message from a file, if a filename is given.
 if infname is not None:
-    inf = file (infname, 'r', 1)
+    inf = open (infname, 'r', 1)
 
 # read message into buffer, or preinitialise the buffer if --new is given
 if options.new:
 
 # read message into buffer, or preinitialise the buffer if --new is given
 if options.new:
     err('template ' + templpath + ' could not be read.')
     sys.exit(posix.EX_OSFILE)
 
     err('template ' + templpath + ' could not be read.')
     sys.exit(posix.EX_OSFILE)
 
-templ = file (templpath, 'r', 1)
+templ = open (templpath, 'r', 1)
 
 for line in templ:
     if not options.auto and line[0] == REGEXPCHAR:
 
 for line in templ:
     if not options.auto and line[0] == REGEXPCHAR:
         if len(l) == 0:
             payload = '' # end of headers
         elif l[0] == KEEP_SLOT_LEADER:
         if len(l) == 0:
             payload = '' # end of headers
         elif l[0] == KEEP_SLOT_LEADER:
-            if KEEP_HEADERS.has_key(l[1:]) :
+            if l[1:] in KEEP_HEADERS :
                 # found predefined header slot keyword
                 for header in KEEP_HEADERS[l[1:]]:
                     headers[header.lower()] = (header, _keep_header)
                 # found predefined header slot keyword
                 for header in KEEP_HEADERS[l[1:]]:
                     headers[header.lower()] = (header, _keep_header)
 for header, content in msg.items():
     # iterate all existing mail headers
     lheader = header.lower()
 for header, content in msg.items():
     # iterate all existing mail headers
     lheader = header.lower()
-    if headers.has_key(lheader) :
         # the template defines this header
         if headers[lheader][1] == _keep_header:
             # it's marked as keep, thus use content from email message
         # the template defines this header
         if headers[lheader][1] == _keep_header:
             # it's marked as keep, thus use content from email message
 
 # open the output file
 if outfname is not None:
 
 # open the output file
 if outfname is not None:
-    outf = file(outfname, 'w', 0 )
+    outf = open(outfname, 'w' )
 
 # print the headers, starting with the standard headers in order
 for header in STD_HEADERS:
 
 # print the headers, starting with the standard headers in order
 for header in STD_HEADERS:
     if headers.get(lheader, (None, _keep_header))[1] is not _keep_header:
         # the template header contains mandatory data, let's print it.
         hpair = headers[lheader]
     if headers.get(lheader, (None, _keep_header))[1] is not _keep_header:
         # the template header contains mandatory data, let's print it.
         hpair = headers[lheader]
-        print >>outf, ': '.join(hpair )
+        print(': '.join(hpair), file=outf )
         # and remove it from the dict
         del headers[lheader]
 
         # and remove it from the dict
         del headers[lheader]
 
-for i, (header, content) in headers.iterite ms():
+for i, (header, content) in headers.items():
     # print all remaining headers
     if content == _keep_header: continue
     # print all remaining headers
     if content == _keep_header: continue
-    print >>outf, ': '.join((header, content) )
+    print(': '.join((header, content)), file=outf )
 
 # print empty line to indicate end of headers.
 
 # print empty line to indicate end of headers.
 
 # split payload of existing message into body and signature
 body = msg.get_payload().rsplit(SIG_DELIM, 1)
 
 # split payload of existing message into body and signature
 body = msg.get_payload().rsplit(SIG_DELIM, 1)
 if keep_sig:
     payload = payload.replace('@KEEP_SIGNATURE', signature, 1)
 
 if keep_sig:
     payload = payload.replace('@KEEP_SIGNATURE', signature, 1)
 
-print >>outf, payload.rstrip( )
+print(payload.rstrip(), file=outf )
 outf.close()
 
 if options.edit:
 outf.close()
 
 if options.edit: