-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c61256a
commit 5ff9271
Showing
13 changed files
with
162 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
cpython@3.11.3 | ||
3.11.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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] | ||
|
@@ -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] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |