Skip to content

Commit

Permalink
BREAKING CHANGE: rename decorators and create isolated modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cafadev committed Oct 12, 2023
1 parent 1d6057a commit 5464c41
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 67 deletions.
Empty file added tests/__init__.py
Empty file.
7 changes: 3 additions & 4 deletions tests/log/axioma/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
from dataclasses import dataclass
from typing import TypeAlias

from turbobus.bus import Command, CommandHandler
from turbobus.decorators import command
from turbobus.command import Command, CommandHandler


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

content: str

class ILogHandler(CommandHandler[str]):
class ILogHandler(CommandHandler[LogCommand, str]):

...

class ILogger(ABC):

@abstractmethod
def logger(self, text: str) -> None:
...
...
11 changes: 5 additions & 6 deletions tests/log/capabilities/log.py
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
4 changes: 2 additions & 2 deletions tests/log/capabilities/logger.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from turbobus.decorators import injectable
from turbobus.injection import injectable_of
from log.axioma.log import ILogger


@injectable(ILogger)
@injectable_of(ILogger)
class Logger:

def logger(self, text: str):
Expand Down
9 changes: 9 additions & 0 deletions tests/main.py
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')
)
6 changes: 4 additions & 2 deletions turbobus/__init__.py
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 *
23 changes: 3 additions & 20 deletions turbobus/bus.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
from abc import abstractmethod, ABC
from typing import Any, Generic, TypeAlias, TypeVar
from typing import Any, TypeVar

CommandHandlerType: TypeAlias = 'CommandHandler'
HandlerType = TypeVar('HandlerType', bound=CommandHandlerType, covariant=True)
ReturnType = TypeVar('ReturnType')

class Command(ABC, Generic[HandlerType]):

__handler__: type[HandlerType]


class CommandHandler(ABC, Generic[ReturnType, ]):

def __init__(self, ) -> None:
super().__init__()

@abstractmethod
def execute(self, cmd: Command[HandlerType]) -> ReturnType:
...
from .exception import CommandHandlerDoesNotExistException

ReturnType = TypeVar('ReturnType')

class CommandBus:

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

if Handler is None:
from turbobus.exception import CommandHandlerDoesNotExistException
raise CommandHandlerDoesNotExistException()

result = Handler(**providers).execute(cmd)
Expand Down
33 changes: 33 additions & 0 deletions turbobus/command.py
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
1 change: 1 addition & 0 deletions turbobus/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
providers = {}
33 changes: 0 additions & 33 deletions turbobus/decorators.py

This file was deleted.

21 changes: 21 additions & 0 deletions turbobus/injection.py
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()

0 comments on commit 5464c41

Please sign in to comment.