1
0
mirror of https://github.com/MarkParker5/STARK.git synced 2024-11-24 08:12:13 +02:00

DependencyManager

This commit is contained in:
MarkParker5 2023-03-17 12:34:03 +01:00
parent 60aab31713
commit 632b60ab8e
No known key found for this signature in database
GPG Key ID: 0632FDCE0F9ED5C7
4 changed files with 49 additions and 12 deletions

View File

@ -2,6 +2,7 @@ from typing import Any, Protocol
from dataclasses import dataclass
import asyncio
from general.dependencies import DependencyManager, default_dependency_manager
from .CommandsManager import CommandsManager
from .Command import Command, Response, ResponseHandler
from .Threads import ThreadData
@ -20,15 +21,18 @@ class CommandsContext:
delegate: CommandsContextDelegate
commands_manager: CommandsManager
dependency_manager: DependencyManager
last_response: Response | None = None
_context_queue: list[CommandsContextLayer]
_threads: list[ThreadData]
def __init__(self, commands_manager):
def __init__(self, commands_manager: CommandsManager, dependency_manager: DependencyManager = default_dependency_manager):
self.commands_manager = commands_manager
self._context_queue = [self.root_context]
self._threads = []
self.dependency_manager = dependency_manager
self.dependency_manager.add_dependency(None, ResponseHandler, self)
@property
def root_context(self):
@ -52,14 +56,10 @@ class CommandsContext:
for search_result in search_results:
parameters = {**current_context.parameters, **search_result.match_result.parameters}
parameters = current_context.parameters
parameters.update(search_result.match_result.parameters)
parameters.update(self.dependency_manager.resolve(search_result.command._runner))
# pass dependencies as parameters
for name, annotation in search_result.command._runner.__annotations__.items():
if annotation == ResponseHandler:
parameters[name] = self
# execute command
command_response = search_result.command(parameters)
if not command_response:

View File

@ -82,9 +82,7 @@ class VoiceAssistant(SpeechRecognizerDelegate, CommandsContextDelegate):
# repeat responses
while self._responses:
response = self._responses.pop(0)
print('Reporting:', response.text)
if (datetime.now() - response.time).total_seconds() <= timeout_before_repeat:
print('Skip because of timeout', (datetime.now() - response.time).total_seconds(), timeout_before_repeat)
continue
self._play_response(response)

View File

@ -0,0 +1,38 @@
from typing import Callable, Any
from dataclasses import dataclass
@dataclass
class Dependency:
name: str | None
annotation: type
value: Any
def __eq__(self, other: object) -> bool:
if not isinstance(other, Dependency):
raise TypeError(f"Cannot compare Dependency with {type(other)}")
return self.name == other.name and self.annotation == other.annotation
def __hash__(self):
return hash((self.name, self.annotation))
class DependencyManager:
dependencies: set[Dependency]
def __init__(self):
self.dependencies = set()
def resolve(self, callable: Callable) -> dict[str, Any]:
parameters = {}
for name, annotation in callable.__annotations__.items():
for dependency in self.dependencies:
if dependency.annotation == annotation and (dependency.name == name or not dependency.name):
parameters[name] = dependency.value
return parameters
def add_dependency(self, name: str | None, annotation: type, value: Any):
self.dependencies.add(Dependency(name, annotation, value))
default_dependency_manager = DependencyManager()
default_dependency_manager.add_dependency(None, DependencyManager, default_dependency_manager)

View File

@ -1,6 +1,6 @@
import pytest
import config
import time
from general.dependencies import DependencyManager
from VICore import (
CommandsManager,
CommandsContext,
@ -46,8 +46,9 @@ class SpeechSynthesizerMock:
@pytest.fixture
def commands_context_flow() -> tuple[CommandsManager, CommandsContext, CommandsContextDelegateMock]:
dependencies = DependencyManager()
manager = CommandsManager()
context = CommandsContext(manager)
context = CommandsContext(manager, dependencies)
context_delegate = CommandsContextDelegateMock()
context.delegate = context_delegate