Skip to content

Commit

Permalink
Blackify
Browse files Browse the repository at this point in the history
  • Loading branch information
barseghyanartur committed Jun 4, 2021
1 parent ae61f8c commit 3119896
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 98 deletions.
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
59 changes: 49 additions & 10 deletions src/tld/base.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions src/tld/exceptions.py
Original file line number Diff line number Diff line change
@@ -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",
)


Expand Down
16 changes: 7 additions & 9 deletions src/tld/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 10 additions & 43 deletions src/tld/registry.py
Original file line number Diff line number Diff line change
@@ -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,
)
34 changes: 16 additions & 18 deletions src/tld/result.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -36,6 +32,7 @@ def extension(self) -> str:
:return str:
"""
return self.tld

suffix = extension

@property
Expand All @@ -49,6 +46,7 @@ def fld(self) -> str:

def __str__(self) -> str:
return self.tld

__repr__ = __str__

@property
Expand All @@ -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,
}
16 changes: 8 additions & 8 deletions src/tld/trie.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions src/tld/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down

0 comments on commit 3119896

Please sign in to comment.