Skip to content

Commit

Permalink
ruff config
Browse files Browse the repository at this point in the history
  • Loading branch information
KrySeyt committed Feb 27, 2024
1 parent 492e793 commit 34aba03
Show file tree
Hide file tree
Showing 64 changed files with 217 additions and 190 deletions.
28 changes: 28 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ disallow_untyped_defs = true

[tool.ruff]
line-length = 120
target-version="py311"
src = ["src"]
include = ["src/**.py", "tests/**.py"]
lint.select = [
"ALL"
]
lint.ignore = [
"ARG",
"ANN",
"D",
"EM101",
"EM102",
"PT001",
"PT023",
"SIM108",
"SIM114",
"TRY003",
"PLW2901",
"RET505",
"PLR0913",
"S101",
]

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["TID252", "PLR2004", "S101"]

[tool.ruff.lint.isort]
no-lines-before = ["local-folder"]

[project]
name = "just_chat"
Expand Down
14 changes: 7 additions & 7 deletions src/just_chat/adapters/database/ram_chat_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from dataclasses import asdict

from just_chat.adapters.database.ram_user_db import RAM_USERS_DB
from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFound
from just_chat.application.common.user_gateway import UserNotFound
from just_chat.domain.models.chat import ChatId, Chat
from just_chat.application.common.chat_gateway import ChatGateway, ChatNotFoundError
from just_chat.application.common.user_gateway import UserNotFoundError
from just_chat.domain.models.chat import Chat, ChatId
from just_chat.domain.models.user import UserId

RAM_CHATS_DB: list[Chat] = []
Expand All @@ -17,7 +17,7 @@ class RAMChatGateway(ChatGateway):
async def save_chat(self, chat: Chat) -> Chat:
async with self.next_chat_id_lock:
chat_in_db = Chat(
**asdict(chat) | {"id": self.next_chat_id}
**asdict(chat) | {"id": self.next_chat_id},
)
type(self).next_chat_id += 1

Expand All @@ -33,7 +33,7 @@ async def create_chat_with_random_user(self, title: str, user_id: UserId) -> Cha

for user in RAM_USERS_DB:
assert user.id is not None

if user.id in excluded_users_ids:
continue

Expand All @@ -48,14 +48,14 @@ async def create_chat_with_random_user(self, title: str, user_id: UserId) -> Cha

return chat

raise UserNotFound
raise UserNotFoundError

async def get_chat_by_id(self, id_: ChatId) -> Chat:
for chat in RAM_CHATS_DB:
if chat.id == id_:
return chat

raise ChatNotFound(f"Chat with id {id_} not found")
raise ChatNotFoundError(f"Chat with id {id_} not found")

async def delete_chat_by_id(self, id_: ChatId) -> Chat:
chat = await self.get_chat_by_id(id_)
Expand Down
13 changes: 4 additions & 9 deletions src/just_chat/adapters/database/ram_message_db.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from dataclasses import asdict

from just_chat.application.common.message_gateway import MessageGateway, MessageNotFound
from just_chat.application.common.message_gateway import MessageGateway, MessageNotFoundError
from just_chat.domain.models.chat import ChatId
from just_chat.domain.models.message import Message, MessageId

Expand All @@ -15,7 +15,7 @@ class RAMMessageGateway(MessageGateway):
async def save_message(self, message: Message) -> Message:
async with self.next_message_id_lock:
message_in_db = Message(
**asdict(message) | {"id": self.next_message_id}
**asdict(message) | {"id": self.next_message_id},
)
type(self).next_message_id += 1

Expand All @@ -28,12 +28,7 @@ async def get_message_by_id(self, id_: MessageId) -> Message:
if message.id == id_:
return message

raise MessageNotFound(f"Message with id {id_} not found")
raise MessageNotFoundError(f"Message with id {id_} not found")

async def get_chat_messages_by_chat_id(self, chat_id: ChatId) -> list[Message]:
messages = []
for message in RAM_MESSAGES_DB:
if message.chat_id == chat_id:
messages.append(message)

return messages
return [message for message in RAM_MESSAGES_DB if message.chat_id == chat_id]
4 changes: 2 additions & 2 deletions src/just_chat/adapters/database/ram_session_db.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from just_chat.application.common.session_gateway import SessionGateway, SessionToken, SessionNotFound
from just_chat.application.common.session_gateway import SessionGateway, SessionNotFoundError, SessionToken
from just_chat.domain.models.user import UserId

RAM_SESSION_DB: dict[SessionToken, UserId] = {}
Expand All @@ -10,7 +10,7 @@ async def get_user_id(self, token: SessionToken) -> UserId:
if token in RAM_SESSION_DB:
return RAM_SESSION_DB[token]

raise SessionNotFound
raise SessionNotFoundError

async def save_session_token(self, user_id: UserId, token: SessionToken) -> None:
RAM_SESSION_DB[token] = user_id
14 changes: 7 additions & 7 deletions src/just_chat/adapters/database/ram_user_db.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
from collections.abc import Container
from dataclasses import asdict
from typing import Container

from just_chat.application.common.user_gateway import UserGateway, UserNotFound
from just_chat.domain.models.user import UserId, User
from just_chat.application.common.user_gateway import UserGateway, UserNotFoundError
from just_chat.domain.models.user import User, UserId

RAM_USERS_DB: list[User] = []

Expand All @@ -15,7 +15,7 @@ class RAMUserGateway(UserGateway):
async def save_user(self, user: User) -> User:
async with self.next_user_id_lock:
user_in_db = User(
**asdict(user) | {"id": self.next_user_id}
**asdict(user) | {"id": self.next_user_id},
)
type(self).next_user_id += 1

Expand All @@ -28,21 +28,21 @@ async def get_user_by_id(self, id_: UserId) -> User:
if user.id == id_:
return user

raise UserNotFound(f"User with id {id_} not found")
raise UserNotFoundError(f"User with id {id_} not found")

async def get_user_by_username(self, username: str) -> User:
for user in RAM_USERS_DB:
if user.username == username:
return user

raise UserNotFound(f"User with username {username} not found")
raise UserNotFoundError(f"User with username {username} not found")

async def get_random_user(self, exclude: Container[UserId]) -> User:
for user in RAM_USERS_DB:
if user.id not in exclude:
return user

raise UserNotFound("User not found")
raise UserNotFoundError("User not found")

async def delete_user_by_id(self, id_: UserId) -> User:
user = await self.get_user_by_id(id_)
Expand Down
17 changes: 7 additions & 10 deletions src/just_chat/adapters/events/websocket_event_gateway.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
from collections.abc import Sequence
from contextlib import suppress
from dataclasses import asdict
from typing import Sequence


from just_chat.application.common.event_bus import EventBus, ConnectionClosed
from just_chat.application.common.event_bus import ConnectionClosedError, EventBus
from just_chat.application.common.event_gateway import EventGateway
from just_chat.domain.models.event import NewMessage
from just_chat.domain.models.user import UserId


EVENT_BUS_DB: dict[UserId, EventBus] = {}


class WSEventGateway(EventGateway):
async def send_new_message_event(self, event: NewMessage, target_user_ids: Sequence[UserId]) -> None:
for user_id in target_user_ids:
bus = EVENT_BUS_DB.get(user_id, None)
bus = EVENT_BUS_DB.get(user_id)

if bus is None:
continue

try:
with suppress(ConnectionClosedError):
await bus.send_json({
"event": "new_message",
"message": asdict(event.message)
})
except ConnectionClosed:
pass
"message": asdict(event.message),
})

async def add_user_event_bus(self, event_bus: EventBus, user_id: UserId) -> None:
EVENT_BUS_DB[user_id] = event_bus
Expand Down
3 changes: 1 addition & 2 deletions src/just_chat/adapters/security/password_provider.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import Type

from passlib.ifc import PasswordHash

from just_chat.application.common.password_provider import PasswordProvider


class HashingPasswordProvider(PasswordProvider):
def __init__(self, hasher: PasswordHash | Type[PasswordHash]) -> None:
def __init__(self, hasher: PasswordHash | type[PasswordHash]) -> None:
self._hasher = hasher

def hash_password(self, password: str) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def __call__(self, data: NewChatDTO) -> Chat:

chat = await self._chat_gateway.create_chat_with_random_user(
title=data.title,
user_id=user_id
user_id=user_id,
)

assert chat.id is not None
Expand Down
2 changes: 1 addition & 1 deletion src/just_chat/application/common/chat_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from just_chat.domain.models.user import UserId


class ChatNotFound(ValueError):
class ChatNotFoundError(ValueError):
pass


Expand Down
4 changes: 2 additions & 2 deletions src/just_chat/application/common/event_bus.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod
from typing import Any


class ConnectionClosed(Exception):
class ConnectionClosedError(Exception):
pass


Expand Down
2 changes: 1 addition & 1 deletion src/just_chat/application/common/event_gateway.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Sequence
from collections.abc import Sequence

from just_chat.application.common.event_bus import EventBus
from just_chat.domain.models.event import NewMessage
Expand Down
2 changes: 1 addition & 1 deletion src/just_chat/application/common/message_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from just_chat.domain.models.message import Message, MessageId


class MessageNotFound(ValueError):
class MessageNotFoundError(ValueError):
pass


Expand Down
4 changes: 2 additions & 2 deletions src/just_chat/application/common/password_provider.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod, ABC
from abc import ABC, abstractmethod


class PasswordProvider(ABC): # TODO: mb better naming?
class PasswordProvider(ABC):
@abstractmethod
def hash_password(self, password: str) -> str:
raise NotImplementedError
Expand Down
3 changes: 1 addition & 2 deletions src/just_chat/application/common/session_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

from just_chat.domain.models.user import UserId


SessionToken = NewType("SessionToken", str)


class SessionNotFound(ValueError):
class SessionNotFoundError(ValueError):
pass


Expand Down
4 changes: 2 additions & 2 deletions src/just_chat/application/common/user_gateway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from abc import ABC, abstractmethod
from typing import Container
from collections.abc import Container

from just_chat.domain.models.user import User, UserId


class UserNotFound(ValueError):
class UserNotFoundError(ValueError):
pass


Expand Down
4 changes: 2 additions & 2 deletions src/just_chat/application/message/create_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(
event_service: EventService,
event_gateway: EventGateway,
message_gateway: MessageGateway,
id_provider: IdProvider
id_provider: IdProvider,
) -> None:
self._chat_access_service = chat_access_service
self._chat_gateway = chat_gateway
Expand All @@ -47,7 +47,7 @@ async def __call__(self, data: NewMessageDTO) -> Message:
chat_id=data.chat_id,
author_id=user_id,
owner_id=user_id,
)
),
)

assert message.id is not None
Expand Down
2 changes: 1 addition & 1 deletion src/just_chat/application/message/get_chat_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
chat_access_service: ChatAccessService,
message_gateway: MessageGateway,
chat_gateway: ChatGateway,
id_provider: IdProvider
id_provider: IdProvider,
) -> None:
self._chat_access_service = chat_access_service
self._message_gateway = message_gateway
Expand Down
2 changes: 1 addition & 1 deletion src/just_chat/application/user/create_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ async def __call__(self, data: NewUserDTO) -> CreatedUserDTO:

return CreatedUserDTO(
id=user.id,
username=user.username
username=user.username,
)
2 changes: 1 addition & 1 deletion src/just_chat/application/user/get_user_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ async def __call__(self, data: UserId) -> UserDTO:

return UserDTO(
id=user.id,
username=user.username
username=user.username,
)
3 changes: 1 addition & 2 deletions src/just_chat/application/user/get_user_by_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ def __init__(self, session_gateway: SessionGateway) -> None:
self._session_gateway = session_gateway

async def __call__(self, data: SessionToken) -> UserId:
user_id = await self._session_gateway.get_user_id(token=data)
return user_id
return await self._session_gateway.get_user_id(token=data)
10 changes: 5 additions & 5 deletions src/just_chat/application/user/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from just_chat.application.common.interactor import Interactor
from just_chat.application.common.password_provider import PasswordProvider
from just_chat.application.common.session_gateway import SessionGateway, SessionToken
from just_chat.application.common.user_gateway import UserGateway, UserNotFound
from just_chat.application.common.user_gateway import UserGateway, UserNotFoundError
from just_chat.domain.services.user import UserService


class WrongCredentials(Exception):
class WrongCredentialsError(Exception):
pass


Expand All @@ -34,13 +34,13 @@ def __init__(
async def __call__(self, data: LoginDTO) -> SessionToken:
try:
user = await self._user_gateway.get_user_by_username(data.username)
except UserNotFound:
raise WrongCredentials
except UserNotFoundError as err:
raise WrongCredentialsError from err

assert user.id is not None

if not self._password_provider.verify_password(data.password, user.hashed_password):
raise WrongCredentials
raise WrongCredentialsError

token = uuid.uuid4()

Expand Down
Loading

0 comments on commit 34aba03

Please sign in to comment.