1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-09-11 18:27:21 +03:00

feat(bridge/python): add Settings module

This commit is contained in:
Divlo 2023-05-26 23:03:02 +02:00
parent 8c105a043d
commit 30e405bbe3
No known key found for this signature in database
GPG Key ID: 8F9478F220CE65E9
6 changed files with 81 additions and 29 deletions

View File

@ -25,9 +25,6 @@ SKILLS_PATH = SKILLS_ROOT_PATH
with open(os.path.join(SKILL_PATH, 'config', INTENT_OBJECT['extra_context_data']['lang'] + '.json'), 'r') as f:
SKILL_CONFIG = json.load(f)
with open(os.path.join(SKILL_PATH, 'src', 'config.json'), 'r') as f:
SKILL_SRC_CONFIG = json.load(f)['configurations']
LEON_VERSION = os.getenv('npm_package_version')
PYTHON_BRIDGE_VERSION = version.__version__

View File

@ -5,7 +5,7 @@ from time import sleep
import json
from .types import AnswerInput, AnswerData, AnswerConfig
from ..constants import SKILL_SRC_CONFIG, SKILL_CONFIG, INTENT_OBJECT
from ..constants import SKILL_CONFIG, INTENT_OBJECT
class Leon:
@ -15,20 +15,6 @@ class Leon:
if not Leon.instance:
Leon.instance = self
@staticmethod
def get_src_config(key: Union[str, None] = None):
"""
Get source configuration
"""
try:
if key:
return SKILL_SRC_CONFIG[key]
return SKILL_SRC_CONFIG
except Exception as e:
print('Error while getting source configuration:', e)
return {}
@staticmethod
def set_answer_data(answer_key: Union[str, None], data: Union[AnswerData, None] = None) -> Union[str, AnswerConfig, None]:
"""
@ -84,8 +70,7 @@ class Leon:
'output': {
'codes': 'widget' if answer_input.get('widget') and not answer_input.get('key') else answer_input.get('key'),
'answer': self.set_answer_data(answer_input.get('key'), answer_input.get('data')) or '',
'core': answer_input.get('core'),
'options': self.get_src_config('options')
'core': answer_input.get('core')
}
}

View File

@ -0,0 +1,61 @@
import json
import os
from os import path
from typing import Union, Any
from ..constants import SKILL_PATH
class Settings:
def __init__(self):
self.settings_path = path.join(SKILL_PATH, 'src', 'settings.json')
self.settings_sample_path = path.join(SKILL_PATH, 'src', 'settings.sample.json')
def is_already_set(self) -> bool:
settings_sample = self.get_settings_sample()
settings = self.get()
return json.dumps(settings) != json.dumps(settings_sample)
def clear(self) -> None:
settings_sample = self.get_settings_sample()
self.set(settings_sample)
def get_settings_sample(self) -> dict[str, Any]:
try:
with open(self.settings_sample_path, 'r') as file:
return json.load(file)
except Exception as e:
print(f"Error while reading settings sample at '{self.settings_sample_path}': {e}")
raise e
def get(self, key: Union[str, None] = None) -> dict[str, Any]:
try:
if not os.path.exists(self.settings_path):
self.clear()
with open(self.settings_path, 'r') as file:
settings = json.load(file)
if key is not None:
return settings[key]
return settings
except Exception as e:
print(f"Error while reading settings at '{self.settings_path}': {e}")
raise e
def set(self, key_or_settings: Union[str, dict[str, Any]], value=None) -> dict[str, Any]:
try:
settings = self.get()
if isinstance(key_or_settings, dict):
new_settings = key_or_settings
else:
new_settings = {**settings, key_or_settings: value}
with open(self.settings_path, 'w') as file:
json.dump(new_settings, file, indent=2)
return new_settings
except Exception as e:
print(f"Error while writing settings at '{self.settings_path}': {e}")
raise e

View File

@ -2,6 +2,7 @@
from bridges.python.src.sdk.leon import leon
from bridges.python.src.sdk.types import ActionParams
from bridges.python.src.sdk.memory import Memory
from bridges.python.src.sdk.settings import Settings
from bridges.python.src.sdk.network import Network
from bridges.python.src.sdk.aurora.button import Button
@ -103,12 +104,23 @@ def run(params: ActionParams) -> None:
leon.answer({'key': 'test'})
###
options = leon.get_src_config('options')
settings = Settings()
leon.answer({
'key': 'answer',
'data': {
'answer': options['test_config']
'answer': f"Skill settings already set: {settings.is_already_set()}"
}
})
settings.set({
'some_sample_config': 'Hello world'
})
settings.set('some_sample_config', 'Hello world 2')
options = settings.get()
some_sample_config = settings.get('some_sample_config')
leon.answer({
'key': 'answer',
'data': {
'answer': f"{options['some_sample_config']} {some_sample_config}"
}
})

View File

@ -1,6 +0,0 @@
{
"configurations": {
"options": {},
"credentials": {}
}
}

View File

@ -0,0 +1,3 @@
{
"someSampleConfig": "someSampleValue"
}