2019-02-10 15:26:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
from json import loads, dumps
|
2019-03-02 07:54:25 +03:00
|
|
|
from os import path, environ
|
2019-02-10 15:26:50 +03:00
|
|
|
from pathlib import Path
|
|
|
|
from random import choice
|
|
|
|
from sys import argv, stdout
|
|
|
|
from vars import useragent
|
|
|
|
from tinydb import TinyDB, Query, operations
|
|
|
|
from time import sleep
|
|
|
|
import sqlite3
|
|
|
|
import requests
|
2019-03-24 03:17:40 +03:00
|
|
|
import re
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
dirname = path.dirname(path.realpath(__file__))
|
2019-03-02 07:54:25 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
intent_object_path = argv[1]
|
2019-03-28 03:45:37 +03:00
|
|
|
codes = []
|
2019-03-02 07:54:25 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
intent_obj_file = open(intent_object_path, 'r', encoding = 'utf8')
|
|
|
|
intent_obj = loads(intent_obj_file.read())
|
|
|
|
intent_obj_file.close()
|
2019-03-02 07:54:25 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
def get_intent_obj():
|
|
|
|
"""Return intent object"""
|
2019-03-02 07:54:25 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
return intent_obj
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
def translate(key, d = { }):
|
|
|
|
"""Pickup the language file according to the cmd arg
|
2022-02-14 18:06:37 +03:00
|
|
|
and return the value according to the params"""
|
2019-02-10 15:26:50 +03:00
|
|
|
|
|
|
|
output = ''
|
|
|
|
|
2022-02-15 17:26:06 +03:00
|
|
|
file = open(path.join(dirname, '../../skills', intent_obj['domain'], intent_obj['skill'], 'nlu', intent_obj['lang'] + '.json'), 'r', encoding = 'utf8')
|
2019-02-10 15:26:50 +03:00
|
|
|
obj = loads(file.read())
|
|
|
|
file.close()
|
|
|
|
|
2022-02-15 17:26:06 +03:00
|
|
|
prop = obj['answers'][key]
|
2019-02-10 15:26:50 +03:00
|
|
|
if isinstance(prop, list):
|
|
|
|
output = choice(prop)
|
|
|
|
else:
|
|
|
|
output = prop
|
|
|
|
|
|
|
|
if d:
|
|
|
|
for k in d:
|
|
|
|
output = output.replace('%' + k + '%', str(d[k]))
|
|
|
|
|
|
|
|
# "Temporize" for the data buffer ouput on the core
|
|
|
|
sleep(0.1)
|
2019-03-28 03:45:37 +03:00
|
|
|
|
2019-02-10 15:26:50 +03:00
|
|
|
return output
|
|
|
|
|
2022-05-05 16:11:48 +03:00
|
|
|
def output(type, code, speech = '', core = { }):
|
2022-02-14 18:06:37 +03:00
|
|
|
"""Communicate with the core"""
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2019-03-28 03:45:37 +03:00
|
|
|
codes.append(code)
|
|
|
|
|
2019-02-10 15:26:50 +03:00
|
|
|
print(dumps({
|
2022-02-14 18:06:37 +03:00
|
|
|
'domain': intent_obj['domain'],
|
|
|
|
'skill': intent_obj['skill'],
|
2022-02-10 16:47:43 +03:00
|
|
|
'action': intent_obj['action'],
|
|
|
|
'lang': intent_obj['lang'],
|
|
|
|
'utterance': intent_obj['utterance'],
|
|
|
|
'entities': intent_obj['entities'],
|
2019-02-10 15:26:50 +03:00
|
|
|
'output': {
|
|
|
|
'type': type,
|
2019-03-28 03:45:37 +03:00
|
|
|
'codes': codes,
|
2019-02-10 15:26:50 +03:00
|
|
|
'speech': speech,
|
2022-05-05 16:11:48 +03:00
|
|
|
'core': core,
|
2019-02-10 15:26:50 +03:00
|
|
|
'options': config('options')
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
if (type == 'inter'):
|
|
|
|
stdout.flush()
|
|
|
|
|
2019-04-07 07:28:22 +03:00
|
|
|
def http(method, url, headers = None):
|
2019-02-10 15:26:50 +03:00
|
|
|
"""Send HTTP request with the Leon user agent"""
|
|
|
|
|
|
|
|
session = requests.Session()
|
|
|
|
session.headers.update({ 'User-Agent': useragent, 'Cache-Control': 'no-cache' })
|
|
|
|
|
2019-04-07 07:28:22 +03:00
|
|
|
if headers != None:
|
|
|
|
session.headers.update(headers)
|
|
|
|
|
2019-02-10 15:26:50 +03:00
|
|
|
return session.request(method, url)
|
|
|
|
|
|
|
|
def config(key):
|
2022-02-14 18:06:37 +03:00
|
|
|
"""Get a skill configuration value"""
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-14 18:06:37 +03:00
|
|
|
file = open(path.join(dirname, '../../skills', intent_obj['domain'], intent_obj['skill'], 'src/config.json'), 'r', encoding = 'utf8')
|
2019-02-10 15:26:50 +03:00
|
|
|
obj = loads(file.read())
|
|
|
|
file.close()
|
|
|
|
|
2022-02-15 17:26:06 +03:00
|
|
|
return obj['configurations'][key]
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
def create_dl_dir():
|
2022-02-14 18:06:37 +03:00
|
|
|
"""Create the downloads folder of a current skill"""
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
dl_dir = path.dirname(path.realpath(__file__)) + '/../../downloads/'
|
2022-02-14 18:06:37 +03:00
|
|
|
skill_dl_dir = path.join(dl_dir, intent_obj['domain'], intent_obj['skill'])
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-14 18:06:37 +03:00
|
|
|
Path(skill_dl_dir).mkdir(parents = True, exist_ok = True)
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-14 18:06:37 +03:00
|
|
|
return skill_dl_dir
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
def db(db_type = 'tinydb'):
|
2019-02-10 15:26:50 +03:00
|
|
|
"""Create a new dedicated database
|
2022-02-14 18:06:37 +03:00
|
|
|
for a specific skill"""
|
2019-02-10 15:26:50 +03:00
|
|
|
|
2022-02-10 16:47:43 +03:00
|
|
|
if db_type == 'tinydb':
|
2019-06-06 18:15:33 +03:00
|
|
|
ext = '.json' if environ.get('LEON_NODE_ENV') != 'testing' else '.spec.json'
|
2022-02-14 18:06:37 +03:00
|
|
|
db = TinyDB(path.join(dirname, '../../skills', intent_obj['domain'], intent_obj['skill'], 'memory/db' + ext))
|
2019-02-10 15:26:50 +03:00
|
|
|
return { 'db': db, 'query': Query, 'operations': operations }
|