Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve code quality using pylint & isort #29

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
./test
./.git
./.gitignore
./.gitignore
1 change: 1 addition & 0 deletions .github/workflows/before-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ jobs:
python -m pip install --upgrade pip
pip install pipenv
pipenv sync --system --dev
- run: pylint ./src ./test
- run: mypy ./src ./test
- run: pytest -v --cov=src.use_case --cov-report term-missing --cov-report term:skip-covered test
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ debugpy = "*"
pytest = "*"
pytest-cov = "*"
mypy = "*"
pylint = "*"
isort = "*"

[requires]
python_version = "3.12"
124 changes: 91 additions & 33 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,19 @@ disallow_untyped_defs = true
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true

[tool.pylint.imports]
max-line-length = 120
disable = [
"missing-module-docstring",
"missing-class-docstring",
"missing-function-docstring",
"too-few-public-methods",
"too-many-arguments",
"duplicate-code"
]
ignore = ["wireup.py", "settings.py"]

[tool.isort]
line_length = 90 # Specify your desired max line length
profile = "black" # Optional: if you're using Black's formatting style
7 changes: 6 additions & 1 deletion src/abc/infra/inode_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from abc import ABC, abstractmethod

from src.type.internal import EndPoint, NodeIdentifier, PeerCredentials, UniversalPeerIdentifier
from src.type.entity import Message, Node, Peer
from src.type.internal import (
EndPoint,
NodeIdentifier,
PeerCredentials,
UniversalPeerIdentifier,
)


class INodeClient(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/abc/infra/ipeer_repo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod

from src.type.entity import Peer
from src.type.internal import PeerIdentifier, Keyring
from src.type.internal import Keyring, PeerIdentifier


class IPeerRepo(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/abc/use_case/add_peer_use_case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from abc import ABC, abstractmethod

from src.type.internal import PeerIdentifier, Keyring
from src.type.internal import Keyring, PeerIdentifier


class AddPeerUseCase(ABC):
Expand Down
2 changes: 1 addition & 1 deletion src/abc/use_case/get_related_messages_use_case.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod

from src.type.internal import PeerCredentials, UniversalPeerIdentifier
from src.type.entity import Message
from src.type.internal import PeerCredentials, UniversalPeerIdentifier


class GetRelatedMessagesUseCase(ABC):
Expand Down
10 changes: 7 additions & 3 deletions src/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ def __init__(self, app: Application) -> None:
self.app = app

@property
def _find_peer_use_case(self) -> FindPeerUseCase: return self.app.services.resolve(FindPeerUseCase)
def _find_peer_use_case(self) -> FindPeerUseCase:
return self.app.services.resolve(FindPeerUseCase)

@property
def _settings(self) -> AuthenticationSettings: return self.app.services.resolve(AuthenticationSettings)
def _settings(self) -> AuthenticationSettings:
return self.app.services.resolve(AuthenticationSettings)

# pylint: disable=invalid-overridden-method
async def authenticate(self, context: Request) -> Identity | None: # type: ignore
context.identity = None

Expand All @@ -46,8 +49,9 @@ async def authenticate(self, context: Request) -> Identity | None: # type: ignor
now, window_range = datetime.now(), timedelta(seconds=self._settings.TIME_WINDOW)
start, end = now - window_range, now + window_range
given = datetime.fromtimestamp(signed_timestamp)
if given > start and given < end:
if start < given < end:
context.identity = Identity({'id': universal_identifier}, 'authenticated')
# pylint: disable=broad-exception-caught
except Exception:
pass

Expand Down
4 changes: 2 additions & 2 deletions src/api/controller/node_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from blacksheep import FromJSON, FromRoute, FromQuery, Response
from blacksheep import FromJSON, FromQuery, FromRoute, Response
from blacksheep.server.authorization import allow_anonymous
from blacksheep.server.controllers import post, get
from blacksheep.server.controllers import get, post
from blacksheep.server.openapi.common import ContentInfo, ResponseInfo

from src.abc.use_case.connect_node_use_case import ConnectNodeUseCase
Expand Down
2 changes: 1 addition & 1 deletion src/api/controller/peer_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from blacksheep.server.controllers import post

from src.abc.use_case.add_peer_use_case import AddPeerUseCase
from src.api.docs import docs, unsecure_handler
from src.api.controller.base_controller import BaseController
from src.api.docs import docs, unsecure_handler
from src.api.io_type.peer_io import PeerCreationRequest


Expand Down
2 changes: 1 addition & 1 deletion src/api/controller/related_message_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from src.api.controller.base_controller import BaseController
from src.api.docs import docs
from src.api.io_type.message_io import MessageTransferRequest
from src.type.internal import UniversalPeerIdentifier
from src.type.internal import PeerCredentials, UniversalPeerIdentifier


class RelatedMessageController(BaseController):
Expand Down
2 changes: 1 addition & 1 deletion src/api/controller/related_peer_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from src.abc.use_case.add_peer_use_case import AddPeerUseCase
from src.abc.use_case.find_peer_use_case import FindPeerUseCase
from src.api.docs import docs, unsecure_handler
from src.api.controller.base_controller import BaseController
from src.api.docs import docs, unsecure_handler
from src.api.io_type.peer_io import PeerModel
from src.type.internal import UniversalPeerIdentifier

Expand Down
16 changes: 13 additions & 3 deletions src/api/docs.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
from blacksheep.server.openapi.v3 import OpenAPIHandler
from openapidocs.v3 import Info, Response, Operation, OpenAPI, HTTPSecurity, Security, SecurityRequirement
from openapidocs.v3 import (
HTTPSecurity,
Info,
OpenAPI,
Operation,
Response,
Security,
SecurityRequirement,
)


# pylint: disable=unused-argument
def unsecure_handler(self: OpenAPIHandler, operation: Operation) -> None:
operation.security = list()
operation.security = []


class SecuredOpenAPIHandler(OpenAPIHandler):
# pylint: disable=redefined-outer-name
def on_docs_generated(self, docs: OpenAPI) -> None:
docs.components.security_schemes = {'Basic authentication': HTTPSecurity(scheme='Basic')} # type: ignore
docs.security = Security(requirements=[SecurityRequirement(name='Basic authentication', value=list())])
docs.security = Security(requirements=[SecurityRequirement(name='Basic authentication', value=[])])
super().on_docs_generated(docs)

docs = SecuredOpenAPIHandler(info=Info(title='WCYD', version='0'))
Expand Down
7 changes: 5 additions & 2 deletions src/api/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from guardpost.common import AuthenticatedRequirement

from src.api.auth import DecentralizedAuthenticationHandler
from src.api.wireup import inject_dependencies, register_controllers, register_exception_handlers
from src.api.docs import docs

from src.api.wireup import (
inject_dependencies,
register_controllers,
register_exception_handlers,
)

app = Application(show_error_details=False)

Expand Down
1 change: 1 addition & 0 deletions src/api/io_type/message_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from uuid import UUID

from pydantic.dataclasses import dataclass

from src.type.internal import UniversalPeerIdentifier
Expand Down
2 changes: 1 addition & 1 deletion src/api/io_type/peer_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic.dataclasses import dataclass

from src.type.internal import PeerIdentifier, Keyring, UniversalPeerIdentifier
from src.type.internal import Keyring, PeerIdentifier, UniversalPeerIdentifier


@dataclass(kw_only=True)
Expand Down
Loading