-
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.
BREAKING CHANGE: rename decorators and create isolated modules
- Loading branch information
Showing
11 changed files
with
81 additions
and
67 deletions.
There are no files selected for viewing
Empty file.
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,16 +1,15 @@ | ||
from dataclasses import dataclass | ||
from turbobus.decorators import command, injecting | ||
from turbobus.injection import inject | ||
from turbobus.command import handler_of | ||
from ..axioma.log import ILogHandler, ILogger, LogCommand | ||
|
||
|
||
@command(LogCommand) | ||
@handler_of(LogCommand) | ||
@dataclass(kw_only=True) | ||
class LogHandler(ILogHandler): | ||
|
||
dependency = injecting(ILogger) | ||
dependency = inject(ILogger) | ||
|
||
def execute(self, cmd: LogCommand) -> str: | ||
if self.dependency is not None: | ||
self.dependency.logger(cmd.content) | ||
|
||
self.dependency.logger(cmd.content) | ||
return cmd.content |
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,9 @@ | ||
from turbobus.bus import CommandBus | ||
|
||
from log.axioma import LogCommand | ||
|
||
bus = CommandBus() | ||
|
||
result = bus.execute( | ||
LogCommand('Hello world') | ||
) |
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,5 @@ | ||
from .constants import * | ||
from .command import * | ||
from .injection import * | ||
from .exception import * | ||
from .bus import * | ||
from .decorators import * | ||
from .exception import * |
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,33 @@ | ||
from abc import abstractmethod, ABC | ||
from typing import Generic, TypeAlias, TypeVar | ||
from .constants import providers | ||
|
||
CommandHandlerType: TypeAlias = 'CommandHandler' | ||
HandlerType = TypeVar('HandlerType', bound=CommandHandlerType, covariant=True) | ||
ReturnType = TypeVar('ReturnType') | ||
|
||
class Command(ABC, Generic[HandlerType]): | ||
|
||
__handler__: type[HandlerType] | ||
|
||
|
||
CommandType = TypeVar('CommandType', bound=Command) | ||
class CommandHandler(ABC, Generic[CommandType, ReturnType]): | ||
|
||
def __init__(self, ) -> None: | ||
super().__init__() | ||
|
||
@abstractmethod | ||
def execute(self, cmd: CommandType) -> ReturnType: | ||
... | ||
|
||
|
||
def handler_of(commandClass, injectable: type | None = None): | ||
def wrapper(handlerClass): | ||
if injectable is not None: | ||
providers.__setitem__(injectable.__name__, handlerClass) | ||
|
||
commandClass.__handler__ = handlerClass | ||
return handlerClass | ||
|
||
return wrapper |
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 @@ | ||
providers = {} |
This file was deleted.
Oops, something went wrong.
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,21 @@ | ||
from typing import TypeVar | ||
from .constants import providers | ||
|
||
|
||
T = TypeVar('T') | ||
|
||
|
||
def injectable_of(to: type): | ||
def wrapper(cls): | ||
providers.__setitem__(to.__name__, cls) | ||
return cls | ||
|
||
return wrapper | ||
|
||
|
||
def inject(cls: type[T]) -> T: | ||
Provider = providers.get(cls.__name__) | ||
|
||
assert Provider is not None | ||
|
||
return Provider() |