2017-04-13 05:26:45 +00:00
|
|
|
# Copyright 2017 Mycroft AI, Inc.
|
|
|
|
#
|
|
|
|
# This file is part of Mycroft Core.
|
|
|
|
#
|
|
|
|
# Mycroft Core is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Mycroft Core is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Mycroft Core. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from os.path import isfile
|
|
|
|
|
2017-04-24 10:08:47 +00:00
|
|
|
|
2017-04-13 05:26:45 +00:00
|
|
|
class SkillSettings(dict):
|
|
|
|
def __init__(self, settings_file):
|
|
|
|
super(SkillSettings, self).__init__()
|
2017-04-24 10:08:47 +00:00
|
|
|
self._path = settings_file
|
|
|
|
|
|
|
|
# if file exist, open and read stored values into self
|
|
|
|
if isfile(self._path):
|
|
|
|
with open(self._path) as f:
|
2017-04-13 05:26:45 +00:00
|
|
|
json_data = json.load(f)
|
|
|
|
for key in json_data:
|
|
|
|
self.__setitem__(key, json_data[key])
|
|
|
|
|
2017-04-24 10:08:47 +00:00
|
|
|
self._is_stored = True
|
|
|
|
|
2017-04-13 05:26:45 +00:00
|
|
|
def __getitem__(self, key):
|
|
|
|
return super(SkillSettings, self).__getitem__(key)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2017-04-24 10:08:47 +00:00
|
|
|
self._is_stored = False
|
2017-04-13 05:26:45 +00:00
|
|
|
return super(SkillSettings, self).__setitem__(key, value)
|
|
|
|
|
|
|
|
def store(self):
|
2017-04-24 10:08:47 +00:00
|
|
|
if not self._is_stored:
|
|
|
|
with open(self._path, 'w')as f:
|
|
|
|
json.dump(self, f)
|