diff --git a/mycroft/configuration/__init__.py b/mycroft/configuration/__init__.py index cb3646910ff9..15a1b94ce7bb 100644 --- a/mycroft/configuration/__init__.py +++ b/mycroft/configuration/__init__.py @@ -12,8 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # -from .config import Configuration, LocalConf, RemoteConf, \ - SYSTEM_CONFIG, USER_CONFIG +from .config import Configuration, LocalConf, RemoteConf +from .locations import SYSTEM_CONFIG, USER_CONFIG # Compatibility diff --git a/mycroft/configuration/config.py b/mycroft/configuration/config.py index 72edccc3e986..8890a46cc8ac 100644 --- a/mycroft/configuration/config.py +++ b/mycroft/configuration/config.py @@ -17,28 +17,13 @@ import re import json import inflection -from os.path import exists, isfile, join, dirname, expanduser +from os.path import exists, isfile from requests import RequestException -from mycroft.util.json_helper import load_commented_json +from mycroft.util.json_helper import load_commented_json, merge_dict from mycroft.util.log import LOG - -def merge_dict(base, delta): - """ - Recursively merging configuration dictionaries. - - Args: - base: Target for merge - delta: Dictionary to merge into base - """ - - for k, dv in delta.items(): - bv = base.get(k) - if isinstance(dv, dict) and isinstance(bv, dict): - merge_dict(bv, dv) - else: - base[k] = dv +from .locations import DEFAULT_CONFIG, SYSTEM_CONFIG, USER_CONFIG def is_remote_list(values): @@ -189,12 +174,6 @@ def __init__(self, cache=None): self.load_local(cache) -DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.conf') -SYSTEM_CONFIG = '/etc/mycroft/mycroft.conf' -USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.conf') -REMOTE_CONFIG = "mycroft.ai" - - class Configuration(object): __config = {} # Cached config __patch = {} # Patch config that skills can update to override config diff --git a/mycroft/configuration/locations.py b/mycroft/configuration/locations.py new file mode 100644 index 000000000000..0759152f999a --- /dev/null +++ b/mycroft/configuration/locations.py @@ -0,0 +1,19 @@ +# Copyright 2018 Mycroft AI Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from os.path import join, dirname, expanduser + +DEFAULT_CONFIG = join(dirname(__file__), 'mycroft.conf') +SYSTEM_CONFIG = '/etc/mycroft/mycroft.conf' +USER_CONFIG = join(expanduser('~'), '.mycroft/mycroft.conf') +REMOTE_CONFIG = "mycroft.ai" diff --git a/mycroft/util/json_helper.py b/mycroft/util/json_helper.py index a1cb89250149..b0cebd552722 100644 --- a/mycroft/util/json_helper.py +++ b/mycroft/util/json_helper.py @@ -15,6 +15,23 @@ import json +def merge_dict(base, delta): + """ + Recursively merging configuration dictionaries. + + Args: + base: Target for merge + delta: Dictionary to merge into base + """ + + for k, dv in delta.items(): + bv = base.get(k) + if isinstance(dv, dict) and isinstance(bv, dict): + merge_dict(bv, dv) + else: + base[k] = dv + + def load_commented_json(filename): """ Loads an JSON file, ignoring comments diff --git a/mycroft/util/log.py b/mycroft/util/log.py index dc9e004dbde6..70c243f4afac 100644 --- a/mycroft/util/log.py +++ b/mycroft/util/log.py @@ -18,7 +18,8 @@ from os.path import isfile -from mycroft.util.json_helper import load_commented_json +from mycroft.util.json_helper import load_commented_json, merge_dict +from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG def getLogger(name="MYCROFT"): @@ -61,8 +62,12 @@ class LOG: @classmethod def init(cls): - sys_config = '/etc/mycroft/mycroft.conf' - config = load_commented_json(sys_config) if isfile(sys_config) else {} + confs = [SYSTEM_CONFIG, USER_CONFIG] + config = {} + for conf in confs: + merge_dict(config, + load_commented_json(conf) if isfile(conf) else {}) + cls.level = logging.getLevelName(config.get('log_level', 'DEBUG')) fmt = '%(asctime)s.%(msecs)03d - ' \ '%(name)s - %(levelname)s - %(message)s' @@ -74,11 +79,11 @@ def init(cls): @classmethod def create_logger(cls, name): - l = logging.getLogger(name) - l.propagate = False - l.addHandler(cls.handler) - l.setLevel(cls.level) - return l + logger = logging.getLogger(name) + logger.propagate = False + logger.addHandler(cls.handler) + logger.setLevel(cls.level) + return logger def __init__(self, name): LOG._custom_name = name @@ -107,4 +112,5 @@ def _log(cls, func, *args, **kwargs): name = module_name + ':' + record[3] + ':' + str(record[2]) func(cls.create_logger(name), *args, **kwargs) + LOG.init()