Skip to content

Commit

Permalink
Update sample to follow new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
BenTalese committed Jul 22, 2023
1 parent 70a95ea commit fb0b5eb
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
15 changes: 9 additions & 6 deletions sample/interface_adaptors/greet_presenter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import Any, Coroutine

from sample.use_cases.greet.igreet_output_port import IGreetOutputPort
from src.clapy.outputs import ValidationResult


class GreetPresenter(IGreetOutputPort):

async def present_greeting_async(self, greeting: str) -> Coroutine[Any, Any, None]:
return print(greeting)
async def present_greeting_async(self, greeting: str) -> None:
print(greeting)

async def present_validation_failure_async(self, validation_failure: ValidationResult) -> None:
print(validation_failure.summary)

async def present_validation_failure_async(self, validation_failure: Any) -> Coroutine[Any, Any, None]:
return print(validation_failure)
async def present_missing_names_warning_async(self) -> bool:
user_input = input("You only told me one of your names, do you want to continue? Enter 'Y' to continue or 'N' to stop.")
return user_input == 'Y'
14 changes: 8 additions & 6 deletions sample/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import sys
import time


sys.path.append(os.getcwd())

Expand All @@ -11,11 +9,11 @@

from sample.interface_adaptors.conversation_controller import \
ConversationController
from sample.pipeline.test_pipe import TestPipe
from sample.interface_adaptors.greet_presenter import GreetPresenter
from sample.pipeline.pipeline_configuration import PipelineConfiguration
from sample.use_cases.greet.greet_input_port import GreetInputPort
from src.clapy.dependency_injection import DependencyInjectorServiceProvider
from src.clapy.pipeline import RequiredInputValidator


async def main():
Expand All @@ -26,12 +24,16 @@ async def main():
_ServiceProvider.register_pipe_services(_UsecaseScanLocations, [r"venv", r"src"], [r".*main\.py"])
_ServiceProvider.construct_usecase_invoker(_UsecaseScanLocations)

_ServiceProvider.register_service(providers.Factory, RequiredInputValidator) #TODO: hmm....this isn't great

_ServiceProvider.register_service(providers.Factory, ConversationController)
_ServiceProvider.register_service(providers.Factory, TestPipe)

_Controller = _ServiceProvider.get_service(ConversationController)
_Controller: ConversationController = _ServiceProvider.get_service(ConversationController)

_InputPort = GreetInputPort()
_InputPort.name = "Ben Ben"

await _Controller.greet_async(GreetInputPort("Ben"), GreetPresenter(), PipelineConfiguration.OtherConfiguration.value)
await _Controller.greet_async(_InputPort, GreetPresenter(), PipelineConfiguration.DefaultConfiguration.value)

if __name__ == "__main__":
asyncio.run(main())
8 changes: 3 additions & 5 deletions sample/pipeline/name_checker.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from typing import Generic
from src.clapy.outputs import IOutputPort
from src.clapy.pipeline import IPipe, InputPort

from src.clapy.generics import TInputPort, TOutputPort
from src.clapy.pipeline import IPipe


class NameChecker(IPipe, Generic[TInputPort, TOutputPort]):
class NameChecker(IPipe, InputPort, IOutputPort):
pass
15 changes: 12 additions & 3 deletions sample/use_cases/greet/greet_input_port.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from src.clapy.pipeline import InputPort
from src.clapy.pipeline import InputPort, required


class GreetInputPort(InputPort):

def __init__(self, name: str):
self.name = name
def __init__(self) -> None:
self._name = None

@property
@required
def name(self) -> str:
return self._name

@name.setter
def name(self, value):
self._name = value
9 changes: 2 additions & 7 deletions sample/use_cases/greet/greet_interactor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from typing import Any, Coroutine, Union

from sample.use_cases.greet.greet_input_port import GreetInputPort
from sample.use_cases.greet.igreet_output_port import IGreetOutputPort
from src.clapy.pipeline import Interactor


class GreetInteractor(Interactor):

def execute_async(
self,
input_port: GreetInputPort,
output_port: IGreetOutputPort) -> Coroutine[GreetInputPort, Any, Union[Coroutine, None]]:
return output_port.present_greeting_async(f"Hello {input_port.name}!")
async def execute_async(self, input_port: GreetInputPort, output_port: IGreetOutputPort) -> None:
await output_port.present_greeting_async(f"Hello {input_port.name}!")
18 changes: 11 additions & 7 deletions sample/use_cases/greet/greet_name_checker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Coroutine, Union
from outputs import ValidationResult

from sample.pipeline.name_checker import NameChecker
from sample.use_cases.greet.greet_input_port import GreetInputPort
Expand All @@ -7,11 +8,14 @@

class GreetNameChecker(NameChecker):

async def execute_async(
self,
input_port: GreetInputPort,
output_port: IGreetOutputPort) -> Coroutine[GreetInputPort, Any, Union[Coroutine, None]]:
if len(input_port.name) > 10:
return output_port.present_validation_failure_async("Your name is too long for me to say... 😔")
async def execute_async(self, input_port: GreetInputPort, output_port: IGreetOutputPort) -> None:
if len(input_port.name) > 20:
await output_port.present_validation_failure_async(
ValidationResult.from_error(input_port.name, "Your name is too long for me to say... 😔"))
self.has_failures = True

print("That's a nice name! 😊")
elif len(input_port.name.split(" ")) < 2:
self.has_failures = not await output_port.present_missing_names_warning_async()

else:
print("That's a nice name! 😊")
8 changes: 6 additions & 2 deletions sample/use_cases/greet/igreet_output_port.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from abc import ABC, abstractmethod

from src.clapy.outputs import IValidationOutputPort
from src.clapy.outputs import IOutputPort, IValidationOutputPort


class IGreetOutputPort(IValidationOutputPort, ABC):
class IGreetOutputPort(IOutputPort, IValidationOutputPort, ABC):

@abstractmethod
async def present_greeting_async(self, greeting: str) -> None:
pass

@abstractmethod
async def present_missing_names_warning_async(self) -> bool:
pass

0 comments on commit fb0b5eb

Please sign in to comment.