From 744961a1538fbb0d821c5d2a332d65fd8c6b1290 Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 27 Nov 2024 22:52:45 +0100 Subject: responses basic datastructure and deserialization --- __main__.py | 24 ++++++++++++++------- config.yaml | 4 +++- modules/config.py | 18 +++++++++++----- modules/responses/__init__.py | 0 modules/responses/responses.py | 37 ++++++++++++++++++++++++++++++++ responses/process_HesExecuteProcess.yaml | 5 +++++ 6 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 modules/responses/__init__.py create mode 100644 modules/responses/responses.py create mode 100644 responses/process_HesExecuteProcess.yaml diff --git a/__main__.py b/__main__.py index 797ec56..6692d7e 100644 --- a/__main__.py +++ b/__main__.py @@ -8,6 +8,7 @@ from .modules.config import load_config from .modules.hassil.recognize import recognize from .modules.hassil.util import merge_dict from .modules.hassil.intents import Intents, TextSlotList +from modules.responses.responses import Responses from .modules.input_handlers.stdin_input import StdinInput from .modules.input_handlers.pipewire_record import PipeWireRecord @@ -19,9 +20,22 @@ def main(): config = load_config() input_dict = {"intents": {}} - yaml_path = Path(config.intents_dir) - yaml_file_paths = yaml_path.glob("*.yaml") + intent_yaml_path = Path(config.intents_dir) + intent_yaml_file_paths = intent_yaml_path.glob("*.yaml") + for yaml_file_path in intent_yaml_file_paths: + with open(yaml_file_path, "r", encoding="utf-8") as yaml_file: + merge_dict(input_dict, yaml.safe_load(yaml_file)) + + intents = Intents.from_dict(input_dict) + + response_yaml_path = Path(config.responses_dir) + response_yaml_file_paths = response_yaml_path.glob("*.yaml") + for yaml_file_path in response_yaml_file_paths: + with open(yaml_file_path, "r", encoding="utf-8") as yaml_file: + merge_dict(input_dict, yaml.safe_load(yaml_file)) + responses = Responses.from_dict(input_dict) + processes = [] for file in os.listdir(config.applications_dir): if file == "[": @@ -35,12 +49,6 @@ def main(): "process": TextSlotList.from_strings(processes) } - for yaml_file_path in yaml_file_paths: - with open(yaml_file_path, "r", encoding="utf-8") as yaml_file: - merge_dict(input_dict, yaml.safe_load(yaml_file)) - - intents = Intents.from_dict(input_dict) - input_handler = PipeWireRecord() if config.input_handler == Config.INPUT_PW else StdinInput() try: diff --git a/config.yaml b/config.yaml index 0903321..cc3c816 100644 --- a/config.yaml +++ b/config.yaml @@ -1,5 +1,7 @@ -input_mode: pw-record # other option: stdin intents_dir: sentences +responses_dir: responses applications_dir: /usr/bin +input_mode: pw-record # other option: stdin + lock: swaylock \ No newline at end of file diff --git a/modules/config.py b/modules/config.py index 83243a0..b0fc624 100644 --- a/modules/config.py +++ b/modules/config.py @@ -14,21 +14,29 @@ class Config: self.input_mode = "" self.intents_dir = "" + self.responses_dir = "" self.applications_dir = "" self.lock = "" - def update(self, **entries): + def update(self, **entries) -> None: self.__dict__.update(entries) - if not self.intents_dir.startswith("/"): - self.intents_dir = os.path.join(os.path.dirname(__main__.__file__), self.intents_dir) + self.intents_dir = Config.__convert_to_absolute_path(self.intents_dir) + self.responses_dir = Config.__convert_to_absolute_path(self.responses_dir) - def validate(self): + @staticmethod + def __convert_to_absolute_path(path: str) -> str: + if not path.startswith("/"): + path = os.path.join(os.path.dirname(__main__.__file__), path) + + return path + + def validate(self) -> None: if self.input_mode not in self.INPUT_MODES: sys.exit(f"Invalid input_mode '{self.input_mode}', valid options: {", ".join(self.INPUT_MODES)}") -def load_config(): +def load_config() -> Config: config = Config() with open(os.path.join(os.path.dirname(__main__.__file__), "config.yaml")) as stream: config.update(**yaml.safe_load(stream)) diff --git a/modules/responses/__init__.py b/modules/responses/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/responses/responses.py b/modules/responses/responses.py new file mode 100644 index 0000000..1836deb --- /dev/null +++ b/modules/responses/responses.py @@ -0,0 +1,37 @@ +from dataclasses import dataclass, field +from typing import List, Dict, Any + +@dataclass(frozen=True) +class ResponseData: + sentence_texts: List[str] + +@dataclass(frozen=True) +class Response: + name: str + data: List[ResponseData] = field(default_factory=list) + +@dataclass(frozen=True) +class Responses: + responses: Dict[str, Response] + + @staticmethod + def from_dict(input_dict: Dict[str, Any]) -> "Responses": + # responses: + # ResponseName: + # data: + # - sentences: + # - "" + return Responses( + responses={ + response_name: Response( + name=response_name, + data=[ + ResponseData( + sentence_texts=data_dict["sentences"], + ) + for data_dict in response_dict["data"] + ], + ) + for response_name, response_dict in input_dict["responses"].items() + }, + ) \ No newline at end of file diff --git a/responses/process_HesExecuteProcess.yaml b/responses/process_HesExecuteProcess.yaml new file mode 100644 index 0000000..66653c2 --- /dev/null +++ b/responses/process_HesExecuteProcess.yaml @@ -0,0 +1,5 @@ +responses: + HesExecuteProcess: + data: + - sentences: + "Right away." \ No newline at end of file -- cgit v1.2.3-70-g09d2