| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | # Copyright (C) 2001-2004 Python Software Foundation | 
					
						
							| 
									
										
										
										
											2004-10-03 03:16:19 +00:00
										 |  |  |  | # Author: Barry Warsaw | 
					
						
							|  |  |  |  | # Contact: email-sig@python.org | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | """Miscellaneous utilities.""" | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | import os | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | import re | 
					
						
							| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | import time | 
					
						
							|  |  |  |  | import base64 | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | import random | 
					
						
							| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | import socket | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | import warnings | 
					
						
							|  |  |  |  | from cStringIO import StringIO | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-05 19:54:52 +00:00
										 |  |  |  | from email._parseaddr import quote | 
					
						
							|  |  |  |  | from email._parseaddr import AddressList as _AddressList | 
					
						
							|  |  |  |  | from email._parseaddr import mktime_tz | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | # We need wormarounds for bugs in these methods in older Pythons (see below) | 
					
						
							| 
									
										
										
										
											2002-11-05 19:54:52 +00:00
										 |  |  |  | from email._parseaddr import parsedate as _parsedate | 
					
						
							|  |  |  |  | from email._parseaddr import parsedate_tz as _parsedate_tz | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-09 03:55:11 +00:00
										 |  |  |  | from quopri import decodestring as _qdecode | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | # Intrapackage imports | 
					
						
							| 
									
										
										
										
											2002-06-02 19:07:16 +00:00
										 |  |  |  | from email.Encoders import _bencode, _qencode | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | COMMASPACE = ', ' | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  | EMPTYSTRING = '' | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | UEMPTYSTRING = u'' | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | CRLF = '\r\n' | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2003-03-10 19:20:18 +00:00
										 |  |  |  | specialsre = re.compile(r'[][\\()<>@,:;".]') | 
					
						
							|  |  |  |  | escapesre = re.compile(r'[][\\()"]') | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-04 17:05:11 +00:00
										 |  |  |  |  | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | # Helpers | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def _identity(s): | 
					
						
							|  |  |  |  |     return s | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def _bdecode(s): | 
					
						
							|  |  |  |  |     # We can't quite use base64.encodestring() since it tacks on a "courtesy | 
					
						
							|  |  |  |  |     # newline".  Blech! | 
					
						
							|  |  |  |  |     if not s: | 
					
						
							|  |  |  |  |         return s | 
					
						
							|  |  |  |  |     value = base64.decodestring(s) | 
					
						
							| 
									
										
										
										
											2002-09-28 20:49:57 +00:00
										 |  |  |  |     if not s.endswith('\n') and value.endswith('\n'): | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  |         return value[:-1] | 
					
						
							|  |  |  |  |     return value | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  |  | 
					
						
							|  |  |  |  | def fix_eols(s): | 
					
						
							|  |  |  |  |     """Replace all line-ending characters with \r\n.""" | 
					
						
							|  |  |  |  |     # Fix newlines with no preceding carriage return | 
					
						
							|  |  |  |  |     s = re.sub(r'(?<!\r)\n', CRLF, s) | 
					
						
							|  |  |  |  |     # Fix carriage returns with no following newline | 
					
						
							|  |  |  |  |     s = re.sub(r'\r(?!\n)', CRLF, s) | 
					
						
							|  |  |  |  |     return s | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | def formataddr(pair): | 
					
						
							|  |  |  |  |     """The inverse of parseaddr(), this takes a 2-tuple of the form
 | 
					
						
							|  |  |  |  |     (realname, email_address) and returns the string value suitable | 
					
						
							| 
									
										
										
										
											2002-09-28 20:49:57 +00:00
										 |  |  |  |     for an RFC 2822 From, To or Cc header. | 
					
						
							| 
									
										
										
										
											2002-05-23 15:15:30 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  |     If the first element of pair is false, then the second element is | 
					
						
							|  |  |  |  |     returned unmodified. | 
					
						
							|  |  |  |  |     """
 | 
					
						
							|  |  |  |  |     name, address = pair | 
					
						
							|  |  |  |  |     if name: | 
					
						
							|  |  |  |  |         quotes = '' | 
					
						
							|  |  |  |  |         if specialsre.search(name): | 
					
						
							|  |  |  |  |             quotes = '"' | 
					
						
							|  |  |  |  |         name = escapesre.sub(r'\\\g<0>', name) | 
					
						
							|  |  |  |  |         return '%s%s%s <%s>' % (quotes, name, quotes, address) | 
					
						
							|  |  |  |  |     return address | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-04 17:05:11 +00:00
										 |  |  |  |  | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | def getaddresses(fieldvalues): | 
					
						
							|  |  |  |  |     """Return a list of (REALNAME, EMAIL) for each fieldvalue.""" | 
					
						
							|  |  |  |  |     all = COMMASPACE.join(fieldvalues) | 
					
						
							| 
									
										
										
										
											2002-04-12 20:50:05 +00:00
										 |  |  |  |     a = _AddressList(all) | 
					
						
							| 
									
										
										
										
											2002-05-22 01:52:10 +00:00
										 |  |  |  |     return a.addresslist | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-04 17:05:11 +00:00
										 |  |  |  |  | 
					
						
							| 
									
										
										
										
											2001-09-23 03:17:28 +00:00
										 |  |  |  | ecre = re.compile(r'''
 | 
					
						
							|  |  |  |  |   =\?                   # literal =? | 
					
						
							|  |  |  |  |   (?P<charset>[^?]*?)   # non-greedy up to the next ? is the charset | 
					
						
							|  |  |  |  |   \?                    # literal ? | 
					
						
							|  |  |  |  |   (?P<encoding>[qb])    # either a "q" or a "b", case insensitive | 
					
						
							|  |  |  |  |   \?                    # literal ? | 
					
						
							|  |  |  |  |   (?P<atom>.*?)         # non-greedy up to the next ?= is the atom | 
					
						
							|  |  |  |  |   \?=                   # literal ?= | 
					
						
							|  |  |  |  |   ''', re.VERBOSE | re.IGNORECASE)
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-09 16:59:56 +00:00
										 |  |  |  |  | 
					
						
							| 
									
										
										
										
											2004-10-11 13:53:08 +00:00
										 |  |  |  | def formatdate(timeval=None, localtime=False, usegmt=False): | 
					
						
							| 
									
										
										
										
											2001-11-09 17:07:28 +00:00
										 |  |  |  |     """Returns a date string as specified by RFC 2822, e.g.:
 | 
					
						
							| 
									
										
										
										
											2001-11-09 16:59:56 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     Fri, 09 Nov 2001 01:08:47 -0000 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-09 17:07:28 +00:00
										 |  |  |  |     Optional timeval if given is a floating point time value as accepted by | 
					
						
							|  |  |  |  |     gmtime() and localtime(), otherwise the current time is used. | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-28 20:49:57 +00:00
										 |  |  |  |     Optional localtime is a flag that when True, interprets timeval, and | 
					
						
							| 
									
										
										
										
											2001-11-09 17:07:28 +00:00
										 |  |  |  |     returns a date relative to the local timezone instead of UTC, properly | 
					
						
							|  |  |  |  |     taking daylight savings time into account. | 
					
						
							| 
									
										
										
										
											2004-10-11 13:53:08 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-12 21:51:32 +00:00
										 |  |  |  |     Optional argument usegmt means that the timezone is written out as | 
					
						
							| 
									
										
										
										
											2004-10-11 13:53:08 +00:00
										 |  |  |  |     an ascii string, not numeric one (so "GMT" instead of "+0000"). This | 
					
						
							|  |  |  |  |     is needed for HTTP, and is only used when localtime==False. | 
					
						
							| 
									
										
										
										
											2001-11-09 16:59:56 +00:00
										 |  |  |  |     """
 | 
					
						
							|  |  |  |  |     # Note: we cannot use strftime() because that honors the locale and RFC | 
					
						
							|  |  |  |  |     # 2822 requires that day and month names be the English abbreviations. | 
					
						
							|  |  |  |  |     if timeval is None: | 
					
						
							|  |  |  |  |         timeval = time.time() | 
					
						
							|  |  |  |  |     if localtime: | 
					
						
							|  |  |  |  |         now = time.localtime(timeval) | 
					
						
							|  |  |  |  |         # Calculate timezone offset, based on whether the local zone has | 
					
						
							|  |  |  |  |         # daylight savings time, and whether DST is in effect. | 
					
						
							|  |  |  |  |         if time.daylight and now[-1]: | 
					
						
							|  |  |  |  |             offset = time.altzone | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             offset = time.timezone | 
					
						
							| 
									
										
										
										
											2001-11-19 18:36:43 +00:00
										 |  |  |  |         hours, minutes = divmod(abs(offset), 3600) | 
					
						
							|  |  |  |  |         # Remember offset is in seconds west of UTC, but the timezone is in | 
					
						
							|  |  |  |  |         # minutes east of UTC, so the signs differ. | 
					
						
							|  |  |  |  |         if offset > 0: | 
					
						
							|  |  |  |  |             sign = '-' | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             sign = '+' | 
					
						
							| 
									
										
										
										
											2004-10-03 03:16:19 +00:00
										 |  |  |  |         zone = '%s%02d%02d' % (sign, hours, minutes // 60) | 
					
						
							| 
									
										
										
										
											2001-11-09 16:59:56 +00:00
										 |  |  |  |     else: | 
					
						
							|  |  |  |  |         now = time.gmtime(timeval) | 
					
						
							|  |  |  |  |         # Timezone offset is always -0000 | 
					
						
							| 
									
										
										
										
											2004-10-11 13:53:08 +00:00
										 |  |  |  |         if usegmt: | 
					
						
							|  |  |  |  |             zone = 'GMT' | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             zone = '-0000' | 
					
						
							| 
									
										
										
										
											2001-11-09 16:59:56 +00:00
										 |  |  |  |     return '%s, %02d %s %04d %02d:%02d:%02d %s' % ( | 
					
						
							|  |  |  |  |         ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]], | 
					
						
							|  |  |  |  |         now[2], | 
					
						
							|  |  |  |  |         ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', | 
					
						
							|  |  |  |  |          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1], | 
					
						
							|  |  |  |  |         now[0], now[3], now[4], now[5], | 
					
						
							|  |  |  |  |         zone) | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | def make_msgid(idstring=None): | 
					
						
							| 
									
										
										
										
											2002-10-01 00:44:13 +00:00
										 |  |  |  |     """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
 | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     <20020201195627.33539.96671@nightshade.la.mastaler.com> | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     Optional idstring if given is a string used to strengthen the | 
					
						
							| 
									
										
										
										
											2002-10-01 00:44:13 +00:00
										 |  |  |  |     uniqueness of the message id. | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  |     """
 | 
					
						
							|  |  |  |  |     timeval = time.time() | 
					
						
							|  |  |  |  |     utcdate = time.strftime('%Y%m%d%H%M%S', time.gmtime(timeval)) | 
					
						
							|  |  |  |  |     pid = os.getpid() | 
					
						
							|  |  |  |  |     randint = random.randrange(100000) | 
					
						
							|  |  |  |  |     if idstring is None: | 
					
						
							|  |  |  |  |         idstring = '' | 
					
						
							|  |  |  |  |     else: | 
					
						
							|  |  |  |  |         idstring = '.' + idstring | 
					
						
							|  |  |  |  |     idhost = socket.getfqdn() | 
					
						
							|  |  |  |  |     msgid = '<%s.%s.%s%s@%s>' % (utcdate, pid, randint, idstring, idhost) | 
					
						
							|  |  |  |  |     return msgid | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |  | 
					
						
							|  |  |  |  | # These functions are in the standalone mimelib version only because they've | 
					
						
							|  |  |  |  | # subsequently been fixed in the latest Python versions.  We use this to worm | 
					
						
							|  |  |  |  | # around broken older Pythons. | 
					
						
							|  |  |  |  | def parsedate(data): | 
					
						
							|  |  |  |  |     if not data: | 
					
						
							|  |  |  |  |         return None | 
					
						
							|  |  |  |  |     return _parsedate(data) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def parsedate_tz(data): | 
					
						
							|  |  |  |  |     if not data: | 
					
						
							|  |  |  |  |         return None | 
					
						
							|  |  |  |  |     return _parsedate_tz(data) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def parseaddr(addr): | 
					
						
							| 
									
										
										
										
											2002-04-15 22:00:25 +00:00
										 |  |  |  |     addrs = _AddressList(addr).addresslist | 
					
						
							|  |  |  |  |     if not addrs: | 
					
						
							| 
									
										
										
										
											2002-04-10 21:01:31 +00:00
										 |  |  |  |         return '', '' | 
					
						
							| 
									
										
										
										
											2002-04-15 22:00:25 +00:00
										 |  |  |  |     return addrs[0] | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-09-11 02:22:48 +00:00
										 |  |  |  | # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3. | 
					
						
							|  |  |  |  | def unquote(str): | 
					
						
							|  |  |  |  |     """Remove quotes from a string.""" | 
					
						
							|  |  |  |  |     if len(str) > 1: | 
					
						
							|  |  |  |  |         if str.startswith('"') and str.endswith('"'): | 
					
						
							|  |  |  |  |             return str[1:-1].replace('\\\\', '\\').replace('\\"', '"') | 
					
						
							|  |  |  |  |         if str.startswith('<') and str.endswith('>'): | 
					
						
							|  |  |  |  |             return str[1:-1] | 
					
						
							|  |  |  |  |     return str | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  |  | 
					
						
							|  |  |  |  | # RFC2231-related functions - parameter encoding and decoding | 
					
						
							|  |  |  |  | def decode_rfc2231(s): | 
					
						
							|  |  |  |  |     """Decode string according to RFC 2231""" | 
					
						
							|  |  |  |  |     import urllib | 
					
						
							| 
									
										
										
										
											2003-03-07 22:46:41 +00:00
										 |  |  |  |     parts = s.split("'", 2) | 
					
						
							|  |  |  |  |     if len(parts) == 1: | 
					
						
							| 
									
										
										
										
											2003-08-19 03:49:34 +00:00
										 |  |  |  |         return None, None, urllib.unquote(s) | 
					
						
							| 
									
										
										
										
											2003-03-07 22:46:41 +00:00
										 |  |  |  |     charset, language, s = parts | 
					
						
							|  |  |  |  |     return charset, language, urllib.unquote(s) | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def encode_rfc2231(s, charset=None, language=None): | 
					
						
							| 
									
										
										
										
											2002-10-01 00:44:13 +00:00
										 |  |  |  |     """Encode string according to RFC 2231.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     If neither charset nor language is given, then s is returned as-is.  If | 
					
						
							|  |  |  |  |     charset is given but not language, the string is encoded using the empty | 
					
						
							|  |  |  |  |     string for language. | 
					
						
							|  |  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  |     import urllib | 
					
						
							|  |  |  |  |     s = urllib.quote(s, safe='') | 
					
						
							|  |  |  |  |     if charset is None and language is None: | 
					
						
							|  |  |  |  |         return s | 
					
						
							| 
									
										
										
										
											2002-10-01 00:44:13 +00:00
										 |  |  |  |     if language is None: | 
					
						
							|  |  |  |  |         language = '' | 
					
						
							|  |  |  |  |     return "%s'%s'%s" % (charset, language, s) | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | rfc2231_continuation = re.compile(r'^(?P<name>\w+)\*((?P<num>[0-9]+)\*?)?$') | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | def decode_params(params): | 
					
						
							| 
									
										
										
										
											2002-10-01 00:44:13 +00:00
										 |  |  |  |     """Decode parameters list according to RFC 2231.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     params is a sequence of 2-tuples containing (content type, string value). | 
					
						
							|  |  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  |     new_params = [] | 
					
						
							|  |  |  |  |     # maps parameter's name to a list of continuations | 
					
						
							|  |  |  |  |     rfc2231_params = {} | 
					
						
							|  |  |  |  |     # params is a sequence of 2-tuples containing (content_type, string value) | 
					
						
							|  |  |  |  |     name, value = params[0] | 
					
						
							|  |  |  |  |     new_params.append((name, value)) | 
					
						
							|  |  |  |  |     # Cycle through each of the rest of the parameters. | 
					
						
							|  |  |  |  |     for name, value in params[1:]: | 
					
						
							|  |  |  |  |         value = unquote(value) | 
					
						
							|  |  |  |  |         mo = rfc2231_continuation.match(name) | 
					
						
							|  |  |  |  |         if mo: | 
					
						
							|  |  |  |  |             name, num = mo.group('name', 'num') | 
					
						
							|  |  |  |  |             if num is not None: | 
					
						
							|  |  |  |  |                 num = int(num) | 
					
						
							|  |  |  |  |             rfc2231_param1 = rfc2231_params.setdefault(name, []) | 
					
						
							|  |  |  |  |             rfc2231_param1.append((num, value)) | 
					
						
							|  |  |  |  |         else: | 
					
						
							|  |  |  |  |             new_params.append((name, '"%s"' % quote(value))) | 
					
						
							|  |  |  |  |     if rfc2231_params: | 
					
						
							|  |  |  |  |         for name, continuations in rfc2231_params.items(): | 
					
						
							|  |  |  |  |             value = [] | 
					
						
							|  |  |  |  |             # Sort by number | 
					
						
							|  |  |  |  |             continuations.sort() | 
					
						
							|  |  |  |  |             # And now append all values in num order | 
					
						
							|  |  |  |  |             for num, continuation in continuations: | 
					
						
							|  |  |  |  |                 value.append(continuation) | 
					
						
							|  |  |  |  |             charset, language, value = decode_rfc2231(EMPTYSTRING.join(value)) | 
					
						
							| 
									
										
										
										
											2003-03-07 22:46:41 +00:00
										 |  |  |  |             new_params.append( | 
					
						
							|  |  |  |  |                 (name, (charset, language, '"%s"' % quote(value)))) | 
					
						
							| 
									
										
										
										
											2002-06-29 05:58:04 +00:00
										 |  |  |  |     return new_params | 
					
						
							| 
									
										
										
										
											2004-10-03 03:16:19 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | def collapse_rfc2231_value(value, errors='replace', | 
					
						
							|  |  |  |  |                            fallback_charset='us-ascii'): | 
					
						
							|  |  |  |  |     if isinstance(value, tuple): | 
					
						
							|  |  |  |  |         rawval = unquote(value[2]) | 
					
						
							|  |  |  |  |         charset = value[0] or 'us-ascii' | 
					
						
							|  |  |  |  |         try: | 
					
						
							|  |  |  |  |             return unicode(rawval, charset, errors) | 
					
						
							|  |  |  |  |         except LookupError: | 
					
						
							|  |  |  |  |             # XXX charset is unknown to Python. | 
					
						
							|  |  |  |  |             return unicode(rawval, fallback_charset, errors) | 
					
						
							|  |  |  |  |     else: | 
					
						
							|  |  |  |  |         return unquote(value) |