def get_event_and_context(self) -> (str, str):
        event = None
        context = None

        if self.is_event_with_wrapper is False:
            event_body = load_json(self.event_file_filepath)

            if self.platform not in self.AVAILABLE_PLATFORMS:
                raise Exception(
                    f"The platform named {self.platform} has not been found in the available platforms : {self.AVAILABLE_PLATFORMS}"
                )

            if self.platform == self.PLATFORM_GOOGLE:
                event = load_json(
                    os.path.join(self.wrappers_folderpath,
                                 "api_gateway_google_path_wrapper.json"))
                event["body"] = event_body
            elif self.platform == self.PLATFORM_ALEXA:
                event = event_body
            elif self.platform == self.PLATFORM_BIXBY:
                pass
        else:
            event = load_json(self.event_file_filepath)

        return event, context
Пример #2
0
def _get_from_name_or_ask_to_select_template(selected_template_name: str = None) -> dict:
    current_folder_path = Path(os.path.dirname(os.path.abspath(__file__)))
    templates_folder_path = os.path.join(current_folder_path.parent, "templates")
    invalid_folder_names = ["__pycache__"]

    available_templates = dict()
    for template_dir_name in os.listdir(templates_folder_path):
        if template_dir_name not in invalid_folder_names:
            if os.path.isdir(os.path.join(templates_folder_path, template_dir_name)):
                current_template_dir_path = os.path.join(templates_folder_path, template_dir_name)
                files_names_in_current_template_dir = os.listdir(current_template_dir_path)

                if not len(files_names_in_current_template_dir) > 0:
                    click.echo(f"That's strange. The template folder at {current_template_dir_path} "
                          f"did not contain any file, and so cannot be used.")
                else:
                    paths_to_files_to_copy = list()
                    for filename in files_names_in_current_template_dir:
                        if filename not in ["infos.yaml", "infos.json"]:
                            filepath = os.path.join(current_template_dir_path, filename)
                            if os.path.isfile(filepath):
                                paths_to_files_to_copy.append(filepath)

                    if not len(paths_to_files_to_copy) > 0:
                        click.echo(f"That's strange. The template folder at {current_template_dir_path} "
                              f"did not contain any file that could be copied, and so cannot be used.")
                    else:
                        current_template_infos_dict = None

                        if "infos.yaml" in files_names_in_current_template_dir:
                            from inoft_vocal_framework.utils.general import load_yaml
                            current_template_infos_dict = load_yaml(filepath=os.path.join(current_template_dir_path, "infos.yaml"))
                        elif "infos.json" in files_names_in_current_template_dir:
                            from inoft_vocal_framework.utils.general import load_json
                            current_template_infos_dict = load_json(filepath=os.path.join(current_template_dir_path, "infos.json"))

                        if isinstance(current_template_infos_dict, dict):
                            if "name" in current_template_infos_dict.keys() and isinstance(current_template_infos_dict["name"], str):
                                available_templates[current_template_infos_dict["name"]] = {"pathsToFilesToCopy": paths_to_files_to_copy}
                                continue

                        available_templates[template_dir_name] = {"pathsToFilesToCopy": paths_to_files_to_copy}
                        # If the no infos dict has been created or that no name variable has been found in the infos, we will not have
                        # triggered the continue keyword, and so we will reach this stage where we add the template with its folder name.

    if selected_template_name is None:
        selected_template_name = click.prompt("Type the name of the template you would like to use, the followings are available :",
                                              type=click.Choice(available_templates.keys()))
        # todo: improve that, i would like for the user to select it with a list select (like in PyInquirer)

    if selected_template_name in available_templates.keys():
        return available_templates[selected_template_name]
    else:
        return None
import jinja2

from inoft_vocal_framework.bixby_core.templates.templates_access import TemplatesAccess
from inoft_vocal_framework.safe_dict import SafeDict
from inoft_vocal_framework.utils.general import load_json

model_dict = SafeDict(load_json("F:/Inoft/skill_histoire_decryptage_1/inoft_vocal_framework/bixby_core/test_model.json"))
intents = model_dict.get("intents").to_dict()

class Core:
    def render(self):
        out = TemplatesAccess().endpoints_template.render(intents=intents)
        print(out)

    @staticmethod
    def write_to_file(text: str, filepath: str):
        with open(filepath, "w+") as file:
            file.write(text)

Core().render()
Пример #4
0
    def process(self):
        self.messages = Messages(
            messages_items=load_json(self.builtin_text_filepath))
        self.write_to_file(
            text=self.messages.render(),
            filepath="F:/Inoft/skill_histoire_decryptage_1/messages.py")
        # os.path.join(os.path.dirname(os.path.abspath(__file__)), "messages.py"))

        flow_dict = load_json(filepath=self.main_flow_filepath)
        if "nodes" not in flow_dict.keys():
            raise Exception(
                f"The nodes key has not been found in the dict of the flow_file : {flow_dict}"
            )
        else:
            name_start_node = flow_dict["startNode"]
            nodes_list = flow_dict["nodes"]
            # Initialization of the classes
            for node in nodes_list:
                node_name = node["name"] or ""
                self.node_values_dict[node_name] = node

                if node_name == name_start_node:
                    self.node_classes_dict[node_name] = LaunchRequestHandler(
                        node_dict=node)
                    self.has_request_handlers = True
                else:
                    self.node_classes_dict[node_name] = StateHandler(
                        node_name=node_name)
                    self.has_state_handlers = True

            # Processing of the classes and their interactions with each others
            for node_class in self.node_classes_dict.values():
                node_class.process(parent_core=self)

                if isinstance(node_class, StateHandler):
                    for intent_key, count_value in node_class.counts_used_condition_intent_names.items(
                    ):
                        if intent_key in self.counts_used_condition_intent_names.keys(
                        ):
                            self.counts_used_condition_intent_names[
                                intent_key] += count_value
                        else:
                            self.counts_used_condition_intent_names[
                                intent_key] = count_value

            output_conditions_classes = list()
            for intent_key, count_value in self.counts_used_condition_intent_names.items(
            ):
                if count_value >= self.threshold_of_intent_use_to_create_a_condition:
                    new_intent_name_condition_class = IntentNameCondition(
                        intent_name=intent_key)
                    new_intent_name_condition_class.render(parent_core=self)
                    output_conditions_classes.append(
                        new_intent_name_condition_class)
                    self.has_conditions_classes = True

            output_handlers_list = list()
            for class_from_node in self.node_classes_dict.values():
                for handler_class in class_from_node.render(parent_core=self):
                    output_handlers_list.append(handler_class)

            skill_app_rendered_code = TemplatesAccess(
            ).skill_app_template.render(
                conditions_classes_list=output_conditions_classes,
                handlers_list=output_handlers_list,
                has_condition_classes=self.has_conditions_classes,
                has_request_handlers=self.has_request_handlers,
                has_state_handlers=self.has_state_handlers,
            )

            self.write_to_file(
                text=skill_app_rendered_code,
                filepath="F:/Inoft/skill_histoire_decryptage_1/app_generated.py"
            )