Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12)

Side by Side Diff: Lib/email/utils.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/email/_parseaddr.py ('k') | Lib/filecmp.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright (C) 2001-2010 Python Software Foundation 1 # Copyright (C) 2001-2010 Python Software Foundation
2 # Author: Barry Warsaw 2 # Author: Barry Warsaw
3 # Contact: email-sig@python.org 3 # Contact: email-sig@python.org
4 4
5 """Miscellaneous utilities.""" 5 """Miscellaneous utilities."""
6 6
7 __all__ = [ 7 __all__ = [
8 'collapse_rfc2231_value', 8 'collapse_rfc2231_value',
9 'decode_params', 9 'decode_params',
10 'decode_rfc2231', 10 'decode_rfc2231',
11 'encode_rfc2231', 11 'encode_rfc2231',
12 'formataddr', 12 'formataddr',
13 'formatdate', 13 'formatdate',
14 'format_datetime',
15 'getaddresses', 14 'getaddresses',
16 'make_msgid', 15 'make_msgid',
17 'mktime_tz', 16 'mktime_tz',
18 'parseaddr', 17 'parseaddr',
19 'parsedate', 18 'parsedate',
20 'parsedate_tz', 19 'parsedate_tz',
21 'parsedate_to_datetime',
22 'unquote', 20 'unquote',
23 ] 21 ]
24 22
25 import os 23 import os
26 import re 24 import re
27 import time 25 import time
28 import base64 26 import base64
29 import random 27 import random
30 import socket 28 import socket
31 import datetime
32 import urllib.parse 29 import urllib.parse
33 import warnings 30 import warnings
34 from io import StringIO 31 from io import StringIO
35 32
36 from email._parseaddr import quote 33 from email._parseaddr import quote
37 from email._parseaddr import AddressList as _AddressList 34 from email._parseaddr import AddressList as _AddressList
38 from email._parseaddr import mktime_tz 35 from email._parseaddr import mktime_tz
39 36
40 # We need wormarounds for bugs in these methods in older Pythons (see below) 37 # We need wormarounds for bugs in these methods in older Pythons (see below)
41 from email._parseaddr import parsedate as _parsedate 38 from email._parseaddr import parsedate as _parsedate
42 from email._parseaddr import parsedate_tz as _parsedate_tz 39 from email._parseaddr import parsedate_tz as _parsedate_tz
43 from email._parseaddr import _parsedate_tz as __parsedate_tz
44 40
45 from quopri import decodestring as _qdecode 41 from quopri import decodestring as _qdecode
46 42
47 # Intrapackage imports 43 # Intrapackage imports
48 from email.encoders import _bencode, _qencode 44 from email.encoders import _bencode, _qencode
49 from email.charset import Charset 45 from email.charset import Charset
50 46
51 COMMASPACE = ', ' 47 COMMASPACE = ', '
52 EMPTYSTRING = '' 48 EMPTYSTRING = ''
53 UEMPTYSTRING = '' 49 UEMPTYSTRING = ''
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 =\? # literal =? 103 =\? # literal =?
108 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset 104 (?P<charset>[^?]*?) # non-greedy up to the next ? is the charset
109 \? # literal ? 105 \? # literal ?
110 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive 106 (?P<encoding>[qb]) # either a "q" or a "b", case insensitive
111 \? # literal ? 107 \? # literal ?
112 (?P<atom>.*?) # non-greedy up to the next ?= is the atom 108 (?P<atom>.*?) # non-greedy up to the next ?= is the atom
113 \?= # literal ?= 109 \?= # literal ?=
114 ''', re.VERBOSE | re.IGNORECASE) 110 ''', re.VERBOSE | re.IGNORECASE)
115 111
116 112
117 def _format_timetuple_and_zone(timetuple, zone):
118 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
119 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][timetuple[6]],
120 timetuple[2],
121 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
122 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][timetuple[1] - 1],
123 timetuple[0], timetuple[3], timetuple[4], timetuple[5],
124 zone)
125 113
126 def formatdate(timeval=None, localtime=False, usegmt=False): 114 def formatdate(timeval=None, localtime=False, usegmt=False):
127 """Returns a date string as specified by RFC 2822, e.g.: 115 """Returns a date string as specified by RFC 2822, e.g.:
128 116
129 Fri, 09 Nov 2001 01:08:47 -0000 117 Fri, 09 Nov 2001 01:08:47 -0000
130 118
131 Optional timeval if given is a floating point time value as accepted by 119 Optional timeval if given is a floating point time value as accepted by
132 gmtime() and localtime(), otherwise the current time is used. 120 gmtime() and localtime(), otherwise the current time is used.
133 121
134 Optional localtime is a flag that when True, interprets timeval, and 122 Optional localtime is a flag that when True, interprets timeval, and
(...skipping 24 matching lines...) Expand all
159 else: 147 else:
160 sign = '+' 148 sign = '+'
161 zone = '%s%02d%02d' % (sign, hours, minutes // 60) 149 zone = '%s%02d%02d' % (sign, hours, minutes // 60)
162 else: 150 else:
163 now = time.gmtime(timeval) 151 now = time.gmtime(timeval)
164 # Timezone offset is always -0000 152 # Timezone offset is always -0000
165 if usegmt: 153 if usegmt:
166 zone = 'GMT' 154 zone = 'GMT'
167 else: 155 else:
168 zone = '-0000' 156 zone = '-0000'
169 return _format_timetuple_and_zone(now, zone) 157 return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
158 ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][now[6]],
159 now[2],
160 ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
161 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][now[1] - 1],
162 now[0], now[3], now[4], now[5],
163 zone)
170 164
171 def format_datetime(dt, usegmt=False):
172 """Turn a datetime into a date string as specified in RFC 2822.
173
174 If usegmt is True, dt must be an aware datetime with an offset of zero. In
175 this case 'GMT' will be rendered instead of the normal +0000 required by
176 RFC2822. This is to support HTTP headers involving date stamps.
177 """
178 now = dt.timetuple()
179 if usegmt:
180 if dt.tzinfo is None or dt.tzinfo != datetime.timezone.utc:
181 raise ValueError("usegmt option requires a UTC datetime")
182 zone = 'GMT'
183 elif dt.tzinfo is None:
184 zone = '-0000'
185 else:
186 zone = dt.strftime("%z")
187 return _format_timetuple_and_zone(now, zone)
188 165
189 166
190 def make_msgid(idstring=None, domain=None): 167 def make_msgid(idstring=None, domain=None):
191 """Returns a string suitable for RFC 2822 compliant Message-ID, e.g: 168 """Returns a string suitable for RFC 2822 compliant Message-ID, e.g:
192 169
193 <20020201195627.33539.96671@nightshade.la.mastaler.com> 170 <20020201195627.33539.96671@nightshade.la.mastaler.com>
194 171
195 Optional idstring if given is a string used to strengthen the 172 Optional idstring if given is a string used to strengthen the
196 uniqueness of the message id. Optional domain if given provides the 173 uniqueness of the message id. Optional domain if given provides the
197 portion of the message id after the '@'. It defaults to the locally 174 portion of the message id after the '@'. It defaults to the locally
(...skipping 20 matching lines...) Expand all
218 def parsedate(data): 195 def parsedate(data):
219 if not data: 196 if not data:
220 return None 197 return None
221 return _parsedate(data) 198 return _parsedate(data)
222 199
223 200
224 def parsedate_tz(data): 201 def parsedate_tz(data):
225 if not data: 202 if not data:
226 return None 203 return None
227 return _parsedate_tz(data) 204 return _parsedate_tz(data)
228
229 def parsedate_to_datetime(data):
230 if not data:
231 return None
232 *dtuple, tz = __parsedate_tz(data)
233 if tz is None:
234 return datetime.datetime(*dtuple[:6])
235 return datetime.datetime(*dtuple[:6],
236 tzinfo=datetime.timezone(datetime.timedelta(seconds=tz)))
237 205
238 206
239 def parseaddr(addr): 207 def parseaddr(addr):
240 addrs = _AddressList(addr).addresslist 208 addrs = _AddressList(addr).addresslist
241 if not addrs: 209 if not addrs:
242 return '', '' 210 return '', ''
243 return addrs[0] 211 return addrs[0]
244 212
245 213
246 # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3. 214 # rfc822.unquote() doesn't properly de-backslash-ify in Python pre-2.3.
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 # While value comes to us as a unicode string, we need it to be a bytes 313 # While value comes to us as a unicode string, we need it to be a bytes
346 # object. We do not want bytes() normal utf-8 decoder, we want a straight 314 # object. We do not want bytes() normal utf-8 decoder, we want a straight
347 # interpretation of the string as character bytes. 315 # interpretation of the string as character bytes.
348 charset, language, text = value 316 charset, language, text = value
349 rawbytes = bytes(text, 'raw-unicode-escape') 317 rawbytes = bytes(text, 'raw-unicode-escape')
350 try: 318 try:
351 return str(rawbytes, charset, errors) 319 return str(rawbytes, charset, errors)
352 except LookupError: 320 except LookupError:
353 # charset is not a known codec. 321 # charset is not a known codec.
354 return unquote(text) 322 return unquote(text)
OLDNEW
« no previous file with comments | « Lib/email/_parseaddr.py ('k') | Lib/filecmp.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+