Skip to content

Commit 5464c41

Browse files
committed
BREAKING CHANGE: rename decorators and create isolated modules
1 parent 1d6057a commit 5464c41

File tree

11 files changed

+81
-67
lines changed

11 files changed

+81
-67
lines changed

tests/__init__.py

Whitespace-only changes.

tests/log/axioma/log.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
from dataclasses import dataclass
33
from typing import TypeAlias
44

5-
from turbobus.bus import Command, CommandHandler
6-
from turbobus.decorators import command
5+
from turbobus.command import Command, CommandHandler
76

87

98
LogHandlerType: TypeAlias = "ILogHandler"
@@ -13,12 +12,12 @@ class LogCommand(Command[LogHandlerType]):
1312

1413
content: str
1514

16-
class ILogHandler(CommandHandler[str]):
15+
class ILogHandler(CommandHandler[LogCommand, str]):
1716

1817
...
1918

2019
class ILogger(ABC):
2120

2221
@abstractmethod
2322
def logger(self, text: str) -> None:
24-
...
23+
...

tests/log/capabilities/log.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
from dataclasses import dataclass
2-
from turbobus.decorators import command, injecting
2+
from turbobus.injection import inject
3+
from turbobus.command import handler_of
34
from ..axioma.log import ILogHandler, ILogger, LogCommand
45

56

6-
@command(LogCommand)
7+
@handler_of(LogCommand)
78
@dataclass(kw_only=True)
89
class LogHandler(ILogHandler):
910

10-
dependency = injecting(ILogger)
11+
dependency = inject(ILogger)
1112

1213
def execute(self, cmd: LogCommand) -> str:
13-
if self.dependency is not None:
14-
self.dependency.logger(cmd.content)
15-
14+
self.dependency.logger(cmd.content)
1615
return cmd.content

tests/log/capabilities/logger.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from turbobus.decorators import injectable
1+
from turbobus.injection import injectable_of
22
from log.axioma.log import ILogger
33

44

5-
@injectable(ILogger)
5+
@injectable_of(ILogger)
66
class Logger:
77

88
def logger(self, text: str):

tests/main.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from turbobus.bus import CommandBus
2+
3+
from log.axioma import LogCommand
4+
5+
bus = CommandBus()
6+
7+
result = bus.execute(
8+
LogCommand('Hello world')
9+
)

turbobus/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from .constants import *
2+
from .command import *
3+
from .injection import *
4+
from .exception import *
15
from .bus import *
2-
from .decorators import *
3-
from .exception import *

turbobus/bus.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,15 @@
1-
from abc import abstractmethod, ABC
2-
from typing import Any, Generic, TypeAlias, TypeVar
1+
from typing import Any, TypeVar
32

4-
CommandHandlerType: TypeAlias = 'CommandHandler'
5-
HandlerType = TypeVar('HandlerType', bound=CommandHandlerType, covariant=True)
6-
ReturnType = TypeVar('ReturnType')
7-
8-
class Command(ABC, Generic[HandlerType]):
9-
10-
__handler__: type[HandlerType]
11-
12-
13-
class CommandHandler(ABC, Generic[ReturnType, ]):
14-
15-
def __init__(self, ) -> None:
16-
super().__init__()
17-
18-
@abstractmethod
19-
def execute(self, cmd: Command[HandlerType]) -> ReturnType:
20-
...
3+
from .exception import CommandHandlerDoesNotExistException
214

5+
ReturnType = TypeVar('ReturnType')
226

237
class CommandBus:
248

259
def execute(self, cmd, providers: dict[Any, Any] = {}):
2610
Handler = cmd.__handler__
2711

2812
if Handler is None:
29-
from turbobus.exception import CommandHandlerDoesNotExistException
3013
raise CommandHandlerDoesNotExistException()
3114

3215
result = Handler(**providers).execute(cmd)

turbobus/command.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from abc import abstractmethod, ABC
2+
from typing import Generic, TypeAlias, TypeVar
3+
from .constants import providers
4+
5+
CommandHandlerType: TypeAlias = 'CommandHandler'
6+
HandlerType = TypeVar('HandlerType', bound=CommandHandlerType, covariant=True)
7+
ReturnType = TypeVar('ReturnType')
8+
9+
class Command(ABC, Generic[HandlerType]):
10+
11+
__handler__: type[HandlerType]
12+
13+
14+
CommandType = TypeVar('CommandType', bound=Command)
15+
class CommandHandler(ABC, Generic[CommandType, ReturnType]):
16+
17+
def __init__(self, ) -> None:
18+
super().__init__()
19+
20+
@abstractmethod
21+
def execute(self, cmd: CommandType) -> ReturnType:
22+
...
23+
24+
25+
def handler_of(commandClass, injectable: type | None = None):
26+
def wrapper(handlerClass):
27+
if injectable is not None:
28+
providers.__setitem__(injectable.__name__, handlerClass)
29+
30+
commandClass.__handler__ = handlerClass
31+
return handlerClass
32+
33+
return wrapper

turbobus/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
providers = {}

turbobus/decorators.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)