diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..3d9d15d --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,28 @@ +# Example configuration for Black. + +# NOTE: you have to use single-quoted strings in TOML for regular expressions. +# It's the equivalent of r-strings in Python. Multiline strings are treated as +# verbose regular expressions by Black. Use [ ] to denote a significant space +# character. + +[tool.black] +line-length = 79 +target-version = ['py36', 'py37', 'py38', 'py39'] +include = '\.pyi?$' +extend-exclude = ''' +/( + # The following are specific to Black, you probably don't want those. + | blib2to3 + | tests/data + | profiling + | migrations +)/ +''' + + +# Build system information below. +# NOTE: You don't need this in your own Black configuration. + +[build-system] +requires = ["setuptools>=41.0", "setuptools-scm", "wheel"] +build-backend = "setuptools.build_meta" diff --git a/src/tld/base.py b/src/tld/base.py index 757459d..d39fea7 100644 --- a/src/tld/base.py +++ b/src/tld/base.py @@ -1,18 +1,59 @@ from codecs import open as codecs_open from urllib.request import urlopen -from typing import Optional +from typing import Optional, Dict, Union, ItemsView from .exceptions import ( TldIOError, TldImproperlyConfigured, ) from .helpers import project_dir -from .registry import Registry -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' -__all__ = ('BaseTLDSourceParser',) +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" +__all__ = ( + "BaseTLDSourceParser", + "Registry", +) + + +class Registry(type): + + REGISTRY: Dict[str, "BaseTLDSourceParser"] = {} + + def __new__(mcs, name, bases, attrs): + new_cls = type.__new__(mcs, name, bases, attrs) + # Here the name of the class is used as key but it could be any class + # parameter. + if getattr(new_cls, "_uid", None): + mcs.REGISTRY[new_cls._uid] = new_cls + return new_cls + + @property + def _uid(cls) -> str: + return getattr(cls, "uid", cls.__name__) + + @classmethod + def reset(mcs) -> None: + mcs.REGISTRY = {} + + @classmethod + def get( + mcs, key: str, default: "BaseTLDSourceParser" = None + ) -> Union["BaseTLDSourceParser", None]: + return mcs.REGISTRY.get(key, default) + + @classmethod + def items(mcs) -> ItemsView[str, "BaseTLDSourceParser"]: + return mcs.REGISTRY.items() + + # @classmethod + # def get_registry(mcs) -> Dict[str, Type]: + # return dict(mcs.REGISTRY) + # + # @classmethod + # def pop(mcs, uid) -> None: + # mcs.REGISTRY.pop(uid) class BaseTLDSourceParser(metaclass=Registry): @@ -54,11 +95,9 @@ def update_tld_names(cls, fail_silently: bool = False) -> bool: try: remote_file = urlopen(cls.source_url) local_file = codecs_open( - project_dir(cls.local_path), - 'wb', - encoding='utf8' + project_dir(cls.local_path), "wb", encoding="utf8" ) - local_file.write(remote_file.read().decode('utf8')) + local_file.write(remote_file.read().decode("utf8")) local_file.close() remote_file.close() except Exception as err: diff --git a/src/tld/exceptions.py b/src/tld/exceptions.py index b8a24d1..d6bfd50 100644 --- a/src/tld/exceptions.py +++ b/src/tld/exceptions.py @@ -1,11 +1,11 @@ -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" __all__ = ( - 'TldBadUrl', - 'TldDomainNotFound', - 'TldImproperlyConfigured', - 'TldIOError', + "TldBadUrl", + "TldDomainNotFound", + "TldImproperlyConfigured", + "TldIOError", ) diff --git a/src/tld/helpers.py b/src/tld/helpers.py index 6c1ad23..0e2ba2b 100644 --- a/src/tld/helpers.py +++ b/src/tld/helpers.py @@ -2,21 +2,19 @@ from .conf import get_setting -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" __all__ = ( - 'project_dir', - 'PROJECT_DIR', + "project_dir", + "PROJECT_DIR", ) def project_dir(base: str) -> str: """Project dir.""" - tld_names_local_path_parent = get_setting('NAMES_LOCAL_PATH_PARENT') - return abspath( - join(tld_names_local_path_parent, base).replace('\\', '/') - ) + tld_names_local_path_parent = get_setting("NAMES_LOCAL_PATH_PARENT") + return abspath(join(tld_names_local_path_parent, base).replace("\\", "/")) PROJECT_DIR = project_dir diff --git a/src/tld/registry.py b/src/tld/registry.py index b1592a2..26db23f 100644 --- a/src/tld/registry.py +++ b/src/tld/registry.py @@ -1,45 +1,12 @@ -from typing import Dict +import warnings +from .base import Registry -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' -__all__ = ( - 'Registry', -) - - -class Registry(type): - - REGISTRY = {} # type: Dict[str, Registry] - - def __new__(mcs, name, bases, attrs): - new_cls = type.__new__(mcs, name, bases, attrs) - # Here the name of the class is used as key but it could be any class - # parameter. - if getattr(new_cls, '_uid', None): - mcs.REGISTRY[new_cls._uid] = new_cls - return new_cls +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" +__all__ = ("Registry",) - @property - def _uid(cls) -> str: - return getattr(cls, 'uid', cls.__name__) - - @classmethod - def reset(mcs) -> None: - mcs.REGISTRY = {} - - @classmethod - def get(mcs, key, default=None): - return mcs.REGISTRY.get(key, default) - - @classmethod - def items(mcs): - return mcs.REGISTRY.items() - - # @classmethod - # def get_registry(mcs) -> Dict[str, Type]: - # return dict(mcs.REGISTRY) - # - # @classmethod - # def pop(mcs, uid) -> None: - # mcs.REGISTRY.pop(uid) +warnings.warn( + "The `Registry` class is moved from `tld.registry` to `tld.base`.", + DeprecationWarning, +) diff --git a/src/tld/result.py b/src/tld/result.py index 2275871..9d9799f 100644 --- a/src/tld/result.py +++ b/src/tld/result.py @@ -1,26 +1,22 @@ from typing import Any, Dict from urllib.parse import SplitResult -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' -__all__ = ( - 'Result', -) +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" +__all__ = ("Result",) class Result(object): """Container.""" - __slots__ = ('subdomain', 'domain', 'tld', '__fld', 'parsed_url') + __slots__ = ("subdomain", "domain", "tld", "__fld", "parsed_url") - def __init__(self, - tld: str, - domain: str, - subdomain: str, - parsed_url: SplitResult): + def __init__( + self, tld: str, domain: str, subdomain: str, parsed_url: SplitResult + ): self.tld = tld - self.domain = domain if domain != '' else tld + self.domain = domain if domain != "" else tld self.subdomain = subdomain self.parsed_url = parsed_url @@ -36,6 +32,7 @@ def extension(self) -> str: :return str: """ return self.tld + suffix = extension @property @@ -49,6 +46,7 @@ def fld(self) -> str: def __str__(self) -> str: return self.tld + __repr__ = __str__ @property @@ -59,9 +57,9 @@ def __dict__(self) -> Dict[str, Any]: # type: ignore :rtype: dict """ return { - 'tld': self.tld, - 'domain': self.domain, - 'subdomain': self.subdomain, - 'fld': self.fld, - 'parsed_url': self.parsed_url, + "tld": self.tld, + "domain": self.domain, + "subdomain": self.subdomain, + "fld": self.fld, + "parsed_url": self.parsed_url, } diff --git a/src/tld/trie.py b/src/tld/trie.py index 105f50b..c977a06 100644 --- a/src/tld/trie.py +++ b/src/tld/trie.py @@ -1,16 +1,16 @@ -__author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' -__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' +__author__ = "Artur Barseghyan" +__copyright__ = "2013-2021 Artur Barseghyan" +__license__ = "MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later" __all__ = ( - 'Trie', - 'TrieNode', + "Trie", + "TrieNode", ) class TrieNode(object): """Class representing a single Trie node.""" - __slots__ = ('children', 'exception', 'leaf', 'private') + __slots__ = ("children", "exception", "leaf", "private") def __init__(self): self.children = None @@ -34,11 +34,11 @@ def add(self, tld: str, private: bool = False) -> None: # Iterating over the tld parts in reverse order # for part in reversed(tld.split('.')): - tld_split = tld.split('.') + tld_split = tld.split(".") tld_split.reverse() for part in tld_split: - if part.startswith('!'): + if part.startswith("!"): node.exception = part[1:] break diff --git a/src/tld/utils.py b/src/tld/utils.py index 972336d..625ab04 100644 --- a/src/tld/utils.py +++ b/src/tld/utils.py @@ -8,7 +8,7 @@ from typing import Dict, Type, Union, Tuple, List, Optional from urllib.parse import urlsplit, SplitResult -from .base import BaseTLDSourceParser +from .base import BaseTLDSourceParser, Registry from .exceptions import ( TldBadUrl, TldDomainNotFound, @@ -17,11 +17,10 @@ ) from .helpers import project_dir from .trie import Trie -from .registry import Registry from .result import Result __author__ = 'Artur Barseghyan' -__copyright__ = '2013-2020 Artur Barseghyan' +__copyright__ = '2013-2021 Artur Barseghyan' __license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later' __all__ = ( 'BaseMozillaTLDSourceParser',