email bug?

Stuart D. Gathman stuart at bmsi.com
Sun Aug 24 01:04:38 EDT 2003


On Sun, 24 Aug 2003 00:14:45 -0400, Andrew Dalke wrote:

 
> A quick scan of the code suggests that it isn't a quick fix (eg, not
> just a matter of tweaking that regexp.

Here is my quick (and probably incorrect) fix:

from email.Message import Message
from email.Utils import unquote

# helper to split params while ignoring ';' inside quotes
def _parseparam(str):
    plist = []
    while str[:1] == ';':
        str = str[1:]
        end = str.find(';')
        while end > 0 and (str.count('"',0,end) & 1):
          end = str.find(';',end + 1)
        if end < 0: end = len(str)
        f = str[:end]
        if '=' in f:
            i = f.index('=')
            f = f[:i].strip().lower() + \
                    '=' + f[i+1:].strip()
        plist.append(f.strip())
        str = str[end:]
    return plist

class MimeMessage(Message):

  def getparam(self,name,header='content-type'):
    for key,val in self.getparams(header):
      if key == name: return unquote(val)
    return None

  # like get_params but obey quotes
  def getparams(self,header='content-type'):
    "Return all parameter names and values. Use parser that handles quotes."
    val = self.get(header)
    result = []
    if val:
      plist = _parseparam(';' + val)
      for p in plist:
        i = p.find('=')
        if i >= 0: result.append((p[:i].lower(),unquote(p[i+1:])))
    return result

-- 
	      Stuart D. Gathman <stuart at bmsi.com>
Business Management Systems Inc.  Phone: 703 591-0911 Fax: 703 591-6154
"Confutatis maledictis, flamis acribus addictis" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.




More information about the Python-list mailing list