OpenVoiceOS / zzz-old-ovos-utils

collection of simple utilities for use across the mycroft ecosystem
Apache License 2.0
4 stars 2 forks source link

feat/settingsmeta_gen #50

Closed JarbasAl closed 3 years ago

JarbasAl commented 3 years ago

Adds 2 new utilities, a method to auto generate the settingsmeta.json, useful for bootstraping when starting a skill, and a PrivateSettings class for skills to use, this applied as a patch for skills importing the base class from ovos_utils

This allows basic usage of https://github.com/MycroftAI/mycroft-core/pull/2698 even if skills dont provide settingsmeta.json

I almost never provide the settingsmeta for privacy + buggy behaviour reasons, with this my skills will still provide a UI without mycroft messing things up

Usage

settings2meta

Here is an example from the video collection skill template

s = {"max_videos": 500, "min_duration": -1, "max_duration": -1,
     "shuffle_menu": False, "filter_live": False, "filter_date": False,
     "min_score": 40, "match_description": False, "match_tags": True,
     "match_title": True, "search_depth": 500,
     "__mycroft_skill_firstrun": False, "filter_trailers": True,
     "filter_behind_scenes": True}

meta = settings2meta(s)

outputs

{'skillMetadata': {'sections': [{'fields': [{'label': 'Max Videos',
                                             'name': 'max_videos',
                                             'type': 'number',
                                             'value': '500'},
                                            (...)
                                            {'label': 'Search Depth',
                                             'name': 'search_depth',
                                             'type': 'number',
                                             'value': '500'},
                                            {'label': 'Filter Behind Scenes',
                                             'name': 'filter_behind_scenes',
                                             'type': 'number',
                                             'value': 'True'}],
                                   'name': 'Skill Settings'}]}}

PrivateSettings

can be used standalone

from ovos_utils.skills.settings import PrivateSettings

with PrivateSettings("testskill.jarbasai") as settings:
    print(settings.path)  # ~/.cache/json_database/testskill.jarbasai.json
    settings["key"] = "value"

    meta = settings.settingsmeta
    # can be used for displaying in GUI
    """
    {'skillMetadata': {'sections': [{'fields': [{'label': 'Key',
                                             'name': 'key',
                                             'type': 'text',
                                             'value': 'value'}],
                                     'name': 'testskill.jarbasai'}]}}
    """

    # auto saved when leaving "with" context
    # you can also manually call settings.store() if not using "with" context

or inside a skill

from ovos_utils.waiting_for_mycroft.base_skill import MycroftSkill

class MySkill(MycroftSkill):
    """ 
    this is a skill with more functionality than base mycroft-core, 
    self.private_settings is NOT available in the __init__ method, 
    self.private_settings is NOT available if MycroftSkill is imported from mycroft-core instead of ovos_utils 
    """

    def initialize(self):
        # reading data
        my_secret = self.private_settings.get("secret")
        # saving data
        self.private_settings["password"] = "password1"
        self.private_settings.store()