Skip to content

Commit

Permalink
feat: rename command decorator with kw_only_frozen
Browse files Browse the repository at this point in the history
  • Loading branch information
cafadev committed Apr 14, 2024
1 parent 13d7076 commit 200a93c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
20 changes: 11 additions & 9 deletions turbobus/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from dataclasses import dataclass
from typing import Any, Generic, TypeVar, cast, dataclass_transform

from .exception import CommandHandlerDoesNotExistException
from .exception import CommandHandlerDoesNotExist, IncompatibleCommandType, IncompatibleHandlerReturnType
from .constants import Provider


ReturnType = TypeVar('ReturnType')


@dataclass_transform(kw_only_default=True, frozen_default=True)
def command(cls):
def kw_only_frozen(cls):
C = dataclass(kw_only=True, frozen=True)(cls)
return C

Expand All @@ -19,9 +19,11 @@ class Command(Generic[ReturnType]):

__return_type__: type[ReturnType]

def __class_getitem__(cls, item):
def __class_getitem__(cls, returnType):


C = type(cls.__name__, (), {
'__return_type__': item,
'__return_type__': returnType,
})

return C
Expand All @@ -47,18 +49,18 @@ def __init_subclass__(cls, *args, **kwargs):
if len(function_typing) < 2:
return

cmd, returnType = function_typing
cmd, return_type = function_typing

try:
__return_type__ = cmd.__origin__.__return_type__
except AttributeError:
__return_type__ = cmd.__return_type__

if cmd is not cls.__command__:
raise TypeError(f"Incompatible command type - expected {cls.__command__} but got {cmd} in {cls.__name__}")
raise IncompatibleCommandType(cls.__name__, cls.__command__, cmd)

if __return_type__ != returnType:
raise TypeError(f"Incompatible return type - expected {cmd.__return_type__} but got {returnType} in {cls.__name__}")
if __return_type__ != return_type:
raise IncompatibleHandlerReturnType(cls.__name__, __return_type__, return_type)

@abstractmethod
def execute(self, cmd: Command[ReturnType]) -> ReturnType:
Expand All @@ -71,7 +73,7 @@ def execute(self, cmd: Command[ReturnType], providers: dict[Any, Any] = {}) -> R
Handler = Provider.get(cmd.__class__.__name__)

if Handler is None:
raise CommandHandlerDoesNotExistException()
raise CommandHandlerDoesNotExist()

handler = Handler(**providers)

Expand Down
15 changes: 10 additions & 5 deletions turbobus/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@

class Provider:

providers: dict[str, Any] = {}
_providers: dict[str, Any] = {}

@classmethod
def set(cls, key: str, value: Any):
cls.providers[key] = value
def set(cls, key: type | str, value: Any):
provider_name = key if isinstance(key, str) else key.__name__
cls._providers[provider_name] = value

@classmethod
def get(cls, key: str) -> Any:
return cls.providers.get(key)
def get(cls, key: type | str) -> Any:
return cls._providers.get(key if isinstance(key, str) else key.__name__)

@classmethod
def clear(cls):
cls._providers = {}
19 changes: 15 additions & 4 deletions turbobus/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@ class CommandBusException(Exception):
pass


class CommandAlreadyExistException(CommandBusException):
class CommandAlreadyExist(CommandBusException):
pass


class CommandHandlerDoesNotExistException(CommandBusException):
class CommandHandlerDoesNotExist(CommandBusException):
pass


class CommandExecutionAlreadyInProgressException(CommandBusException):
pass
class IncompatibleHandlerReturnType(Exception):

def __init__(self, class_name, expected_return_type, received_return_type):
super().__init__(
f"Incompatible return type - expected {expected_return_type} but got {received_return_type} in {class_name}"
)

class IncompatibleCommandType(Exception):

def __init__(self, class_name, expected_command_type, received_command_type):
super().__init__(
f"Incompatible command type - expected {expected_command_type} but got {received_command_type} in {class_name}"
)

0 comments on commit 200a93c

Please sign in to comment.