Skip to content

Commit

Permalink
Merge pull request #88 from PureTryOut/xdg-config
Browse files Browse the repository at this point in the history
Migrate installed skills file to XDG Base Directory
  • Loading branch information
krisgesling authored Jan 21, 2021
2 parents 1574337 + 82e4ab8 commit 841f568
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
29 changes: 23 additions & 6 deletions msm/skill_state.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
"""Functions related to manipulating the skills.json file."""
import json
import os
import shutil
from logging import getLogger
from os.path import expanduser, isfile, dirname
from os.path import isfile, dirname, join, expanduser
from os import makedirs
from xdg import BaseDirectory

LOG = getLogger(__name__)
SKILL_STATE_PATH = '~/.mycroft/skills.json'


def get_state_path():
"""Get complete path for skill state file.
Returns:
(str) path to skills.json
"""
return join(BaseDirectory.save_data_path('mycroft'), 'skills.json')


# Make sure we migrate the installed skills file from the old non-XDG location
old_skill_state_path = expanduser('~/.mycroft/skills.json')
if isfile(old_skill_state_path):
shutil.move(old_skill_state_path, get_state_path())


def load_device_skill_state() -> dict:
"""Contains info on how skills should be updated"""
skills_data_path = expanduser(SKILL_STATE_PATH)
skills_data_path = get_state_path()
device_skill_state = {}
if isfile(skills_data_path):
try:
Expand All @@ -24,13 +41,13 @@ def load_device_skill_state() -> dict:

def write_device_skill_state(data: dict):
"""Write the device skill state to disk."""
dir_path = dirname(expanduser(SKILL_STATE_PATH))
dir_path = dirname(get_state_path())
try:
# create folder if it does not exist
makedirs(dir_path)
except Exception:
pass
skill_state_path = expanduser(SKILL_STATE_PATH)
skill_state_path = get_state_path()
with open(skill_state_path, 'w') as skill_state_file:
json.dump(data, skill_state_file, indent=4, separators=(',', ':'))

Expand All @@ -47,7 +64,7 @@ def get_skill_state(name, device_skill_state) -> dict:

def initialize_skill_state(name, origin, beta, skill_gid) -> dict:
"""Create a new skill entry
Arguments:
name: skill name
origin: the source of the installation
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ requests
GitPython
fasteners
lazy
pyxdg
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
packages=['msm'],
install_requires=[
'GitPython', 'fasteners', 'pyyaml', 'pako',
'lazy'
'lazy', 'pyxdg'
],
python_requires='>=3.5',
url='https://github.com/MycroftAI/mycroft-skills-manager',
Expand Down
7 changes: 4 additions & 3 deletions tests/test_mycroft_skills_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ def _mock_log(self):
self.log_mock = log_patch.start()

def _mock_skills_json_path(self):
expanduser_patch = patch('msm.skill_state.expanduser')
self.addCleanup(expanduser_patch.stop)
self.skills_json_path_mock = expanduser_patch.start()
savedatapath_patch = patch('msm.skill_state.get_state_path')
self.skills_json_path_mock = savedatapath_patch.start()
self.skills_json_path_mock.return_value = str(
self.temp_dir.joinpath('skills.json')
)

self.addCleanup(savedatapath_patch.stop)

def _mock_skill_entry(self):
skill_entry_patch = patch(
'msm.mycroft_skills_manager.SkillEntry.install',
Expand Down

0 comments on commit 841f568

Please sign in to comment.