1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-26 18:23:00 +03:00
leon/bridges/python/utils.py

117 lines
2.9 KiB
Python
Raw Normal View History

2019-02-10 15:26:50 +03:00
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from json import loads, dumps
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__))
2022-02-10 16:47:43 +03:00
intent_object_path = argv[1]
codes = []
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()
2022-02-10 16:47:43 +03:00
def get_intent_obj():
"""Return intent object"""
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
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-02-10 15:26:50 +03:00
return output
def output(type, code, speech = '', core = { }):
"""Communicate with the core"""
2019-02-10 15:26:50 +03:00
codes.append(code)
2019-02-10 15:26:50 +03:00
print(dumps({
'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,
'codes': codes,
2019-02-10 15:26:50 +03:00
'speech': speech,
'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):
"""Get a skill configuration value"""
2019-02-10 15:26:50 +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():
"""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/'
skill_dl_dir = path.join(dl_dir, intent_obj['domain'], intent_obj['skill'])
2019-02-10 15:26:50 +03:00
Path(skill_dl_dir).mkdir(parents = True, exist_ok = True)
2019-02-10 15:26:50 +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
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'
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 }