Beispiel #1
0
def delete_entry(language_locale: str,
                 skill: str,
                 entry_list: list = []) -> None:
    """
    deletes entries from '<language_locale>.lng'

    :param language_locale: str
        language locale from (file) which the entry is being deleted
        syntax: <language locale>
        example: "en_US"
    :param skill : str
        name of the skill from which the entries should be deleted
        syntax: <skill name>
        example: "test"
    :param entry_list: list, optional
        name of the entries you want to remove
        syntax: [<entry name>]
        example: ["test_entry"]
    :return: None

    :since: 0.1.0
    """
    try:
        from .utils import BaseXMLWriter
    except ImportError:
        from utils import BaseXMLWriter

    lng_writer = BaseXMLWriter(language_directory + "/" + language_locale +
                               ".lng")
    for item in entry_list:
        lng_writer.remove("<root>", str(skill) + "." + str(item))
    lng_writer.write()
Beispiel #2
0
def delete_acph(language_locale: str, acph_list: list = []) -> None:
    """
    deletes entries from '<language_locale>.acph'

    :param language_locale: str
        language locale from (file) which the activate phases is being deleted
        syntax: <language locale>
        example: "en_US"
    :param acph_list: list, optional
        name of the activate phases you want to remove
        syntax: [<activate phase name>]
        example: ["test_acph"]
    :return: None

    :since: 0.1.0
    """
    try:
        from .utils import BaseXMLWriter
    except ImportError:
        from utils import BaseXMLWriter

    acph_writer = BaseXMLWriter(acph_directory + "/" + language_locale +
                                ".acph")
    for item in acph_list:
        acph_writer.remove("<root>", str(item))
    acph_writer.write()
Beispiel #3
0
def add_acph(fname: str, skill: str, acph_dict: dict = {}) -> None:
    """
    adds an new entry(s) to from argument 'language_locale' given language

    :param fname: str
        name of the file where the activate phrases should be added
        syntax: <file name>
        example: "/home/pi/test.acph"
    :param skill: str
        skill name to which the acph belongs
        syntax: "<skill name>"
        example: "test_skill"
    :param acph_dict: dict, optional
        defines a word or a sentence from which a method is called
        syntax: {<activate phrase>: <method that should get called after the activate phrase was said>}
        example: {"start test": "MyTestMethod"}
        NOTE: in key 'activate_phrase' you can use the '__and__' statement. This checks if the words before and after '__and__' are in the sentence that the user has spoken in

    :since: 0.1.0
    """
    try:
        from .utils import BaseXMLWriter
    except ImportError:
        from utils import BaseXMLWriter

    acph_writer = BaseXMLWriter(fname)
    for acph, method in acph_dict:
        if exist_acph(fname, acph):
            raise IndexError("the activate phrase " + acph + " already exist")
        acph_writer.add("<root>", acph, skill=skill, method=method)
    acph_writer.write()
Beispiel #4
0
def add_entry(language_locale: str, skill: str, entry_dict: dict = {}) -> None:
    """
    adds an new entry(s) to from argument 'language_locale' given language

    :param language_locale: str
        language locale from the language to which the entry(s) is/are to be added
        syntax: <language locale>
        example: "de_DE"
    :param skill: str
        skill name to which the entry belongs
        syntax: "<skill name>"
        example: "test_skill"
    :param entry_dict: dict, optional
        all texts for execution of a function
        syntax: {<entry name>: <text of your entry>}
        example: {"test_entry": "Test function was executed correctly"}
    :return: None

    :since: 0.1.0
    """
    try:
        from ._error_codes import language_lng_file_doesnt_exist, language_entry_already_exist
    except ImportError:
        from _error_codes import language_lng_file_doesnt_exist, language_entry_already_exist

    from os.path import isfile

    lng_file = language_directory + "/" + language_locale + ".lng"
    if isfile(lng_file) is False:
        raise FileNotFoundError("Errno: " + language_lng_file_doesnt_exist +
                                " - The file " + lng_file + " doesn't exist")
    try:
        from .utils import BaseXMLWriter
    except ImportError:
        from utils import BaseXMLWriter

    lng_adder = BaseXMLWriter(lng_file)
    for entry, text in entry_dict.items():
        if exist_entry(language_locale, skill, entry) is True:
            raise IndexError("Errno: " + language_entry_already_exist +
                             " - The entry " + entry + " already exist")
        lng_adder.add("<root>", skill + "." + str(entry), text=str(text))
    lng_adder.write()
Beispiel #5
0
def remove_plugin(
    plugin_name: str, plugin_type: (RUN_AFTER, RUN_BEFORE)) -> None:
    """
    removes given plugin

    :param plugin_name: str
        name of the plugin you want to remove
        syntax: <plugin name>
        example: "test"
    :param plugin_type: (RUN_AFTER, RUN_BEFORE)
        type of the plugin
        syntax: <plugin type>
        example: RUN_AFTER

    :since: 0.1.0
    """
    try:
        from .language import language_directory, delete_entry
        from .utils import BaseXMLReader, BaseXMLWriter
    except ImportError:
        from language import language_directory, delete_entry
        from utils import BaseXMLReader, BaseXMLWriter

    from ast import literal_eval
    from os import remove as remove
    from shutil import rmtree

    if plugin_type == RUN_AFTER:
        plugin_infos = get_run_after_plugin_infos(plugin_name)
        remove(run_after_path + "/" + plugin_infos["main_file"])
        for dir in plugin_infos["additional_directories"]:
            rmtree(run_after_path + "/" + dir)
        for language in plugin_infos["language_locales"]:
            delete_entry(
                language,
                plugin_name,
                entry_list=[
                    key.replace(" ", "_")
                    for key in literal_eval(plugin_infos["language_dict"])
                ])
        remove_xml = BaseXMLWriter(run_after_file)
        remove_xml.remove(plugin_infos["method"], plugin_name)
        remove_xml.write()
Beispiel #6
0
def add_acph(language_locale: str, skill: str, acph_dict: dict = {}) -> None:
    """
    adds an new entry(s) to from argument 'language_locale' given language

    :param language_locale: str
        language locale from the language to which the entry(s) is/are to be added
        syntax: <language locale>
        example: "de_DE"
    :param skill: str
        skill name to which the acph belongs
        syntax: "<skill name>"
        example: "test_skill"
    :param acph_dict: dict, optional
        defines a word or a sentence from which a method is called
        syntax: {<activate phrase>: <method that should get called after the activate phrase was said>}
        example: {"start test": "MyTestMethod"}
        NOTE: in key 'activate_phrase' you can use the '__and__' statement. This checks if the words before and after '__and__' are in the sentence that the user has spoken in
    :return: None

    :since: 0.1.0
    """
    try:
        from ._error_codes import acph_activate_phrase_exist
        from .utils import BaseXMLWriter
    except ImportError:
        from _error_codes import acph_activate_phrase_exist
        from utils import BaseXMLWriter

    acph_writer = BaseXMLWriter(acph_directory + "/" + language_locale +
                                ".acph")
    for acph, method in acph_dict:
        if exist_acph(language_locale, acph):
            raise IndexError("Errno: " + acph_activate_phrase_exist +
                             " - The activate phrase " + acph +
                             " already exist")
        acph_writer.add("<root>", acph, skill=skill, method=method)
    acph_writer.write()
Beispiel #7
0
def execute_aion_file_type_plugin(fname: str) -> None:
    """
    installs custom plugin from '<file name>.aion'

    :param fname: str
        file name of the '.aion' file
        syntax: <file name>
        example: "/home/pi/plugin.aion"
    :return: None

    :since: 0.1.0
    """
    try:
        from ._errors import plugin_couldnt_find_key, plugin_couldnt_find_plugin_type, plugin_illegal_run_after_pseudonym, plugin_run_after_pseudonym_already_exist,\
            plugin_illegal_run_before_pseudonym, plugin_run_before_pseudonym_already_exist, plugin_file_doesnt_exist_in_setup_dir, plugin_directory_doesnt_exist_in_setup_dir,\
            plugin_run_after_additional_directories_already_exist, plugin_run_before_additional_directories_already_exist, plugin_python3_module_not_found
        from .acph import add_acph
        from .language import add_entry, language_directory
        from .utils import remove_space, BaseXMLReader, BaseXMLWriter
    except ImportError:
        from _error_codes import plugin_couldnt_find_key, plugin_couldnt_find_plugin_type, plugin_illegal_run_after_pseudonym, plugin_run_after_pseudonym_already_exist,\
            plugin_illegal_run_before_pseudonym, plugin_run_before_pseudonym_already_exist, plugin_file_doesnt_exist_in_setup_dir, plugin_directory_doesnt_exist_in_setup_dir,\
            plugin_run_after_additional_directories_already_exist, plugin_run_before_additional_directories_already_exist, plugin_python3_module_not_found
        from acph import add_acph
        from language import add_entry, language_directory
        from utils import remove_space, BaseXMLReader, BaseXMLWriter

    from ast import literal_eval
    from importlib import import_module
    from os import listdir, path
    from shutil import copy, copytree
    from subprocess import call
    setup_dict = {}
    setup_dir = path.dirname(path.abspath(fname))

    plugin_type = ""
    is_skill = True

    for line in open(fname, "r"):
        if line.startswith("#"):
            no_space_line = remove_space(line, "")
            if no_space_line == "#type:run_after_plugin":
                plugin_type = RUN_AFTER
            elif no_space_line == "#type:run_before_plugin":
                plugin_type = RUN_BEFORE
            continue

        setup_dict[line.split("=")[0].strip()] = line.split("=")[1].strip()
    try:
        author = setup_dict["author"]
        main_file = setup_dict["main_file"]
        plugin_name = setup_dict["plugin_name"]
        skill = setup_dict["skill"]
        plugin_methods = literal_eval(setup_dict["plugin_methods"])
        version = setup_dict["version"]

        additional_directories = literal_eval(
            setup_dict["additional_directories"])
        description = setup_dict["description"]
        language_locales = literal_eval(setup_dict["language_locales"])
        language_dict = literal_eval(setup_dict["language_dict"])
        license = setup_dict["license"]
        required_python3_packages = literal_eval(
            setup_dict["required_python3_packages"])
    except KeyError as error:
        raise KeyError("Errno: " + plugin_couldnt_find_key +
                       " - Couldn't find key " + str(error) + " in " + fname)

    if plugin_type != RUN_AFTER or plugin_type != RUN_BEFORE:
        raise NameError(
            "Errno: " + plugin_couldnt_find_plugin_type +
            " - Couldn't find plugin type ('RUN_AFTER' or 'RUN_BEFORE') in " +
            fname)

    # ----- #

    if plugin_type == RUN_AFTER:
        for pseudonym in plugin_methods:
            if pseudonym == "run_after":
                raise NameError("Errno: " +
                                plugin_illegal_run_after_pseudonym +
                                " - Illegal name '" + pseudonym + "' in " +
                                str(plugin_methods))
            try:
                if pseudonym in get_all_run_after_plugins()[skill]:
                    raise NameError("Errno: " +
                                    plugin_run_before_pseudonym_already_exist +
                                    " - The plugin pseudonym " + plugin_name +
                                    " already exist")
            except KeyError:
                is_skill = False
                pass

    elif plugin_type == RUN_BEFORE:
        for pseudonym in plugin_methods:
            if pseudonym == "run_before":
                raise NameError("Errno: " +
                                plugin_illegal_run_before_pseudonym +
                                " - Illegal name '" + pseudonym + "' in " +
                                str(plugin_methods))
            try:
                if pseudonym in get_all_run_before_plugins()[skill]:
                    raise NameError("Errno: " +
                                    plugin_run_after_pseudonym_already_exist +
                                    " - The plugin pseudonym " + plugin_name +
                                    " already exist")
            except KeyError:
                is_skill = False
                pass

    if path.isfile(setup_dir + "/" + main_file) is False:
        raise FileNotFoundError("Errno: " +
                                plugin_file_doesnt_exist_in_setup_dir +
                                " - The file " + main_file +
                                " doesn't exist in setup dir (" + setup_dir +
                                ")")

    for directory in additional_directories:
        if path.isdir(setup_dir + "/" + directory) is False:
            raise NotADirectoryError(
                "Errno: " + plugin_directory_doesnt_exist_in_setup_dir +
                " - The directory " + directory +
                " doesn't exist in setup dir (" + setup_dir + ")")
        if plugin_type == RUN_AFTER:
            if directory in listdir(run_after_path):
                raise IsADirectoryError(
                    "Errno: " +
                    plugin_run_after_additional_directories_already_exist +
                    " - The directory " + directory + " already exist in " +
                    run_after_path)
        elif plugin_type == RUN_BEFORE:
            if directory in listdir(run_before_path):
                raise IsADirectoryError(
                    "Errno: " +
                    plugin_run_before_additional_directories_already_exist +
                    " - The directory " + directory + " already exist in " +
                    run_before_path)

    # ----- #

    for package in required_python3_packages:
        call("pip3 install " + package, shell=True)
        try:
            import_module(package)
        except ModuleNotFoundError:
            raise ModuleNotFoundError(
                "Errno: " + plugin_python3_module_not_found +
                " - Couldn't install the required python3 package '" +
                package + "'")

    dat_writer = None

    if plugin_type == RUN_AFTER:
        copy(main_file, run_after_path + "/" + main_file)

        for directory in additional_directories:
            copytree(directory, run_after_path + "/" + directory)

        for language in language_locales:
            add_entry(language, plugin_name, language_dict)

        dat_writer = BaseXMLWriter(run_after_file)

        if is_skill is False:
            dat_writer.add("run_after", skill, type="skill")
            dat_writer.write()

    elif plugin_type == RUN_BEFORE:
        copy(main_file, run_after_path + "/" + main_file)

        for directory in additional_directories:
            copytree(directory, run_after_path + "/" + directory)

        for language in language_locales:
            add_entry(language, plugin_name, language_dict)

        dat_writer = BaseXMLWriter(run_before_file)

        if is_skill is False:
            dat_writer.add("run_before", skill, type="skill")
            dat_writer.write()

    for pseudonym, method in plugin_methods.values():
        dat_writer.add(skill,
                       str(pseudonym),
                       parent_attrib={"type": "skill"},
                       method=method,
                       root_plugin=plugin_name)
        dat_writer.add(str(pseudonym), "additional_directories",
                       str(additional_directories))
        dat_writer.add(str(pseudonym), "author", str(author))
        dat_writer.add(str(pseudonym), "description", str(description))
        dat_writer.add(str(pseudonym), "language_dict", str(language_dict))
        dat_writer.add(str(pseudonym), "language_locales",
                       str(language_locales))
        dat_writer.add(str(pseudonym), "license", str(license))
        dat_writer.add(str(pseudonym), "main_file", str(main_file))
        dat_writer.add(str(pseudonym), "required_python3_packages",
                       str(required_python3_packages))
        dat_writer.add(str(pseudonym), "version", str(version))
    dat_writer.write()