Mercurial > public > mercurial-scm > hg
comparison mercurial/mail.py @ 45942:89a2afe31e82
formating: upgrade to black 20.8b1
This required a couple of small tweaks to un-confuse black, but now it
works. Big formatting changes come from:
* Dramatically improved collection-splitting logic upstream
* Black having a strong (correct IMO) opinion that """ is better than '''
Differential Revision: https://phab.mercurial-scm.org/D9430
author | Augie Fackler <raf@durin42.com> |
---|---|
date | Fri, 27 Nov 2020 17:03:29 -0500 |
parents | 87e7dd8e7734 |
children | b0253377e170 |
comparison
equal
deleted
inserted
replaced
45941:346af7687c6f | 45942:89a2afe31e82 |
---|---|
42 # keep pyflakes happy | 42 # keep pyflakes happy |
43 assert all((Any, List, Tuple, Union)) | 43 assert all((Any, List, Tuple, Union)) |
44 | 44 |
45 | 45 |
46 class STARTTLS(smtplib.SMTP): | 46 class STARTTLS(smtplib.SMTP): |
47 '''Derived class to verify the peer certificate for STARTTLS. | 47 """Derived class to verify the peer certificate for STARTTLS. |
48 | 48 |
49 This class allows to pass any keyword arguments to SSL socket creation. | 49 This class allows to pass any keyword arguments to SSL socket creation. |
50 ''' | 50 """ |
51 | 51 |
52 def __init__(self, ui, host=None, **kwargs): | 52 def __init__(self, ui, host=None, **kwargs): |
53 smtplib.SMTP.__init__(self, **kwargs) | 53 smtplib.SMTP.__init__(self, **kwargs) |
54 self._ui = ui | 54 self._ui = ui |
55 self._host = host | 55 self._host = host |
74 self.does_esmtp = 0 | 74 self.does_esmtp = 0 |
75 return (resp, reply) | 75 return (resp, reply) |
76 | 76 |
77 | 77 |
78 class SMTPS(smtplib.SMTP): | 78 class SMTPS(smtplib.SMTP): |
79 '''Derived class to verify the peer certificate for SMTPS. | 79 """Derived class to verify the peer certificate for SMTPS. |
80 | 80 |
81 This class allows to pass any keyword arguments to SSL socket creation. | 81 This class allows to pass any keyword arguments to SSL socket creation. |
82 ''' | 82 """ |
83 | 83 |
84 def __init__(self, ui, keyfile=None, certfile=None, host=None, **kwargs): | 84 def __init__(self, ui, keyfile=None, certfile=None, host=None, **kwargs): |
85 self.keyfile = keyfile | 85 self.keyfile = keyfile |
86 self.certfile = certfile | 86 self.certfile = certfile |
87 smtplib.SMTP.__init__(self, **kwargs) | 87 smtplib.SMTP.__init__(self, **kwargs) |
219 fp.write(b'\n\n') | 219 fp.write(b'\n\n') |
220 fp.close() | 220 fp.close() |
221 | 221 |
222 | 222 |
223 def connect(ui, mbox=None): | 223 def connect(ui, mbox=None): |
224 '''make a mail connection. return a function to send mail. | 224 """make a mail connection. return a function to send mail. |
225 call as sendmail(sender, list-of-recipients, msg).''' | 225 call as sendmail(sender, list-of-recipients, msg).""" |
226 if mbox: | 226 if mbox: |
227 open(mbox, b'wb').close() | 227 open(mbox, b'wb').close() |
228 return lambda s, r, m: _mbox(mbox, s, r, m) | 228 return lambda s, r, m: _mbox(mbox, s, r, m) |
229 if ui.config(b'email', b'method') == b'smtp': | 229 if ui.config(b'email', b'method') == b'smtp': |
230 return _smtp(ui) | 230 return _smtp(ui) |
265 return cs | 265 return cs |
266 | 266 |
267 | 267 |
268 def mimetextpatch(s, subtype='plain', display=False): | 268 def mimetextpatch(s, subtype='plain', display=False): |
269 # type: (bytes, str, bool) -> email.message.Message | 269 # type: (bytes, str, bool) -> email.message.Message |
270 '''Return MIME message suitable for a patch. | 270 """Return MIME message suitable for a patch. |
271 Charset will be detected by first trying to decode as us-ascii, then utf-8, | 271 Charset will be detected by first trying to decode as us-ascii, then utf-8, |
272 and finally the global encodings. If all those fail, fall back to | 272 and finally the global encodings. If all those fail, fall back to |
273 ISO-8859-1, an encoding with that allows all byte sequences. | 273 ISO-8859-1, an encoding with that allows all byte sequences. |
274 Transfer encodings will be used if necessary.''' | 274 Transfer encodings will be used if necessary.""" |
275 | 275 |
276 cs = [ | 276 cs = [ |
277 'us-ascii', | 277 'us-ascii', |
278 'utf-8', | 278 'utf-8', |
279 pycompat.sysstr(encoding.encoding), | 279 pycompat.sysstr(encoding.encoding), |
291 return mimetextqp(s, subtype, "iso-8859-1") | 291 return mimetextqp(s, subtype, "iso-8859-1") |
292 | 292 |
293 | 293 |
294 def mimetextqp(body, subtype, charset): | 294 def mimetextqp(body, subtype, charset): |
295 # type: (bytes, str, str) -> email.message.Message | 295 # type: (bytes, str, str) -> email.message.Message |
296 '''Return MIME message. | 296 """Return MIME message. |
297 Quoted-printable transfer encoding will be used if necessary. | 297 Quoted-printable transfer encoding will be used if necessary. |
298 ''' | 298 """ |
299 cs = email.charset.Charset(charset) | 299 cs = email.charset.Charset(charset) |
300 msg = email.message.Message() | 300 msg = email.message.Message() |
301 msg.set_type('text/' + subtype) | 301 msg.set_type('text/' + subtype) |
302 | 302 |
303 for line in body.splitlines(): | 303 for line in body.splitlines(): |
335 return [cs for cs in charsets if not cs.endswith('ascii')] | 335 return [cs for cs in charsets if not cs.endswith('ascii')] |
336 | 336 |
337 | 337 |
338 def _encode(ui, s, charsets): | 338 def _encode(ui, s, charsets): |
339 # type: (Any, bytes, List[str]) -> Tuple[bytes, str] | 339 # type: (Any, bytes, List[str]) -> Tuple[bytes, str] |
340 '''Returns (converted) string, charset tuple. | 340 """Returns (converted) string, charset tuple. |
341 Finds out best charset by cycling through sendcharsets in descending | 341 Finds out best charset by cycling through sendcharsets in descending |
342 order. Tries both encoding and fallbackencoding for input. Only as | 342 order. Tries both encoding and fallbackencoding for input. Only as |
343 last resort send as is in fake ascii. | 343 last resort send as is in fake ascii. |
344 Caveat: Do not use for mail parts containing patches!''' | 344 Caveat: Do not use for mail parts containing patches!""" |
345 sendcharsets = charsets or _charsets(ui) | 345 sendcharsets = charsets or _charsets(ui) |
346 if not isinstance(s, bytes): | 346 if not isinstance(s, bytes): |
347 # We have unicode data, which we need to try and encode to | 347 # We have unicode data, which we need to try and encode to |
348 # some reasonable-ish encoding. Try the encodings the user | 348 # some reasonable-ish encoding. Try the encodings the user |
349 # wants, and fall back to garbage-in-ascii. | 349 # wants, and fall back to garbage-in-ascii. |
425 return _addressencode(ui, name, addr, charsets) | 425 return _addressencode(ui, name, addr, charsets) |
426 | 426 |
427 | 427 |
428 def addrlistencode(ui, addrs, charsets=None, display=False): | 428 def addrlistencode(ui, addrs, charsets=None, display=False): |
429 # type: (Any, List[bytes], List[str], bool) -> List[str] | 429 # type: (Any, List[bytes], List[str], bool) -> List[str] |
430 '''Turns a list of addresses into a list of RFC-2047 compliant headers. | 430 """Turns a list of addresses into a list of RFC-2047 compliant headers. |
431 A single element of input list may contain multiple addresses, but output | 431 A single element of input list may contain multiple addresses, but output |
432 always has one address per item''' | 432 always has one address per item""" |
433 straddrs = [] | 433 straddrs = [] |
434 for a in addrs: | 434 for a in addrs: |
435 assert isinstance(a, bytes), '%r unexpectedly not a bytestr' % a | 435 assert isinstance(a, bytes), '%r unexpectedly not a bytestr' % a |
436 straddrs.append(encoding.strfromlocal(a)) | 436 straddrs.append(encoding.strfromlocal(a)) |
437 if display: | 437 if display: |
445 return result | 445 return result |
446 | 446 |
447 | 447 |
448 def mimeencode(ui, s, charsets=None, display=False): | 448 def mimeencode(ui, s, charsets=None, display=False): |
449 # type: (Any, bytes, List[str], bool) -> email.message.Message | 449 # type: (Any, bytes, List[str], bool) -> email.message.Message |
450 '''creates mime text object, encodes it if needed, and sets | 450 """creates mime text object, encodes it if needed, and sets |
451 charset and transfer-encoding accordingly.''' | 451 charset and transfer-encoding accordingly.""" |
452 cs = 'us-ascii' | 452 cs = 'us-ascii' |
453 if not display: | 453 if not display: |
454 s, cs = _encode(ui, s, charsets) | 454 s, cs = _encode(ui, s, charsets) |
455 return mimetextqp(s, 'plain', cs) | 455 return mimetextqp(s, 'plain', cs) |
456 | 456 |