]> git.ozlabs.org Git - patchwork/blobdiff - apps/patchwork/bin/parsemail.py
[parser] Accept x-diff patches
[patchwork] / apps / patchwork / bin / parsemail.py
index a3edad43561f12667fc4f5c10ecc5867acf89022..d73343a9407fd5a0c36f2af0604f414189f3595c 100755 (executable)
@@ -38,16 +38,22 @@ from patchwork.models import Patch, Project, Person, Comment
 
 list_id_headers = ['List-ID', 'X-Mailing-List']
 
+whitespace_re = re.compile('\s+')
+def normalise_space(str):
+    return whitespace_re.sub(' ', str).strip()
+
 def clean_header(header):
     """ Decode (possibly non-ascii) headers """
 
-    def decode(str, fragment):
+    def decode(fragment):
         (frag_str, frag_encoding) = fragment
         if frag_encoding:
-            return str + frag_str.decode(frag_encoding)
-        return str + frag_str.decode()
+            return frag_str.decode(frag_encoding)
+        return frag_str.decode()
+
+    fragments = map(decode, decode_header(header))
 
-    return reduce(decode, decode_header(header), u'').strip()
+    return normalise_space(u' '.join(fragments))
 
 def find_project(mail):
     project = None
@@ -131,21 +137,13 @@ def find_content(project, mail):
         if part.get_content_maintype() != 'text':
             continue
 
-        #print "\t%s, %s" % \
-        #    (part.get_content_subtype(), part.get_content_charset())
+        payload = part.get_payload(decode=True)
+        subtype = part.get_content_subtype()
 
-        charset = part.get_content_charset()
-        if not charset:
-            charset = mail.get_charset()
-        if not charset:
-            charset = 'utf-8'
-
-        payload = unicode(part.get_payload(decode=True), charset, "replace")
-
-        if part.get_content_subtype() == 'x-patch':
+        if subtype in ['x-patch', 'x-diff']:
             patchbuf = payload
 
-        if part.get_content_subtype() == 'plain':
+        elif subtype == 'plain':
             if not patchbuf:
                 (patchbuf, c) = parse_patch(payload)
             else:
@@ -234,7 +232,6 @@ def split_prefixes(prefix):
 
 re_re = re.compile('^(re|fwd?)[:\s]\s*', re.I)
 prefix_re = re.compile('^\[([^\]]*)\]\s*(.*)$')
-whitespace_re = re.compile('\s+')
 
 def clean_subject(subject, drop_prefixes = None):
     """ Clean a Subject: header from an incoming patch.
@@ -286,8 +283,7 @@ def clean_subject(subject, drop_prefixes = None):
     # remove Re:, Fwd:, etc
     subject = re_re.sub(' ', subject)
 
-    # normalise whitespace
-    subject = whitespace_re.sub(' ', subject)
+    subject = normalise_space(subject)
 
     prefixes = []
 
@@ -301,7 +297,7 @@ def clean_subject(subject, drop_prefixes = None):
         subject = match.group(2)
         match = prefix_re.match(subject)
 
-    subject = whitespace_re.sub(' ', subject)
+    subject = normalise_space(subject)
 
     subject = subject.strip()
     if prefixes: