Code

fix broken tests by adding additional permissions now that we check for
[roundup.git] / roundup / rfc2822.py
index 0faa833f2e34d047af431c34fdcb16a96acb7a29..7e016c280fca036118bae1bc875e9bc08b4b3402 100644 (file)
@@ -1,3 +1,7 @@
+"""Some rfc822 functions taken from the new (python2.3) "email" module.
+"""
+__docformat__ = 'restructuredtext'
+
 import re
 from string import letters, digits
 from binascii import b2a_base64, a2b_base64
@@ -14,6 +18,8 @@ ecre = re.compile(r'''
 
 hqre = re.compile(r'^[A-z0-9!"#$%%&\'()*+,-./:;<=>?@\[\]^_`{|}~ ]+$')
 
+CRLF = '\r\n'
+
 def base64_decode(s, convert_eols=None):
     """Decode a raw base64 string.
 
@@ -37,7 +43,8 @@ def base64_decode(s, convert_eols=None):
     return dec
 
 def unquote_match(match):
-    """Turn a match in the form =AB to the ASCII character with value 0xab
+    """Turn a match in the form ``=AB`` to the ASCII character with value
+    0xab.
 
     Taken from 'email' module
     """
@@ -45,7 +52,7 @@ def unquote_match(match):
     return chr(int(s[1:3], 16))
 
 def qp_decode(s):
-    """Decode a string encoded with RFC 2045 MIME header `Q' encoding.
+    """Decode a string encoded with RFC 2045 MIME header 'Q' encoding.
 
     This function does not parse a full MIME header value encoded with
     quoted-printable (like =?iso-8895-1?q?Hello_World?=) -- please use
@@ -114,7 +121,7 @@ def decode_header(hdr):
         outs += unicode(section[0], charset or 'iso-8859-1', 'replace')
     return outs.encode('utf-8')
 
-def encode_header(header):
+def encode_header(header, charset='utf-8'):
     """ Will encode in quoted-printable encoding only if header 
     contains non latin characters
     """
@@ -127,7 +134,6 @@ def encode_header(header):
     if hqre.match(header):
         return header
     
-    charset = 'utf-8'
     quoted = ''
     #max_encoded = 76 - len(charset) - 7
     for c in header:
@@ -135,7 +141,7 @@ def encode_header(header):
         if c == ' ':
             quoted += '_'
         # These characters can be included verbatim
-        elif hqre.match(c):
+        elif hqre.match(c) and c not in '_=?':
             quoted += c
         # Otherwise, replace with hex value like =E2
         else: