This repository has been archived on 2023-06-09. You can view files and clone it, but cannot push or open issues or pull requests.
jarvis-server/jarvis/skills/__init__.py

59 lines
2.0 KiB
Python
Raw Normal View History

import glob
import os
import types
from jarvis import get_path_file
from jarvis.skills import intent_manager
from jarvis.utils import languages_utils
class Skill:
2021-07-28 20:35:20 +02:00
def __init__(self, name):
self.name = name
2021-07-28 20:35:20 +02:00
path = self.__module__.split(".")
self.category = path[2]
self.skill_folder = path[3]
def register(self):
self.register_entities()
self.register_regex()
print("[" + self.name + "] Registered entity/entities and regex(s)")
def register_entities(self):
path = os.path.dirname(get_path_file.__file__) + "/skills/" + self.category + "/" + self.skill_folder
path = path + "/vocab/" + languages_utils.get_language() + "/*.voc"
files = glob.glob(path, recursive=True)
for file in files:
with open(file, "r") as infile:
for line in infile.readlines():
filename = file.split("/")[-1].split(".voc")[0]
intent_manager.register_entity(line.replace('\n', ''), filename, self.name)
def register_regex(self):
path = os.path.dirname(get_path_file.__file__) + "/skills/" + self.category + "/" + self.skill_folder
path = path + "/regex/" + languages_utils.get_language() + "/*.rx"
files = glob.glob(path, recursive=True)
for file in files:
with open(file, "r") as infile:
for line in infile.readlines():
intent_manager.register_regex(line.replace('\n', ''), self.name)
def speak(self, message):
print(message)
class SkillRegistering(type):
def __init__(cls, name, bases, attrs):
for key, val in attrs.items():
if type(val) is types.FunctionType and not str(val).__contains__("__"):
properties = getattr(val, "_register", None)
if properties is not None:
intent = properties[0]
intent_name = intent.name
intent_manager.intents_handlers[f"{intent_name}"] = [getattr(cls, key), name]