Downloading blacklist.txt on install

This commit is contained in:
László Károlyi 2019-03-02 02:17:12 +01:00
parent ce2fa590fd
commit 443f264fd7
Signed by: karolyi
GPG Key ID: 2DCAF25E55735BFE
4 changed files with 42 additions and 22 deletions

View File

@ -3,6 +3,7 @@ include LICENSE
include README.rst
recursive-include validate_email *
prune tests
recursive-exclude tests *
recursive-exclude * __pycache__
recursive-exclude * *.pyc
recursive-exclude * *.pyo

View File

@ -1,30 +1,33 @@
from setuptools import setup
from setuptools.command.develop import develop
from setuptools.command.install import install
from os.path import join
from urllib.request import urlopen
from setuptools import find_packages, setup
from setuptools.command.build_py import build_py
blacklist_url = (
'https://raw.githubusercontent.com/martenson/disposable-email-domains/'
'master/disposable_email_blocklist.conf')
class PostDevelopCommand(develop):
'Post-installation for development mode'
class PostBuildPyCommand(build_py):
'Post-installation for build_py'
def run(self):
if self.dry_run:
return super().run()
with urlopen(url=blacklist_url) as fd:
content = fd.read().decode('utf-8')
target_dir = join(self.build_lib, 'validate_email/lib')
self.mkpath(target_dir)
with open(join(target_dir, 'blacklist.txt'), 'w') as fd:
fd.write(content)
super().run()
with open('/tmp/test-develop', 'w') as fd:
fd.write(str(vars(self)))
class PostInstallCommand(install):
'Post-installation for installation mode'
def run(self):
super().run()
with open('/tmp/test-install', 'w') as fd:
fd.write(str(vars(self)))
setup(
name='py3-validate-email',
version='0.1',
py_modules=('validate_email',),
packages=find_packages(exclude=['tests']),
install_requires=['dnspython'],
author='László Károlyi',
author_email='laszlo@karolyi.hu',
@ -32,6 +35,6 @@ setup(
long_description=open('README.rst').read(),
keywords='email validation verification mx verify',
url='http://github.com/karolyi/py3-validate-email',
cmdclass=dict(develop=PostDevelopCommand, install=PostInstallCommand),
cmdclass=dict(build_py=PostBuildPyCommand),
license='LGPL',
)

View File

@ -1,4 +1,5 @@
from ipaddress import IPv4Address, IPv6Address
from os.path import dirname, join
from re import IGNORECASE
from re import compile as re_compile
from typing import Optional
@ -51,10 +52,24 @@ class EmailValidator(object):
def __init__(
self, whitelist: SetOrNone = None, blacklist: SetOrNone = None):
self.domain_whitelist = whitelist or self.domain_whitelist
self.domain_blacklist = blacklist or self.domain_blacklist
self.domain_whitelist = set(whitelist) \
if whitelist else self.domain_whitelist
self._load_blacklist(blacklist=blacklist)
def __call__(self, value) -> bool:
def _load_blacklist(self, blacklist: SetOrNone = None):
'Load our blacklist.'
self.domain_blacklist = set(blacklist) \
if blacklist else self.domain_blacklist
path = join(dirname(__file__), 'lib', 'blacklist.txt')
try:
with open(path) as fd:
lines = fd.readlines()
except FileNotFoundError:
return
self.domain_blacklist = self.domain_blacklist.union(
x.strip() for x in lines)
def __call__(self, value: str, use_blacklist: bool) -> bool:
if not value or '@' not in value:
return False

View File

@ -3,7 +3,8 @@ from .regex_check import regex_check
def validate_email(
email_address, check_regex=True, check_mx=True, smtp_timeout=10):
email_address: str, check_regex: bool = True, check_mx: bool = True,
smtp_timeout: int = 10, use_blacklist: bool = True) -> bool:
if check_regex and not regex_check(email_address):
return False