Skip to content

Commit

Permalink
Merge pull request #47 from ninoseki/update
Browse files Browse the repository at this point in the history
feat: update Pydantic and other deps
  • Loading branch information
ninoseki committed Jul 7, 2023
2 parents 493c4e7 + 631e195 commit 351b5f2
Show file tree
Hide file tree
Showing 16 changed files with 773 additions and 831 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Poetry
uses: abatilo/actions-poetry@v2
with:
poetry-version: 1.3.2
poetry-version: 1.5.1

- name: Install plugin
run: poetry self add "poetry-dynamic-versioning[plugin]"
Expand Down
19 changes: 9 additions & 10 deletions .github/workflows/test.yml
Expand Up @@ -9,26 +9,25 @@ jobs:
strategy:
matrix:
python-version: ["3.10", 3.11]
poetry-version: [1.3.2]
poetry-version: [1.5.1]

steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Run image
uses: abatilo/actions-poetry@v2
- uses: abatilo/actions-poetry@v2
with:
poetry-version: ${{ matrix.poetry-version }}

- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: "poetry"

- name: Install whois
run: |
sudo apt-get -yqq install whois
run: sudo apt-get -yqq install whois

- name: Install Python dependencies
run: |
poetry install
run: poetry install

- name: Run tests
run: poetry run pytest -v --cov=abuse_whois --cov-report=term-missing
Expand Down
25 changes: 14 additions & 11 deletions .pre-commit-config.yaml
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pycqa/autoflake
rev: v2.0.0
rev: v2.2.0
hooks:
- id: autoflake
args:
Expand All @@ -10,28 +10,31 @@ repos:
"--remove-unused-variable",
]

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.1
hooks:
- id: pyupgrade
args: [--py310-plus]

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies: [flake8-print]
args: ["--ignore=E501,W503,E203"]

- repo: https://github.com/asottile/pyupgrade
rev: v3.8.0
hooks:
- id: pyupgrade
args: [--py310-plus]

- repo: https://github.com/timothycrosley/isort
rev: 5.11.4
rev: 5.12.0
hooks:
- id: isort
additional_dependencies: [toml]
exclude: ^.*/?setup\.py$
args: ["--profile", "black"]

- repo: https://github.com/psf/black
rev: 22.12.0
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/andrei-shabanski/poetry-plugin-sort
rev: v0.2.0
hooks:
- id: poetry-sort
2 changes: 1 addition & 1 deletion abuse_whois/matchers/shared_hosting/rules.py
Expand Up @@ -15,4 +15,4 @@ def load_rules() -> list[SharedHostingRule]:
DEFAULT_RULE_DIRECTORY,
additional_directories=settings.ADDITIONAL_SHARED_HOSTING_RULE_DIRECTORIES,
)
return [SharedHostingRule.parse_obj(load_yaml(path)) for path in paths]
return [SharedHostingRule.model_validate(load_yaml(path)) for path in paths]
2 changes: 1 addition & 1 deletion abuse_whois/matchers/whois/rule.py
Expand Up @@ -10,7 +10,7 @@ async def match(self, hostname: str) -> bool:

try:
whois_record = await get_whois_record(hostname)
data = whois_record.dict(by_alias=True)
data = whois_record.model_dump(by_alias=True)
except Exception:
data = {"domain": hostname}

Expand Down
2 changes: 1 addition & 1 deletion abuse_whois/matchers/whois/rules.py
Expand Up @@ -15,4 +15,4 @@ def load_rules() -> list[WhoisRule]:
DEFAULT_RULE_DIRECTORY,
additional_directories=settings.ADDITIONAL_WHOIS_RULE_DIRECTORIES,
)
return [WhoisRule.parse_obj(load_yaml(path)) for path in paths]
return [WhoisRule.model_validate(load_yaml(path)) for path in paths]
9 changes: 4 additions & 5 deletions abuse_whois/schemas/api_model.py
@@ -1,9 +1,8 @@
from humps import camelize
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


class APIModel(BaseModel):
class Config:
orm_mode = True
alias_generator = camelize
allow_population_by_field_name = True
model_config = ConfigDict(
from_attributes=True, alias_generator=camelize, populate_by_name=True
)
10 changes: 2 additions & 8 deletions abuse_whois/schemas/contact.py
@@ -1,14 +1,8 @@
from typing import Any

from abuse_whois.schemas.whois import WhoisRecord

try:
from typing import Literal
except ImportError:
from typing import Literal
from typing import Any, Literal

from pydantic import Field

from abuse_whois.schemas.whois import WhoisRecord
from abuse_whois.utils import is_email, is_url

from .api_model import APIModel
Expand Down
2 changes: 1 addition & 1 deletion abuse_whois/settings.py
Expand Up @@ -38,7 +38,7 @@
"RULE_EXTENSIONS", cast=CommaSeparatedStrings, default="yaml,yml"
)

ADDITIONAL_WHOIS_RULE_DIRECTORIES = config(
ADDITIONAL_WHOIS_RULE_DIRECTORIES: CommaSeparatedStrings = config(
"ADDITIONAL_WHOIS_RULE_DIRECTORIES", cast=CommaSeparatedStrings, default=""
)

Expand Down
41 changes: 15 additions & 26 deletions abuse_whois/utils.py
@@ -1,27 +1,28 @@
import pathlib
from collections.abc import Callable
from functools import lru_cache
from typing import cast
from urllib.parse import urlparse

import tldextract
import validators
import yaml
from email_validator import EmailNotValidError, validate_email
from pydantic import BaseModel, ValidationError
from pydantic.networks import AnyHttpUrl
from starlette.datastructures import CommaSeparatedStrings

from abuse_whois import settings


class URLModel(BaseModel):
url: AnyHttpUrl
def _is_x(v: str, *, validator: Callable[[str], bool]) -> bool:
res = validator(v)

if isinstance(res, validators.ValidationFailure):
return False

return res


def is_ip_address(v: str) -> bool:
try:
model = URLModel(url=f"http://{v}")
return model.url.host_type in ["ipv4", "ipv6"]
except ValidationError:
return False
return _is_x(v, validator=validators.ipv4) or _is_x(v, validator=validators.ipv6) # type: ignore


def is_domain(v: str) -> bool:
Expand All @@ -31,30 +32,18 @@ def is_domain(v: str) -> bool:
if "@" in v:
return False

try:
model = URLModel(url=f"http://{v}")
return model.url.host_type in ["domain", "int_domain"]
except ValidationError:
return False
return _is_x(v, validator=validators.domain) # type: ignore


def is_url(v: str) -> bool:
if not v.startswith(("http://", "https://")):
return False

try:
URLModel(url=v)
return True
except ValidationError:
return False
return _is_x(v, validator=validators.url) # type: ignore


def is_email(v: str) -> bool:
try:
validate_email(v, check_deliverability=False)
return True
except EmailNotValidError:
return False
return _is_x(v, validator=validators.email) # type: ignore


def is_supported_address(v: str) -> bool:
Expand Down Expand Up @@ -93,7 +82,7 @@ def load_yaml(path: str | pathlib.Path) -> dict:
def glob_rules(
base_directory: str | pathlib.Path,
*,
additional_directories: list[str | pathlib.Path],
additional_directories: list[str] | list[pathlib.Path] | CommaSeparatedStrings,
rule_extensions=settings.RULE_EXTENSIONS,
) -> list[pathlib.Path]:
directories = [base_directory]
Expand Down
2 changes: 1 addition & 1 deletion abuse_whois/whois.py
Expand Up @@ -28,7 +28,7 @@ def parse(raw_text: str, hostname: str) -> schemas.WhoisRecord:
parser = get_whois_parser()
record = parser.parse(raw_text, hostname=hostname)

return schemas.WhoisRecord.parse_obj(record.to_dict())
return schemas.WhoisRecord.model_validate(record.to_dict())


@cached(
Expand Down

0 comments on commit 351b5f2

Please sign in to comment.