3
3
import sys
4
4
import re
5
5
import unicodedata
6
- import dns .resolver , dns .exception
7
- import idna # implements IDNA 2008; Python's codec is only IDNA 2003
6
+ import dns .resolver
7
+ import dns .exception
8
+ import idna # implements IDNA 2008; Python's codec is only IDNA 2003
8
9
9
10
10
11
# Based on RFC 2822 section 3.2.4 / RFC 5322 section 3.2.3, these
32
33
if sys .version_info >= (3 ,):
33
34
unicode_class = str
34
35
else :
35
- unicode_class = unicode
36
+ unicode_class = unicode # noqa: F821
36
37
37
38
# turn regexes to unicode (because 'ur' literals are not allowed in Py3)
38
39
ATEXT = ATEXT .decode ("ascii" )
@@ -62,10 +63,13 @@ def validate_email(
62
63
allow_smtputf8 = True ,
63
64
allow_empty_local = False ,
64
65
check_deliverability = True ,
65
- timeout = DEFAULT_TIMEOUT ):
66
-
67
- """Validates an email address, raising an EmailNotValidError if the address is not valid or returning a dict of information
68
- when the address is valid. The email argument can be a str or a bytes instance, but if bytes it must be ASCII-only."""
66
+ timeout = DEFAULT_TIMEOUT ,
67
+ ):
68
+ """
69
+ Validates an email address, raising an EmailNotValidError if the address is not valid or returning a dict of
70
+ information when the address is valid. The email argument can be a str or a bytes instance,
71
+ but if bytes it must be ASCII-only.
72
+ """
69
73
70
74
# Allow email to be a str or bytes instance. If bytes,
71
75
# it must be ASCII because that's how the bytes work
@@ -82,7 +86,7 @@ def validate_email(
82
86
raise EmailSyntaxError ("The email address is not valid. It must have exactly one @-sign." )
83
87
84
88
# Prepare a dict to return on success.
85
- ret = { }
89
+ ret = {}
86
90
87
91
# Validate the email address's local part syntax and update the return
88
92
# dict with metadata.
@@ -137,7 +141,9 @@ def validate_email_local_part(local, allow_smtputf8=True, allow_empty_local=Fals
137
141
m = re .match (DOT_ATOM_TEXT_UTF8 + "$" , local )
138
142
if not m :
139
143
# It's not a valid internationalized address either. Report which characters were not valid.
140
- bad_chars = ', ' .join (sorted (set ( c for c in local if not re .match (u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8 ) + u"]" , c ) )))
144
+ bad_chars = ', ' .join (sorted (set (
145
+ c for c in local if not re .match (u"[" + (ATEXT if not allow_smtputf8 else ATEXT_UTF8 ) + u"]" , c )
146
+ )))
141
147
raise EmailSyntaxError ("The email address contains invalid characters before the @-sign: %s." % bad_chars )
142
148
143
149
# It would be valid if internationalized characters were allowed by the caller.
@@ -229,7 +235,9 @@ def validate_email_domain_part(domain):
229
235
if "." not in domain :
230
236
raise EmailSyntaxError ("The domain name %s is not valid. It should have a period." % domain_i18n )
231
237
if not re .search (r"[A-Za-z]$" , domain ):
232
- raise EmailSyntaxError ("The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n )
238
+ raise EmailSyntaxError (
239
+ "The domain name %s is not valid. It is not within a valid top-level domain." % domain_i18n
240
+ )
233
241
234
242
# Return the IDNA ASCII-encoded form of the domain, which is how it
235
243
# would be transmitted on the wire (except when used with SMTPUTF8
@@ -293,7 +301,9 @@ def validate_email_deliverability(domain, domain_i18n, timeout=DEFAULT_TIMEOUT):
293
301
294
302
except Exception as e :
295
303
# Unhandled conditions should not propagate.
296
- raise EmailUndeliverableError ("There was an error while checking if the domain name in the email address is deliverable: " + str (e ))
304
+ raise EmailUndeliverableError (
305
+ "There was an error while checking if the domain name in the email address is deliverable: " + str (e )
306
+ )
297
307
298
308
return {
299
309
"mx" : mtas ,
@@ -302,7 +312,8 @@ def validate_email_deliverability(domain, domain_i18n, timeout=DEFAULT_TIMEOUT):
302
312
303
313
304
314
def main ():
305
- import sys , json
315
+ import sys
316
+ import json
306
317
307
318
if sys .argv [- 1 ] == "--tests" :
308
319
# Pass a file of valid/invalid email addresses.
@@ -311,8 +322,10 @@ def main():
311
322
for line in sys .stdin :
312
323
# Strip newlines and skip blank lines and comments.
313
324
line = line .strip ()
314
- if line == "" or line [0 ] == "#" : continue
315
- if sys .version_info < (3 ,): line = line .decode ("utf8" ) # assume utf8 in input
325
+ if line == "" or line [0 ] == "#" :
326
+ continue
327
+ if sys .version_info < (3 ,):
328
+ line = line .decode ("utf8" ) # assume utf8 in input
316
329
317
330
# Pick up "[valid]"/"[invalid]" lines.
318
331
if line == "[valid]" :
@@ -328,12 +341,12 @@ def main():
328
341
try :
329
342
email = line
330
343
validate_email (email , check_deliverability = False )
331
- if correct_answer == False :
344
+ if correct_answer is False :
332
345
# Should have failed.
333
346
print (email , "was recognized as valid." )
334
347
failed += 1
335
348
except EmailNotValidError as e :
336
- if correct_answer == True :
349
+ if correct_answer is True :
337
350
# Should have passed.
338
351
print (email , e )
339
352
failed += 1
@@ -346,7 +359,8 @@ def main():
346
359
for line in sys .stdin :
347
360
try :
348
361
email = line .strip ()
349
- if sys .version_info < (3 ,): email = email .decode ("utf8" ) # assume utf8 in input
362
+ if sys .version_info < (3 ,):
363
+ email = email .decode ("utf8" ) # assume utf8 in input
350
364
validate_email (email , allow_smtputf8 = allow_smtputf8 )
351
365
except EmailNotValidError as e :
352
366
print (email , e )
@@ -355,17 +369,17 @@ def main():
355
369
email = sys .argv [1 ]
356
370
allow_smtputf8 = True
357
371
check_deliverability = True
358
- if sys .version_info < (3 ,): email = email .decode ("utf8" ) # assume utf8 in input
372
+ if sys .version_info < (3 ,):
373
+ email = email .decode ("utf8" ) # assume utf8 in input
359
374
try :
360
375
result = validate_email (email , allow_smtputf8 = allow_smtputf8 , check_deliverability = check_deliverability )
361
376
print (json .dumps (result , indent = 2 , sort_keys = True , ensure_ascii = False ))
362
377
except EmailNotValidError as e :
363
378
if sys .version_info < (3 ,):
364
- print (unicode (e ).encode ("utf8" ))
379
+ print (unicode_class (e ).encode ("utf8" ))
365
380
else :
366
381
print (e )
367
382
368
383
369
384
if __name__ == "__main__" :
370
385
main ()
371
-
0 commit comments