I just noticed that I am doing this waaaayyyy to complicated. How about just this:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import subprocess
from string import Template
import re
sub_temp = Template("nice -n 19 ionice -c3 /package/host/localhost/dspam/bin/dspam \
--source=error --class=$type --signature='$sig'")
sig_re = "[0-9A-Fa-f]*"
sig_test = re.compile("^" + sig_re + "$")
#check python version as we are using argpaser instead of optparser
if not ((int(sys.version_info[0]), int(sys.version[2])) >= (2,7)) :
print("This program uses packages only avaiable in python 2.7 and above")
#this should be build in since python 2.7
import argparse
def main():
parser = argparse.ArgumentParser(description='Pipe Inputmail to dspam-learn as ham or spam')
parser.add_argument('val', choices=['innocent', 'spam'])
args = parser.parse_args()
for line in sys.stdin:
if line.startswith("X-DSPAM-Signature:"):
sig = line.split()[-1]
if sig_test.match(sig):
subprocess.call(sub_temp.substitute(type=args.val, sig=sig), shell=True)
if __name__ == '__main__':

No mail parsing, works with attachments as well as normal forwarded emails and less stuff that could potentially blow up! Also, subprocess gets filtered for the signature so no