|
| 1 | +# NEON AI (TM) SOFTWARE, Software Development Kit & Application Framework |
| 2 | +# All trademark and other rights reserved by their respective owners |
| 3 | +# Copyright 2008-2022 Neongecko.com Inc. |
| 4 | +# Contributors: Daniel McKnight, Guy Daniels, Elon Gasper, Richard Leeds, |
| 5 | +# Regina Bloomstine, Casimiro Ferreira, Andrii Pernatii, Kirill Hrymailo |
| 6 | +# BSD-3 License |
| 7 | +# Redistribution and use in source and binary forms, with or without |
| 8 | +# modification, are permitted provided that the following conditions are met: |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, |
| 10 | +# this list of conditions and the following disclaimer. |
| 11 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | +# this list of conditions and the following disclaimer in the documentation |
| 13 | +# and/or other materials provided with the distribution. |
| 14 | +# 3. Neither the name of the copyright holder nor the names of its |
| 15 | +# contributors may be used to endorse or promote products derived from this |
| 16 | +# software without specific prior written permission. |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 19 | +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 20 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 21 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 24 | +# OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | +# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | +# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import os |
| 30 | +import json |
| 31 | +from abc import ABC, abstractmethod |
| 32 | + |
| 33 | +from os.path import isfile, join, dirname |
| 34 | + |
| 35 | +# from neon_utils.log_aggregators import init_log_aggregators |
| 36 | +from ovos_config.config import Configuration |
| 37 | +from ovos_utils import deprecated |
| 38 | + |
| 39 | +from utils.exceptions import MalformedConfigurationException |
| 40 | +from utils.logging_utils import LOG |
| 41 | + |
| 42 | + |
| 43 | +class KlatConfigurationBase(ABC): |
| 44 | + """Generic configuration module""" |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self._config_data: dict = None |
| 48 | + self._init_ovos_config() |
| 49 | + if not self._config_data: |
| 50 | + LOG.warning( |
| 51 | + f"OVOS Config does not contain required key = {self.config_key}, " |
| 52 | + f"trying setting up legacy config" |
| 53 | + ) |
| 54 | + self._init_legacy_config() |
| 55 | + self._config_data = self._config_data[self.config_key] |
| 56 | + self.validate_provided_configuration() |
| 57 | + # init_log_aggregators(config=self.config_data) |
| 58 | + |
| 59 | + def _init_ovos_config(self): |
| 60 | + ovos_config = _load_ovos_config() |
| 61 | + if self.config_key in ovos_config: |
| 62 | + self._config_data = ovos_config |
| 63 | + |
| 64 | + @deprecated("Legacy configuration is deprecated", |
| 65 | + "0.0.1") |
| 66 | + def _init_legacy_config(self): |
| 67 | + legacy_config_path = os.path.expanduser( |
| 68 | + os.environ.get( |
| 69 | + f"{self.config_key}_CONFIG", "~/.local/share/neon/credentials.json" |
| 70 | + ) |
| 71 | + ) |
| 72 | + self.add_new_config_properties( |
| 73 | + self.extract_config_from_path(legacy_config_path) |
| 74 | + ) |
| 75 | + |
| 76 | + def validate_provided_configuration(self): |
| 77 | + for key in self.required_sub_keys: |
| 78 | + if key not in self._config_data: |
| 79 | + return MalformedConfigurationException( |
| 80 | + f"Required configuration {key = !r} is missing" |
| 81 | + ) |
| 82 | + |
| 83 | + @property |
| 84 | + @abstractmethod |
| 85 | + def required_sub_keys(self) -> tuple[str]: |
| 86 | + pass |
| 87 | + |
| 88 | + @property |
| 89 | + @abstractmethod |
| 90 | + def config_key(self) -> str: |
| 91 | + pass |
| 92 | + |
| 93 | + def add_new_config_properties(self, new_config_dict: dict, at_key: str = None): |
| 94 | + """ |
| 95 | + Adds new configuration properties to existing configuration dict |
| 96 | +
|
| 97 | + :param new_config_dict: dictionary containing new configuration |
| 98 | + :param at_key: the key at which to append new dictionary |
| 99 | + (optional but setting that will reduce possible future key conflicts) |
| 100 | + """ |
| 101 | + if at_key: |
| 102 | + self.config_data[at_key] = new_config_dict |
| 103 | + else: |
| 104 | + # merge existing config with new dictionary (python 3.5+ syntax) |
| 105 | + self.config_data |= new_config_dict |
| 106 | + |
| 107 | + def get(self, key, default=None): |
| 108 | + return self.config_data.get(key, default) |
| 109 | + |
| 110 | + def __getitem__(self, key): |
| 111 | + return self.config_data.get(key) |
| 112 | + |
| 113 | + def __setitem__(self, key, value): |
| 114 | + self.config_data[key] = value |
| 115 | + |
| 116 | + @property |
| 117 | + def config_data(self) -> dict: |
| 118 | + if not self._config_data: |
| 119 | + self._config_data = dict() |
| 120 | + return self._config_data |
| 121 | + |
| 122 | + @config_data.setter |
| 123 | + def config_data(self, value): |
| 124 | + if not isinstance(value, dict): |
| 125 | + raise TypeError(f"Type: {type(value)} not supported") |
| 126 | + self._config_data = value |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def extract_config_from_path(file_path: str) -> dict: |
| 130 | + """ |
| 131 | + Extracts configuration dictionary from desired file path |
| 132 | +
|
| 133 | + :param file_path: desired file path |
| 134 | +
|
| 135 | + :returns dictionary containing configs from target file, empty dict otherwise |
| 136 | + """ |
| 137 | + try: |
| 138 | + with open(os.path.expanduser(file_path)) as input_file: |
| 139 | + extraction_result = json.load(input_file) |
| 140 | + except Exception as ex: |
| 141 | + LOG.error( |
| 142 | + f"Exception occurred while extracting data from {file_path}: {ex}" |
| 143 | + ) |
| 144 | + extraction_result = dict() |
| 145 | + # LOG.info(f'Extracted config: {extraction_result}') |
| 146 | + return extraction_result |
| 147 | + |
| 148 | + |
| 149 | +def _load_ovos_config() -> dict: |
| 150 | + """ |
| 151 | + Load and return a configuration object, |
| 152 | + """ |
| 153 | + config = Configuration() |
| 154 | + if not config: |
| 155 | + LOG.warning(f"No configuration found! falling back to defaults") |
| 156 | + default_config_path = join(dirname(__file__), "default_config.json") |
| 157 | + with open(default_config_path) as f: |
| 158 | + config = json.load(f) |
| 159 | + return dict(config) |
0 commit comments