Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Fix remote settings overwrite at startup
Browse files Browse the repository at this point in the history
Mycroft always mangeled any local settings changes at startup.

This caches the last settings from home on disk and only updates skills
if there is a change.

This means that as if a member is changed locally (manually edited or
changed by the skill itself) it will be kept until another change is made
to the skill on Home.
  • Loading branch information
forslund committed Sep 29, 2020
1 parent ae72ebd commit 8c8a210
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions mycroft/skills/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@
"""
import json
import os
from os.path import dirname
import re
from pathlib import Path
from threading import Timer
from xdg.BaseDirectory import xdg_cache_home

from mycroft.api import DeviceApi, is_paired
from mycroft.configuration import Configuration
from mycroft.messagebus.message import Message
from mycroft.util import camel_case_split
from mycroft.util.log import LOG
from mycroft.util.file_utils import ensure_directory_exists
from .msm_wrapper import build_msm_config, create_msm

ONE_MINUTE = 60
Expand Down Expand Up @@ -301,6 +304,42 @@ def _issue_api_call(self):
return success


# Path to remote cache
REMOTE_CACHE = Path(xdg_cache_home, 'mycroft', 'remote_skill_settings.json')


def load_remote_settings_cache():
"""Load cached remote skill settings.
Returns:
(dict) Loaded remote settings cache or None of none exists.
"""
remote_settings = None
if REMOTE_CACHE.exists():
try:
with open(str(REMOTE_CACHE)) as cache:
remote_settings = json.load(cache)
except Exception as error:
LOG.warning('Failed to read remote_cache ({})'.format(error))
return remote_settings


def save_remote_settings_cache(remote_settings):
"""Save updated remote settings to cache file.
Arguments:
remote_settings (dict): downloaded remote settings.
"""
try:
ensure_directory_exists(dirname(str(REMOTE_CACHE)))
with open(str(REMOTE_CACHE), 'w') as cache:
json.dump(remote_settings, cache)
except Exception as error:
LOG.warning('Failed to write remote_cache. ({})'.format(error))
else:
LOG.debug('Updated local cache of remote skill settings.')


class SkillSettingsDownloader:
"""Manages download of skill settings.
Expand All @@ -311,8 +350,9 @@ class SkillSettingsDownloader:
def __init__(self, bus):
self.bus = bus
self.continue_downloading = True
self.last_download_result = {}
self.remote_settings = None
self.remote_settings = load_remote_settings_cache()
self.last_download_result = self.remote_settings or {}

self.api = DeviceApi()
self.download_timer = None

Expand All @@ -333,11 +373,11 @@ def download(self):
LOG.debug('Skill settings changed since last download')
self._emit_settings_change_events(remote_settings)
self.last_download_result = remote_settings
save_remote_settings_cache(remote_settings)
else:
LOG.debug('No skill settings changes since last download')
else:
LOG.debug('Settings not downloaded - device is not paired')

# If this method is called outside of the timer loop, ensure the
# existing timer is canceled before starting a new one.
if self.download_timer:
Expand Down

0 comments on commit 8c8a210

Please sign in to comment.