Issues 96 - Moving configuration to init
parent
24232a6b5b
commit
c46bc43e72
|
@ -25,7 +25,7 @@ import serial
|
|||
from mycroft.client.enclosure.arduino import EnclosureArduino
|
||||
from mycroft.client.enclosure.eyes import EnclosureEyes
|
||||
from mycroft.client.enclosure.mouth import EnclosureMouth
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.client.ws import WebsocketClient
|
||||
from mycroft.messagebus.message import Message
|
||||
from mycroft.util import kill
|
||||
|
|
|
@ -28,7 +28,7 @@ from mycroft.client.speech.mic import MutableMicrophone, Recognizer
|
|||
from mycroft.client.speech.recognizer_wrapper import \
|
||||
RemoteRecognizerWrapperFactory
|
||||
from mycroft.client.speech.word_extractor import WordExtractor
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.message import Message
|
||||
from mycroft.metrics import MetricsAggregator, Stopwatch
|
||||
from mycroft.session import SessionManager
|
||||
|
|
|
@ -20,7 +20,7 @@ import sys
|
|||
from threading import Thread, Lock
|
||||
|
||||
from mycroft.client.speech.listener import RecognizerLoop
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.client.ws import WebsocketClient
|
||||
from mycroft.messagebus.message import Message
|
||||
from mycroft.tts import tts_factory
|
||||
|
|
|
@ -21,7 +21,7 @@ import json
|
|||
import requests
|
||||
from speech_recognition import UnknownValueError
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.identity import IdentityManager
|
||||
from mycroft.metrics import Stopwatch
|
||||
from mycroft.util import CerberusAccessDenied
|
||||
|
|
|
@ -14,6 +14,159 @@
|
|||
#
|
||||
# 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 collections
|
||||
|
||||
import requests
|
||||
from configobj import ConfigObj
|
||||
from genericpath import exists, isfile
|
||||
from os.path import join, dirname, expanduser
|
||||
|
||||
from mycroft.identity import IdentityManager
|
||||
from mycroft.util import str2bool
|
||||
from mycroft.util.log import getLogger
|
||||
|
||||
__author__ = 'seanfitz, jdorleans'
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.ini')
|
||||
SYSTEM_CONFIG = '/etc/mycroft/mycroft.ini'
|
||||
USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.ini')
|
||||
|
||||
|
||||
__author__ = 'seanfitz'
|
||||
class ConfigurationLoader(object):
|
||||
"""
|
||||
A utility for loading Mycroft configuration files.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def load(config=None, locations=None):
|
||||
"""
|
||||
Loads default or specified configuration files
|
||||
"""
|
||||
if not config:
|
||||
config = {}
|
||||
|
||||
if not locations:
|
||||
locations = [DEFAULT_CONFIG, SYSTEM_CONFIG, USER_CONFIG]
|
||||
|
||||
if isinstance(locations, list):
|
||||
for location in locations:
|
||||
config = ConfigurationLoader.__load(config, location)
|
||||
else:
|
||||
logger.debug("Invalid configurations: %s" % locations)
|
||||
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
def __load(config, location):
|
||||
if exists(location) and isfile(location):
|
||||
try:
|
||||
cobj = ConfigObj(location)
|
||||
config = ConfigurationLoader.__merge(config, cobj)
|
||||
logger.debug("Configuration '%s' loaded" % location)
|
||||
except Exception, e:
|
||||
logger.error("Error loading configuration '%s'" % location)
|
||||
logger.error(repr(e))
|
||||
else:
|
||||
logger.debug("Configuration '%s' not found" % location)
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
def __merge(config, cobj):
|
||||
for k, v in cobj.iteritems():
|
||||
if isinstance(v, collections.Mapping):
|
||||
config[k] = ConfigurationLoader.__merge(config.get(k, {}), v)
|
||||
else:
|
||||
config[k] = cobj[k]
|
||||
return config
|
||||
|
||||
|
||||
class RemoteConfiguration(object):
|
||||
"""
|
||||
map remote configuration properties to
|
||||
config in the [core] config section
|
||||
"""
|
||||
__remote_keys = {
|
||||
"default_location": "location",
|
||||
"default_language": "lang",
|
||||
"timezone": "timezone"
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def load(config=None):
|
||||
if not config:
|
||||
logger.debug("No remote configuration found")
|
||||
return
|
||||
|
||||
identity = IdentityManager().get()
|
||||
config_remote = config.get("remote_configuration")
|
||||
enabled = str2bool(config_remote.get("enabled", "False"))
|
||||
|
||||
if enabled and identity.token:
|
||||
url = config_remote.get("url")
|
||||
auth_header = "Bearer %s:%s" % (identity.device_id, identity.token)
|
||||
try:
|
||||
response = requests.get(url,
|
||||
headers={"Authorization": auth_header})
|
||||
user = response.json()
|
||||
RemoteConfiguration.__load_attributes(config, user)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Failed to fetch remote configuration: %s" % repr(e))
|
||||
else:
|
||||
logger.debug(
|
||||
"Device not paired, cannot retrieve remote configuration.")
|
||||
|
||||
@staticmethod
|
||||
def __load_attributes(config, user):
|
||||
config_core = config["core"]
|
||||
|
||||
for att in user["attributes"]:
|
||||
att_name = att.get("attribute_name")
|
||||
name = RemoteConfiguration.__remote_keys.get(att_name)
|
||||
|
||||
if name:
|
||||
config_core[name] = str(att.get("attribute_value"))
|
||||
logger.info(
|
||||
"Accepting remote configuration: core[%s] == %s" %
|
||||
(name, att["attribute_value"]))
|
||||
|
||||
|
||||
class ConfigurationManager(object):
|
||||
"""
|
||||
Static management utility for calling up cached configuration.
|
||||
"""
|
||||
__config = None
|
||||
|
||||
@staticmethod
|
||||
def load_defaults():
|
||||
ConfigurationManager.__config = ConfigurationLoader.load()
|
||||
RemoteConfiguration.load(ConfigurationManager.__config)
|
||||
|
||||
@staticmethod
|
||||
def load_local(locations=None):
|
||||
ConfigurationManager.__config = ConfigurationLoader.load(
|
||||
ConfigurationManager.get(), locations)
|
||||
|
||||
@staticmethod
|
||||
def load_remote():
|
||||
if not ConfigurationManager.__config:
|
||||
ConfigurationManager.load_defaults()
|
||||
else:
|
||||
RemoteConfiguration.load(ConfigurationManager.__config)
|
||||
|
||||
@staticmethod
|
||||
def get(locations=None):
|
||||
"""
|
||||
Get cached configuration.
|
||||
|
||||
:return: A dictionary representing Mycroft configuration.
|
||||
"""
|
||||
if not ConfigurationManager.__config:
|
||||
ConfigurationManager.load_defaults()
|
||||
|
||||
if locations:
|
||||
ConfigurationManager.load_local(locations)
|
||||
|
||||
return ConfigurationManager.__config
|
||||
|
|
|
@ -1,173 +0,0 @@
|
|||
# Copyright 2016 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 collections
|
||||
|
||||
import requests
|
||||
from configobj import ConfigObj
|
||||
from os.path import join, dirname, expanduser, exists, isfile
|
||||
|
||||
from mycroft.identity import IdentityManager
|
||||
from mycroft.util import str2bool
|
||||
from mycroft.util.log import getLogger
|
||||
|
||||
__author__ = 'seanfitz, jdorleans'
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.ini')
|
||||
SYSTEM_CONFIG = '/etc/mycroft/mycroft.ini'
|
||||
USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.ini')
|
||||
|
||||
|
||||
class ConfigurationLoader(object):
|
||||
"""
|
||||
A utility for loading Mycroft configuration files.
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def load(config=None, locations=None):
|
||||
"""
|
||||
Loads default or specified configuration files
|
||||
"""
|
||||
if not config:
|
||||
config = {}
|
||||
|
||||
if not locations:
|
||||
locations = [DEFAULT_CONFIG, SYSTEM_CONFIG, USER_CONFIG]
|
||||
|
||||
if isinstance(locations, list):
|
||||
for location in locations:
|
||||
config = ConfigurationLoader.__load(config, location)
|
||||
else:
|
||||
logger.debug("Invalid configurations: %s" % locations)
|
||||
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
def __load(config, location):
|
||||
if exists(location) and isfile(location):
|
||||
try:
|
||||
cobj = ConfigObj(location)
|
||||
config = ConfigurationLoader.__merge(config, cobj)
|
||||
logger.debug("Configuration '%s' loaded" % location)
|
||||
except Exception, e:
|
||||
logger.error("Error loading configuration '%s'" % location)
|
||||
logger.error(repr(e))
|
||||
else:
|
||||
logger.debug("Configuration '%s' not found" % location)
|
||||
return config
|
||||
|
||||
@staticmethod
|
||||
def __merge(config, cobj):
|
||||
for k, v in cobj.iteritems():
|
||||
if isinstance(v, collections.Mapping):
|
||||
config[k] = ConfigurationLoader.__merge(config.get(k, {}), v)
|
||||
else:
|
||||
config[k] = cobj[k]
|
||||
return config
|
||||
|
||||
|
||||
class RemoteConfiguration(object):
|
||||
"""
|
||||
map remote configuration properties to
|
||||
config in the [core] config section
|
||||
"""
|
||||
__remote_keys = {
|
||||
"default_location": "location",
|
||||
"default_language": "lang",
|
||||
"timezone": "timezone"
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def load(config=None):
|
||||
if not config:
|
||||
logger.debug("No remote configuration found")
|
||||
return
|
||||
|
||||
identity = IdentityManager().get()
|
||||
config_remote = config.get("remote_configuration")
|
||||
enabled = str2bool(config_remote.get("enabled", "False"))
|
||||
|
||||
if enabled and identity.token:
|
||||
url = config_remote.get("url")
|
||||
auth_header = "Bearer %s:%s" % (identity.device_id, identity.token)
|
||||
try:
|
||||
response = requests.get(url,
|
||||
headers={"Authorization": auth_header})
|
||||
user = response.json()
|
||||
RemoteConfiguration.__load_attributes(config, user)
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
"Failed to fetch remote configuration: %s" % repr(e))
|
||||
else:
|
||||
logger.debug(
|
||||
"Device not paired, cannot retrieve remote configuration.")
|
||||
|
||||
@staticmethod
|
||||
def __load_attributes(config, user):
|
||||
config_core = config["core"]
|
||||
|
||||
for att in user["attributes"]:
|
||||
att_name = att.get("attribute_name")
|
||||
name = RemoteConfiguration.__remote_keys.get(att_name)
|
||||
|
||||
if name:
|
||||
config_core[name] = str(att.get("attribute_value"))
|
||||
logger.info(
|
||||
"Accepting remote configuration: core[%s] == %s" %
|
||||
(name, att["attribute_value"]))
|
||||
|
||||
|
||||
class ConfigurationManager(object):
|
||||
"""
|
||||
Static management utility for calling up cached configuration.
|
||||
"""
|
||||
__config = None
|
||||
|
||||
@staticmethod
|
||||
def load_defaults():
|
||||
ConfigurationManager.__config = ConfigurationLoader.load()
|
||||
RemoteConfiguration.load(ConfigurationManager.__config)
|
||||
|
||||
@staticmethod
|
||||
def load_local(locations=None):
|
||||
ConfigurationManager.__config = ConfigurationLoader.load(
|
||||
ConfigurationManager.get(), locations)
|
||||
|
||||
@staticmethod
|
||||
def load_remote():
|
||||
if not ConfigurationManager.__config:
|
||||
ConfigurationManager.load_defaults()
|
||||
else:
|
||||
RemoteConfiguration.load(ConfigurationManager.__config)
|
||||
|
||||
@staticmethod
|
||||
def get(locations=None):
|
||||
"""
|
||||
Get cached configuration.
|
||||
|
||||
:return: A dictionary representing Mycroft configuration.
|
||||
"""
|
||||
if not ConfigurationManager.__config:
|
||||
ConfigurationManager.load_defaults()
|
||||
|
||||
if locations:
|
||||
ConfigurationManager.load_local(locations)
|
||||
|
||||
return ConfigurationManager.__config
|
|
@ -24,7 +24,7 @@ from pyee import EventEmitter
|
|||
from websocket import WebSocketApp
|
||||
|
||||
import mycroft.util.log
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.message import Message
|
||||
from mycroft.util import str2bool
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
import tornado.ioloop as ioloop
|
||||
import tornado.web as web
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.service.ws import WebsocketEventHandler
|
||||
|
||||
__author__ = 'seanfitz'
|
||||
|
|
|
@ -23,7 +23,7 @@ import requests
|
|||
|
||||
from mycroft.util import str2bool
|
||||
from mycroft.util.log import getLogger
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.session import SessionManager
|
||||
from mycroft.util.setup_base import get_version
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
import shortuuid
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.identity import IdentityManager
|
||||
from mycroft.messagebus.client.ws import WebsocketClient
|
||||
from mycroft.messagebus.message import Message
|
||||
|
|
|
@ -20,7 +20,7 @@ import time
|
|||
from uuid import uuid4
|
||||
from threading import Lock
|
||||
from mycroft.util import log
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
|
||||
__author__ = 'seanfitz'
|
||||
logger = log.getLogger(__name__)
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
from adapt.intent import IntentBuilder
|
||||
from os.path import join, dirname
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.identity import IdentityManager
|
||||
from mycroft.skills.core import MycroftSkill
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ import sys
|
|||
|
||||
from os.path import dirname, exists, isdir
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.messagebus.client.ws import WebsocketClient
|
||||
from mycroft.skills.core import create_skill_descriptor, load_skill
|
||||
from mycroft.skills.intent import create_skill as create_intent_skill
|
||||
|
|
|
@ -26,7 +26,7 @@ from adapt.intent import Intent
|
|||
from os.path import join, dirname, splitext, isdir
|
||||
|
||||
from mycroft.client.enclosure.api import EnclosureAPI
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.dialog import DialogLoader
|
||||
from mycroft.filesystem import FileSystemAccess
|
||||
from mycroft.messagebus.message import Message
|
||||
|
|
|
@ -27,7 +27,7 @@ from pyowm.webapi25 import stationhistoryparser
|
|||
from pyowm.webapi25 import weathercoderegistry
|
||||
from pyowm.webapi25 import cityidregistry
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
|
||||
"""
|
||||
Configuration for the PyOWM library specific to OWM web API version 2.5
|
||||
|
|
|
@ -21,7 +21,7 @@ from os.path import join
|
|||
|
||||
from mycroft import MYCROFT_ROOT_PATH
|
||||
from mycroft.tts import TTS, TTSValidator
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
|
||||
__author__ = 'jdorleans'
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
import logging
|
||||
|
||||
from mycroft.configuration.config import ConfigurationManager
|
||||
from mycroft.configuration import ConfigurationManager
|
||||
from mycroft.tts import espeak_tts
|
||||
from mycroft.tts import fa_tts
|
||||
from mycroft.tts import google_tts
|
||||
|
|
Loading…
Reference in New Issue