diff --git a/MANIFEST.in b/MANIFEST.in index 67ba3f8..4b9da9a 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -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 diff --git a/setup.py b/setup.py index 0d91f15..9c73a66 100644 --- a/setup.py +++ b/setup.py @@ -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', ) diff --git a/validate_email/regex_check.py b/validate_email/regex_check.py index 48648e6..a3d2154 100644 --- a/validate_email/regex_check.py +++ b/validate_email/regex_check.py @@ -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 diff --git a/validate_email/validate_email.py b/validate_email/validate_email.py index a76e88b..c17f6ca 100644 --- a/validate_email/validate_email.py +++ b/validate_email/validate_email.py @@ -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