20 from os.path
import exists, isfile
21 from requests
import RequestException
26 from .locations
import (DEFAULT_CONFIG, SYSTEM_CONFIG, USER_CONFIG,
31 ''' check if this list corresponds to a backend formatted collection of 34 if not isinstance(v, dict):
36 if "@type" not in v.keys():
43 Translate config names from server to equivalents usable 47 config: base config to populate 48 settings: remote settings to be translated 50 IGNORED_SETTINGS = [
"uuid",
"@type",
"active",
"user",
"device"]
52 for k, v
in setting.items():
53 if k
not in IGNORED_SETTINGS:
56 key = inflection.underscore(re.sub(
r"Setting(s)?",
"", k))
57 if isinstance(v, dict):
58 config[key] = config.get(key, {})
60 elif isinstance(v, list):
73 Translate list formated by mycroft server. 76 config (dict): target config 77 values (list): list from mycroft server config 82 config[
"module"] = module
83 config[module] = config.get(module, {})
89 Config dict from file. 99 Load local json file into self. 102 path (str): file to load 104 if exists(path)
and isfile(path):
108 self.__setitem__(key, config[key])
110 LOG.debug(
"Configuration {} loaded".format(path))
111 except Exception
as e:
112 LOG.error(
"Error loading configuration '{}'".format(path))
115 LOG.debug(
"Configuration '{}' not defined, skipping".format(path))
119 Cache the received settings locally. The cache will be used if 120 the remote is unreachable to load settings that are as close 121 to the user's as possible 123 path = path
or self.
path 124 with open(path,
'w')
as f:
125 json.dump(self, f, indent=2)
133 Config dict fetched from mycroft.ai 136 super(RemoteConf, self).
__init__(
None)
138 cache = cache
or WEB_CONFIG_CACHE
148 setting = api.get_settings()
151 location = api.get_location()
152 except RequestException
as e:
153 LOG.error(
"RequestException fetching remote location: {}" 155 if exists(cache)
and isfile(cache):
159 setting[
"location"] = location
164 self.__setitem__(key, config[key])
167 except RequestException
as e:
168 LOG.error(
"RequestException fetching remote configuration: {}" 172 except Exception
as e:
173 LOG.error(
"Failed to fetch remote configuration: %s" % repr(e),
183 def get(configs=None, cache=True):
185 Get configuration, returns cached instance if available otherwise 186 builds a new configuration dict. 189 configs (list): List of configuration dicts 190 cache (boolean): True if the result should be cached 192 if Configuration.__config:
193 return Configuration.__config
195 return Configuration.load_config_stack(configs, cache)
200 load a stack of config dicts into a single dict 203 configs (list): list of dicts to load 204 cache (boolean): True if result should be cached 206 Returns: merged dict of all configuration files 211 Configuration.__patch]
214 for index, item
in enumerate(configs):
215 if isinstance(item, str):
225 Configuration.__config.clear()
227 Configuration.__config[key] = base[key]
228 return Configuration.__config
235 Setup websocket handlers to update config. 238 ws: Websocket instance 240 ws.on(
"configuration.updated", Configuration.updated)
241 ws.on(
"configuration.patch", Configuration.patch)
246 handler for configuration.updated, triggers an update 249 Configuration.load_config_stack(cache=
True)
254 patch the volatile dict usable by skills 257 message: Messagebus message should contain a config 260 config = message.data.get(
"config", {})
262 Configuration.load_config_stack(cache=
True)
def translate_remote(config, setting)
def is_remote_list(values)
def is_paired(ignore_errors=True)
def __init__(self, cache=None)
def load_config_stack(configs=None, cache=False)
def load_commented_json(filename)
def translate_list(config, values)
def get(configs=None, cache=True)
def store(self, path=None)
def load_local(self, path)
def merge_dict(base, delta)
def get(phrase, lang=None, context=None)