Skip to content

Commit

Permalink
Add check_isp command
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrgredowski committed Aug 11, 2023
1 parent c61256a commit 5ff9271
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 14 deletions.
13 changes: 7 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ repos:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
hooks:
- id: mypy
args: [--ignore-missing-imports, --follow-imports=skip]

# FIXME: Tests are not working for now
# # FIXME: enable when mypy is fixed
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v1.4.1
# hooks:
# - id: mypy
# args: [--ignore-missing-imports, --follow-imports=skip]

# - repo: local
# hooks:
# - id: unittests
Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cpython@3.11.3
3.11.3
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"liveServer.settings.port": 5501
"liveServer.settings.port": 5501,
"cSpell.words": [
"typer"
]
}
File renamed without changes.
60 changes: 60 additions & 0 deletions bucket_of_utils/check_isp/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Module for checking if the current internet service provider is the one you specify.
Usage:
```bash
python -m bucket_of_utils.check_isp --part-of-name "Orange" --exclude-in-name "Mobile"
```
"""
from http import HTTPStatus
from typing import Optional
from typing import TypedDict

import requests


class ResponseData(TypedDict):
org: str


def check_isp(*, include_in_name: Optional[str] = None, exclude_in_name: Optional[str] = None):
if all((include_in_name is None, exclude_in_name is None)):
msg = "You must specify at least one of the parameters: include_in_name, exclude_in_name"
raise ValueError(msg)

check_ip_url = "https://ipinfo.io"

response = requests.get(check_ip_url)

if response.status_code != HTTPStatus.OK:
msg = f"Error: {response.status_code}"
raise RuntimeError(msg)

is_it = False

include_in_name = include_in_name.lower() if include_in_name is not None else None
exclude_in_name = exclude_in_name.lower() if exclude_in_name is not None else None

data: ResponseData = response.json()
org = data["org"].lower()

if include_in_name is not None and include_in_name in org:
is_it = True

if exclude_in_name is not None and exclude_in_name in org:
is_it = False
return is_it


def main():
def _run(*, include_in_name: Optional[str] = None, exclude_in_name: Optional[str] = None):
is_it = check_isp(include_in_name=include_in_name, exclude_in_name=exclude_in_name)
print(is_it)

import typer

print(typer.run(_run))


if __name__ == "__main__":
main()
File renamed without changes.
17 changes: 14 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@ description = "Bunch of utils to be hosted on GH pages using `pyscript`."
authors = [{ name = "Piotr Grędowski", email = "[email protected]" }]
dependencies = [
"pdf-annotate>=0.12.0",
"requests>=2.31.0",
"typer>=0.9.0",
"ruff>=0.0.284",
"rich>=13.5.2",
"shellingham>=1.5.0.post1",
"pyodide-py>=0.23.4",
"types-requests>=2.31.0.2",
]
readme = "README.md"
requires-python = ">= 3.11"

[project.scripts]
check_isp = "bucket_of_utils.check_isp.main:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand All @@ -22,6 +32,8 @@ dev-dependencies = [
"ruff>=0.0.282",
"pre-commit>=3.3.3",
"mypy>=1.4.1",
"pytest-mock>=3.11.1",
"types-requests>=2.31.0.2",
]

[tool.hatch.metadata]
Expand Down Expand Up @@ -95,13 +107,12 @@ select = [
"RUF", # Ruff-specific rules
]

extend-ignore = ["F841"]
extend-ignore = ["F841", "UP007"]

[tool.ruff.extend-per-file-ignores]
"tests/**/*.py" = ["PLR2004"]
"tests/**/*.py" = ["PLR2004", "PT011"]

[tool.ruff.isort]
known-first-party = ["src"]
force-single-line = true

[tool.mypy]
Expand Down
17 changes: 16 additions & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
-e file:.
attrs==23.1.0
black==23.7.0
certifi==2023.7.22
cfgv==3.3.1
charset-normalizer==3.2.0
click==8.1.6
coverage==7.2.7
distlib==0.3.7
filelock==3.12.2
fonttools==4.42.0
identify==2.5.26
idna==3.4
iniconfig==2.0.0
markdown-it-py==3.0.0
mdurl==0.1.2
mypy==1.4.1
mypy-extensions==1.0.0
nodeenv==1.8.0
Expand All @@ -28,11 +33,21 @@ pillow==10.0.0
platformdirs==3.10.0
pluggy==1.2.0
pre-commit==3.3.3
pygments==2.16.1
pyodide-py==0.23.4
pytest==7.4.0
pytest-cov==4.1.0
pytest-mock==3.11.1
pyyaml==6.0.1
ruff==0.0.282
requests==2.31.0
rich==13.5.2
ruff==0.0.284
shellingham==1.5.0.post1
typer==0.9.0
types-requests==2.31.0.2
types-urllib3==1.26.25.14
typing-extensions==4.7.1
urllib3==2.0.4
virtualenv==20.24.2
# The following packages are considered to be unsafe in a requirements file:
setuptools==68.0.0
17 changes: 17 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,24 @@

-e file:.
attrs==23.1.0
certifi==2023.7.22
charset-normalizer==3.2.0
click==8.1.6
fonttools==4.42.0
idna==3.4
markdown-it-py==3.0.0
mdurl==0.1.2
pdf-annotate==0.12.0
pdfrw==0.4
pillow==10.0.0
pygments==2.16.1
pyodide-py==0.23.4
requests==2.31.0
rich==13.5.2
ruff==0.0.284
shellingham==1.5.0.post1
typer==0.9.0
types-requests==2.31.0.2
types-urllib3==1.26.25.14
typing-extensions==4.7.1
urllib3==2.0.4
4 changes: 2 additions & 2 deletions tests/pdf/test_add_title_to_first_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pdf_annotate as pa

from src.bucket_of_utils.pdf.add_title_to_first_page import AnnotationConfig
from src.bucket_of_utils.pdf.add_title_to_first_page import add_file_name_annotation_to_pdf
from bucket_of_utils.pdf.add_title_to_first_page import AnnotationConfig
from bucket_of_utils.pdf.add_title_to_first_page import add_file_name_annotation_to_pdf


def test_add_file_name_annotation_to_pdf():
Expand Down
41 changes: 41 additions & 0 deletions tests/test_check_isp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from http import HTTPStatus
from unittest.mock import MagicMock

import pytest

from bucket_of_utils.check_isp.main import check_isp


def mock_response(org_value: str):
class MockResponse(MagicMock):
@property
def status_code(self):
return HTTPStatus.OK

def json(self):
return {"org": org_value}

return MockResponse()


def test_check_isp(mocker):
mocker.patch("requests.get", return_value=mock_response("AS5617 Orange Polska Spolka Akcyjna"))
is_it = check_isp(include_in_name="Orange")
assert is_it is True


def test_check_isp_returns_false_when_include_in_name_is_not_in_org(mocker):
mocker.patch("requests.get", return_value=mock_response("Orange Mobile Polska Spolka Akcyjna"))
is_it = check_isp(include_in_name="Orange", exclude_in_name="Mobile")
assert is_it is False


def test_check_isp_returns_false_when_exclude_in_name_is_in_org(mocker):
mocker.patch("requests.get", return_value=mock_response("AS5617 Orange Polska Spolka Akcyjna"))
is_it = check_isp(exclude_in_name="Orange")
assert is_it is False


def test_check_isp_raises_value_error_when_both_parameters_are_none():
with pytest.raises(ValueError):
check_isp()

0 comments on commit 5ff9271

Please sign in to comment.