diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5e2126 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +**_aionlib_ - library support for [aion](https://github.com/blueShard-dev/aion)** + +- [Introduction](#introduction) +- [Installation](#installation) +- [Todo](#todo) +- [License](#license) + +# Introduction + +**aionlib** is the official library support for the [aion](https://github.com/blueShard-dev/aion_project) + +# Installation + +To install **aionlib** with pip3, type: + +``` +sudo pip3 install aionlib +``` + +If you want to install **aionlib** from github, type: + +``` +sudo git clone https://github.com/blueShard-dev/aionlib +cd aionlib +sudo python3 setup.py install +``` + +# Todo + +- [ ] tutorial for all classes / functions + +# License + +This project is licensed under the Mozilla Public Licence 2.0 (MPL-2.0) License - see the [LICENSE](License) file for more details diff --git a/aionlib/__init__.py b/aionlib/__init__.py new file mode 100644 index 0000000..1c3845c --- /dev/null +++ b/aionlib/__init__.py @@ -0,0 +1,36 @@ +#!/usr/bin/python3 + +__author__ = "blueShard" +__license__ = "GPL-3.0" +__version__ = "0.1.0" + +from ._utils import start_check as _start_check + +_start_check() + +is_aion = _start_check.is_aion +is_linux = _start_check.is_linux + +from ._utils import aion_data_path, aion_path + +from . import config, language, logging, plugin, utils, variable + + +def speech_output(speech_output: str) -> None: + """ + plays a output of an artificial voice from the given words + + :param speech_output: str + the words to be said + syntax: + example: "This is an test" + :return: None + + :since: 0.1.0 + """ + if is_aion: + from ._utils import import_aion_internal_file + return import_aion_internal_file("__init__").speech_output(speech_output) + else: + from ._utils import no_aion + no_aion() diff --git a/aionlib/_utils.py b/aionlib/_utils.py new file mode 100644 index 0000000..85478ad --- /dev/null +++ b/aionlib/_utils.py @@ -0,0 +1,881 @@ +#!/usr/bin/python3 + +from glob import glob as _glob + +from xml.dom import minidom as _minidom +import xml.etree.ElementTree as _ET + + +aion_data_path = "/etc/aion_data" +aion_path = "".join(_glob("/usr/local/aion-*")) + + +def import_aion_internal_file(fname: str): + """ + imports an file from the aion core + + :param fname: str + name of the file + syntax: + example: "version.py" + :return: a imported module from the aion core + + :since: 0.1.0 + """ + from . import is_aion + + if is_aion: + from importlib import import_module + from sys import path + path.insert(0, aion_path + "/aion_core") + return import_module(fname) + else: + no_aion() + + +def no_aion() -> None: + """ + a function which get called from some 'aionlib' functions if aion is not installed but required + + :return: None + + :since: 0.1.0 + """ + pass + + +def start_check() -> None: + """ + checks if system is linux and if aion is installed + + :return: None + + :since: 0.1.0 + """ + from colorama import Fore + from os.path import isdir + from platform import system + + if system().lower() != "linux": + print(Fore.RED + "It seems like you not using Linux (Raspbian on Raspberry Pi recommended). To use the whole library run this library on Linux (recommended Raspbian on Raspberry Pi)" + Fore.RESET) + start_check.is_linux = False + else: + start_check.is_linux = True + + if isdir(aion_path) is False: + print(Fore.RED + "It seems like you haven't installed aion. To use the whole library install aion" + Fore.RESET) + start_check.is_aion = False + else: + start_check.is_aion = True + + +class BaseXMLBuilder: + """ + a class to simple build a '.xml' file + + :since: 0.1.0 + """ + + def __init__(self, root_name: str = "root", **root_extra: str) -> None: + """ + :param root_name: str, optional + name of the root element of the xml file + syntax: + example: "root" + :param root_extra: kwargs, optional + attributes for the root element + syntax: = + example: author="blueShard" + :return: None + + :since: 0.1.0 + """ + self.root_name = root_name + + self._element_list = [self.root_name] + self._root = _ET.Element(root_name, **root_extra) + + def _prettify(self, string: str = None) -> str: + """ + prettifies the given string + + :param string: str + string to prettify + syntax: + example: "" + :return: str + returns the_prettified string + syntax: + example: " + + + " + + :since: 0.1.0 + """ + if string is None: + reparsed = _minidom.parseString(_ET.tostring(self._root, "utf-8")) + else: + reparsed = _minidom.parseString(bytes(string, "utf-8", errors="ignore")) + pre_output = reparsed.toprettyxml(indent=" ") + return "\n".join(pre_output.split("\n")[1:]) + + def create_root_element(self, name: str, text: str = None, attrib: dict = {}, **extra: str) -> None: + """ + creates a new entry as a sub element of the root element + + :param name: str + name of the new element + syntax: + example: "root_child" + :param text: str, optional + text of the new element + syntax: + example: "This is a root element" + :param attrib: dict, optional + attributes for the new element + syntax: {, } + example: {"author": "blueShard"} + :param extra: kwargs, optional + attributes for the new element + syntax: = + example: author="blueShard" + :return: None + + :since: 0.1.0 + """ + if text: + element = _ET.Element(name, attrib, **extra).text = text + else: + element = _ET.Element(name, attrib, **extra) + + self._root.append(element) + self._element_list.append(name) + + def create_sub_element(self, parent_name: str, name: str, text: str = None, attrib: dict = {}, parent_attrib: dict = None, **extra: str) -> None: + """ + creates a sub element of an parent element + + :param parent_name: str + name of the parent element to which the sub element should be added + syntax: + example: "root_child" + :param name: str + name of the new sub element you want to add + syntax: + example: "sub_child" + :param text: str, optional + text of the new sub element + syntax: + example: "This is a sub element" + :param attrib: dict, optional + attributes for the new element + syntax: {, } + example: {"author": "blueShard"} + :param parent_attrib: dict, optional + attributes of the new sub element + syntax: {: } + example: {"language": "en_US"} + :param extra: kwargs, optional + attributes of the new sub element + syntax: = + example: language="en_US" + :return: None + + :since: 0.1.0 + """ + + if parent_name in self._element_list: + for parent in self._root.iter(parent_name): + if parent_attrib: + if parent.attrib == parent_attrib: + if text: + _ET.SubElement(parent, name, attrib, **extra).text = text + else: + _ET.SubElement(parent, name, attrib, **extra) + self._element_list.append(name) + else: + if text: + _ET.SubElement(parent, name, attrib, **extra).text = text + else: + _ET.SubElement(parent, name, attrib, **extra) + self._element_list.append(name) + else: + raise IndexError("Couldn't find parent '" + parent_name + "'. The available parents are in this list: " + str(self._element_list)) + + def get_string(self, pretty_print: bool = True) -> str: + """ + get sting of the xml tree + + :param pretty_print: bool, optional + sets True or False if the xml tree string should be pretty printed + syntax: + example: True + :return: str + returns the string of the builded xml tree + syntax: + example: " + + This is a sub element + + " + + :since: 0.1.0 + """ + if pretty_print is True: + return self._prettify() + else: + return _ET.tostring(self._root, "utf-8").decode("ascii") + + def write(self, fname: str, mode: str = "w", pretty_print: bool = True) -> None: + """ + writes the xml tree to a file + + :param fname: str + filename of file you want to write + syntax: + example: "/home/pi/text.xml" + :param mode: str, optional + mode to write on file + syntax: + example: "w" + :param pretty_print: bool, optional + sets True or False if the xml tree string should be pretty printed + syntax: + example: True + :return: None + + :since: 0.1.0 + """ + with open(fname, mode=mode) as file: + file.write(self.get_string(pretty_print)) + file.close() + + +class BaseXMLReader: + + """ + a class to simple reead '.xml' file + + :since: 0.1.0 + """ + + def __init__(self, fname: str) -> None: + """ + makes the fname and auto_write available for all class methods and set all variables + + :param fname: str + filename of the file you want to read + syntax: + example: "/home/pi/test.xml" + :return: None + + :since: 0.1.0 + """ + self.fname = fname + + self._tree = _ET.parse(self.fname) + self._root = self._tree.getroot() + + self.get_infos._root = self._root + + def _prettify(self, string: str = None) -> str: + """ + prettifies the given string + + :param string: str + string to prettify + syntax: + example: "" + :return: str + returns the_prettified string + syntax: + example: " + + + " + + :since: 0.1.0 + """ + if string is None: + reparsed = _minidom.parseString(_ET.tostring(self._root, "utf-8")) + else: + reparsed = _minidom.parseString(bytes(string, "utf-8", errors="ignore")) + pre_output = reparsed.toprettyxml(indent=" ") + return "\n".join(pre_output.split("\n")[1:]) + + class get_infos(dict): + """ + a modified dict class with indexing items + + :since: 0.1.0 + """ + + def __init__(self, elem_tags: (str, list) = []) -> dict: + """ + get infos about an element in the file + + :param elem_tags: list + name of elements you want to get infos about + syntax: [] + example: ["sub_child"] + :return: dict + returns a dict of names from the given elements with a list of dictionaries of found elements (complex description xD) + syntax: {: [{"parent": {"tag": , "text": , "attrib": {}}, "childs": [], "tag": , "text": , "attrib": {}}]} + example: {"sub_child": [{"parent": {"tag": "root_child", "text": "", "attrib": {"author": "blueShard"}}, "childs": ["sub_child"], "tag": "sub_child", "text": "This is a sub element", "attrib": {}}]} + + :since: 0.1.0 + """ + if isinstance(elem_tags, str): + elem_tags = [elem_tags] + + child_list = [] + return_dict = {} + for elem in elem_tags: + if elem == "": + continue + elif elem == "": + return_dict[self._root.tag] = [] + else: + return_dict[elem] = [] + + all_child_list = [] + + if "" in elem_tags: + if self._root.tag in return_dict: + pass + else: + return_dict[self._root.tag] = [] + return_dict[self._root.tag].append( + {"parent": {"tag": "", "text": "", "attrib": {}}, "childs": [child.tag for child in self._root], "tag": self._root.tag, "text": "", "attrib": self._root.attrib}) + for root_child in self._root: + if root_child.tag in return_dict: + pass + else: + return_dict[root_child.tag] = [] + return_dict[root_child.tag].append( + {"parent": {"tag": self._root.tag, "text": self._root.text, "attrib": self._root.attrib}, "childs": [sub_root_child.tag for sub_root_child in root_child], + "tag": root_child.tag, "text": root_child.text, "attrib": root_child.attrib}) + all_child_list.append(root_child) + for parent in list(all_child_list): + for child in parent: + if child.tag in return_dict: + pass + else: + return_dict[child.tag] = [] + return_dict[child.tag].append( + {"parent": {"tag": parent.tag, "text": parent.text, "attrib": parent.attrib}, "childs": [sub_child.tag for sub_child in child], "tag": child.tag, "text": child.text, + "attrib": child.attrib}) + all_child_list.append(child) + if child in all_child_list: + all_child_list.remove(child) + else: + if self._root.tag in return_dict: + return_dict[self._root.tag].append({"parent": {}, "childs": [child.tag for child in self._root], "tag": self._root.tag, "text": "", "attrib": self._root.attrib}) + for root_child in self._root: + if root_child.tag in return_dict: + return_dict[root_child.tag].append( + {"parent": {"tag": self._root.tag, "text": self._root.text, "attrib": self._root.attrib}, "childs": [sub_root_child.tag for sub_root_child in root_child], + "tag": root_child.tag, "text": root_child.text, "attrib": root_child.attrib}) + else: + child_list.append(root_child) + for parent in list(child_list): + for child in parent: + if child.tag in return_dict: + return_dict[child.tag].append( + {"parent": {"tag": parent.tag, "text": parent.text, "attrib": parent.attrib}, "childs": [sub_child.tag for sub_child in child], "tag": child.tag, "text": child.text, + "attrib": child.attrib}) + else: + child_list.append(child) + if child in child_list: + child_list.remove(child) + + self._return_dict = return_dict + + self.items._return_dict_keys = return_dict.keys() + self.items._return_dict_values = return_dict.values() + self.keys._return_dict_keys = return_dict.keys() + self.values._return_dict_values = return_dict.values() + + super().__init__(self._return_dict) + + def __iter__(self): + return iter(self._return_dict) + + def __next__(self): + return self._return_dict + + def __repr__(self): + return self._return_dict + + def __str__(self): + return str(self._return_dict) + + def index(self, index: int) -> dict: + """ + index a key-value pair in a dict + + :param index: int + index of the key-value pair you want to get + syntax: + example: 5 + :return: dict + returns the key-value pair of the given index + syntax: {: } + example: {"test_key": "test_value"} + + :since: 0.1.0 + """ + i = 0 + for key, value in self._return_dict.items(): + if i == index: + return {key: value} + else: + i += 1 + raise IndexError("dict index out of range") + + class items: + """ + a modified items() function from dict with indexing items + + :since: 0.1.0 + """ + + def __init__(self): + pass + + def __getitem__(self, item): + return tuple(self._return_dict_items)[item] + + def __iter__(self): + return iter(self._return_dict_items) + + def __len__(self): + return len(self._return_dict_items) + + def __next__(self): + return self._return_dict_items + + def __repr__(self): + return self._return_dict_items + + def __str__(self): + return str(self._return_dict_items) + + def index(self, index: int): + """ + index a key-value pair in a dict + + :param index: int + index of the key-value pair you want to get + syntax: + example: 5 + :return: the given index in the values + + :since: 0.1.0 + """ + return {list(self._return_dict_keys)[index]: list(self._return_dict_values)[index]} + + class keys: + """ + a modified keys() function from dict with indexing items + + :since: 0.1.0 + """ + + def __init__(self): + pass + + def __iter__(self): + return iter(self._return_dict_keys) + + def __len__(self): + return len(list(self._return_dict_keys)) + + def __next__(self): + return self._return_dict_keys + + def __repr__(self): + return self._return_dict_keys + + def __str__(self): + return str(self._return_dict_keys) + + def index(self, index: int): + """ + index a key in a dict + + :param index: int + index of the key you want to get + syntax: + example: 5 + :return: the given index in the keys + + :since: 0.1.0 + """ + return list(self._return_dict_keys)[index] + + class values: + """ + a modified values() function from dict with indexing items + + :since: 0.1.0 + """ + + def __init__(self): + pass + + def __iter__(self): + return iter(self._return_dict_values) + + def __len__(self): + return len(list(self._return_dict_values)) + + def __next__(self): + return self._return_dict_values + + def __repr__(self): + return self._return_dict_values + + def __str__(self): + return str(self._return_dict_values) + + def index(self, index: int): + """ + index a value in a dict + + :param index: int + index of the value you want to get + syntax: + example: 5 + :return: the given index in the values + + :since: 0.1.0 + """ + return list(self._return_dict_values)[index] + + def get_string(self, pretty_print: bool = True) -> str: + """ + gets the string of the xml tree in the file + + :param pretty_print: bool, optional + sets True or False if the xml tree string should be pretty printed + syntax: + example: True + :return: str + returns the string of the xml tree + syntax: + example: " + + This is a sub element + + " + + :since: 0.1.0 + """ + string = _ET.tostring(self._root, "utf-8").decode("ascii") + if pretty_print is True: + if "\n" in string: + return string + else: + return self._prettify() + else: + if "\n" in string: + return "".join([line.strip() for line in _ET.tostring(self._root, "utf-8").decode("ascii").split("\n")]) + else: + return string + + +class BaseXMLWriter: + """ + a class to simple change/write a '.xml' file + + :since: 0.1.0 + """ + + def __init__(self, fname: str, auto_write: bool = False) -> None: + """ + :param fname: str + filename of the file you want to write to + syntax: + example: "/home/pi/test.xml" + :param auto_write: bool, optional + sets if after every change to the getted xml tree the changes should be write to the file + syntax: + example: False + :return: None + + :since: 0.1.0 + """ + self.auto_write = auto_write + self.fname = fname + + self._root = _ET.fromstring("".join([item.replace("\n", "").strip() for item in [line for line in open(self.fname, "r")]])) + + def _prettify(self, string: str = None) -> str: + """ + prettifies the given string + + :param string: str + string to prettify + syntax: + example: "" + + :return: str + returns the_prettified string + syntax: + example: " + + + " + + :since: 0.1.0 + """ + if string is None: + reparsed = _minidom.parseString(_ET.tostring(self._root, "utf-8")) + else: + reparsed = _minidom.parseString(string) + pre_output = reparsed.toprettyxml(indent=" ") + return "\n".join(pre_output.split("\n")[1:]) + + def add(self, parent_tag: str, elem_tag: str, text: str = None, attrib: dict = {}, parent_attrib: dict = None, **extra: str) -> None: + """ + adds an element to xml tree + + :param parent_tag : str + name of the parent element + syntax: + example: "root_child" + :param elem_tag : str + name of the element you want to add + syntax: + example: "second_sub_child" + :param text : str, optional + text of the element you want to add + syntax: + example: "This is the second sub child" + :param attrib : dict + attributes for the new element + syntax: {, } + example: {"author": "blueShard"} + :param parent_attrib : dict, optional + attributes of the parent element + syntax: {: } + example: {"author": "blueShard"} + :param extra : kwargs, optional + attributes of the new element + syntax: = + example: language="de_DE" + :return: None + + :since: 0.1.0 + """ + if parent_tag == "": + parent_tag = self._root.tag + + if parent_tag == self._root.tag: + if parent_attrib: + if parent_attrib == self._root.attrib: + if text: + root_text_element = _ET.Element(elem_tag, attrib, **extra) + root_text_element.text = text + self._root.append(root_text_element) + else: + self._root.append(_ET.Element(elem_tag, attrib, **extra)) + else: + if text: + root_text_element = _ET.Element(elem_tag, attrib, **extra) + root_text_element.text = text + self._root.append(root_text_element) + else: + self._root.append(_ET.Element(elem_tag, attrib, **extra)) + else: + for parent in self._root.iter(parent_tag): + if parent_attrib: + if parent.attrib == parent_attrib: + if text: + _ET.SubElement(parent, elem_tag).text = text + else: + _ET.SubElement(parent, elem_tag, attrib, **extra) + else: + if text: + _ET.SubElement(parent, elem_tag).text = text + else: + _ET.SubElement(parent, elem_tag, attrib, **extra) + + if self.auto_write is True: + self.write() + + def get_string(self, pretty_print: bool = False) -> str: + """ + gets the string of the xml tree in the file + + :param pretty_print: bool, optional + sets True or False if the xml tree string should be pretty printed + syntax: + example: True + :return: str + returns the string of the xml tree + syntax: + example: " + + This is a sub element + + + " + + :since: 0.1.0 + """ + string = _ET.tostring(self._root, "utf-8").decode("ascii") + if pretty_print is True: + if "\n" in string: + return string + else: + return self._prettify() + else: + if "\n" in string: + return "".join([line.strip() for line in _ET.tostring(self._root, "utf-8").decode("ascii").split("\n")]) + else: + return string + + def remove(self, parent_tag: str, elem_tag: str, parent_attrib: dict = None) -> None: + """ + removes an element from the xml tree + + :param parent_tag : str + name of the parent element + syntax: + example: "root_child" + :param elem_tag : str + name of the element you want to remove + syntax: + example: "second_sub_child" + :param parent_attrib : dict, optional + attributes of the parent element + syntax: {: } + example: {"author": "blueShard"} + :return: None + + :since: 0.1.0 + """ + if parent_tag == "": + parent_tag = self._root.tag + + if parent_tag == self._root.tag: + for child in self._root: + if child.tag == elem_tag: + if parent_attrib: + if self._root.attrib == parent_attrib: + self._root.remove(child) + else: + self._root.remove(child) + + for parent in self._root.iter(parent_tag): + for child in parent: + if child.tag == elem_tag: + if parent_attrib: + if parent.attrib == parent_attrib: + parent.remove(child) + else: + parent.remove(child) + + if self.auto_write is True: + self.write() + + def update(self, parent_tag: str, elem_tag: str, text: str = None, attrib: dict = {}, parent_attrib: dict = None, **extra: str) -> None: + """ + updates an element in the xml tree + + :param parent_tag : str + name of the parent element + syntax: + example: "root_child" + :param elem_tag : str + name of the element you want to update + syntax: + example: "second_sub_child" + :param text : str, optional + new text of the updated element + syntax: + example: "New text of the second sub child" + :param attrib : dict + attributes for the new element + syntax: {, } + example: {"author": "blueShard"} + :param parent_attrib : dict, optional + attributes of the parent element + syntax: {: } + example: {"author": "blueShard"} + :param extra : kwargs, optional + new attributes of the updated element + syntax: = + example: language="de_DE" + :return: None + + :since: 0.1.0 + """ + if parent_tag == "": + parent_tag = self._root.tag + + if parent_tag == self._root.tag: + for child in self._root: + if child.tag == elem_tag: + if parent_attrib: + if self._root.attrib == parent_attrib: + if text: + child.text = str(text) + for key, value in attrib.items(): + child.set(str(key), str(value)) + for key, value in extra.items(): + child.set(key, str(value)) + else: + if text: + child.text = str(text) + for key, value in attrib.items(): + child.set(str(key), str(value)) + for key, value in extra.items(): + child.set(key, str(value)) + for parent in self._root.iter(parent_tag): + for child in parent: + if child.tag == elem_tag: + if parent_attrib: + if parent.attrib == parent_attrib: + if text: + child.text = str(text) + for key, value in attrib.items(): + child.set(str(key), str(value)) + for key, value in extra.items(): + child.set(key, str(value)) + else: + if text: + child.text = str(text) + for key, value in attrib.items(): + child.set(str(key), str(value)) + for key, value in extra.items(): + child.set(key, str(value)) + + if self.auto_write is True: + self.write() + + def write(self, mode: str = "w", pretty_print: bool = True) -> None: + """ + writes the xml tree to a file + + :param mode : str, optional + mode to write on file + syntax: + example: "w" + :param pretty_print : bool, optional + sets True or False if the xml tree string should be pretty printed + syntax: + example: True + :return: None + + :since: 0.1.0 + """ + with open(self.fname, mode=mode) as file: + if pretty_print is False: + file.write(_ET.tostring(self._root, "utf-8").decode("ascii")) + else: + file.write(self._prettify()) + file.close() diff --git a/aionlib/acph.py b/aionlib/acph.py new file mode 100644 index 0000000..f478812 --- /dev/null +++ b/aionlib/acph.py @@ -0,0 +1,125 @@ +#!/usr/bin/python3 + + +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: + example: "/home/pi/test.acph" + :param skill: str + skill name to which the acph belongs + syntax: "" + example: "test_skill" + :param acph_dict: dict, optional + defines a word or a sentence from which a method is called + syntax: {: } + 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("", acph, skill=skill, method=method) + acph_writer.write() + + +def create_acph_file(language_locale: str, skill_acph_dict_dict: dict = {}) -> None: + """ + creates a new '.acph' file for given language locale with given skill_acph_dict_dict + + :param language_locale: str + language locale of language from which the new file is to be created + syntax: + example: "en_US" + :param skill_acph_dict_dict: dict, optional + skill name you want to add specific entries + syntax: {: {: }} + example: {"test_skill": {"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 .utils import BaseXMLBuilder + except ImportError: + from utils import BaseXMLBuilder + + acph_builder = BaseXMLBuilder(language_locale) + for skill, acph_dict in skill_acph_dict_dict.items(): + for acph, method in acph_dict.items(): + acph_builder.create_root_element(acph, skill=skill, method=method) + + acph_builder.write(language_locale + ".acph") + + +def delete_acph(fname: str, acph_list: list = []) -> None: + """ + deletes entries from '.acph' + + :param fname: str + file from which the activate phases is being deleted + syntax: + example: "/home/pi/test.acph" + :param acph_list: list, optional + name of the activate phases you want to remove + syntax: [] + example: ["test_acph"] + :return: None + + :since: 0.1.0 + """ + try: + from .utils import BaseXMLWriter + except ImportError: + from utils import BaseXMLWriter + + acph_writer = BaseXMLWriter(fname) + for item in acph_list: + acph_writer.remove("", str(item)) + acph_writer.write() + + +def exist_acph(fname: str, acph: str) -> bool: + """ + checks if a entry exist + + :param fname: str + file from which the activate phrase should be search + syntax: + example: "en_US" + :param acph: str + activate phrase you want to check if exists + syntax: + example: "start test" + :return: bool + returns True if acph exist / False if not + syntax: + example: False + + :since: 0.1.0 + """ + try: + from .utils import BaseXMLReader + except ImportError: + from utils import BaseXMLReader + + acph = acph.replace(" ", "_") + + acph_reader = BaseXMLReader(fname) + for item in acph_reader.get_infos([""]).items().index(0): + if acph in item["childs"]: + return True + else: + return False diff --git a/aionlib/config.py b/aionlib/config.py new file mode 100644 index 0000000..bb1083c --- /dev/null +++ b/aionlib/config.py @@ -0,0 +1,373 @@ +#!/usr/bin/python3 + +from . import is_aion +from ._utils import no_aion + +if is_aion: + from ._utils import import_aion_internal_file as _import_aion_internal_files + _config = _import_aion_internal_files("config") + + +class Aion: + + def __init__(self) -> None: + if is_aion: + self._aion_config = _config.Aion() + + self.all_listening_modes = ["auto", "manual"] + self.all_stt_engines = ["google", "pocketsphinx"] + self.all_time_formats = ["12", "24"] + self.all_tts_engines = ["pico2wave", "espeak"] + self.supported_languages = ["de_DE", "en_US"] + + def get_hotword_file(self) -> str: + """ + get set hotword file path + + :return: str + returns path of the hotword file + syntax: + example: "/usr/local/aion-/etc/Aion.pmdl" + + :since: 0.1.0 + """ + if is_aion: + return self._aion_config.get_hotword_file() + else: + no_aion() + + def get_language(self) -> str: + """ + get sett language locale + + :return: str + returns language locale + syntax: + example: "en_US" + + :since: 0.1.0 + """ + if is_aion: + return self._aion_config.get_language() + else: + no_aion() + + def get_listening_mode(self) -> str: + """ + get set listening mode + + :return: str + returns listening mode + syntax: + example: "auto" + + :since: 0.1.0 + """ + if is_aion: + return self._aion_config.get_listening_mode() + else: + no_aion() + + def get_pid_manipulation_number(self) -> int: + """ + get set pid manipulation number + + :return: int + returns the pid manipulation number + syntax: + example: 4 + + :since: 0.1.0 + """ + if is_aion: + return int(self._aion_config.get_pid_manipulation_number()) + else: + no_aion() + + def get_stt_engine(self) -> str: + """ + get set speech-to-text engine + + :return: str + returns speech-to-text engine + syntax: + example: "google" + + :since: 0.1.0 + """ + if is_aion: + return self._aion_config.get_stt_engine() + else: + no_aion() + + def get_time_format(self) -> int: + """ + get set time format + + :return: str + returns time format + syntax: