Skip to content

Commit f8e374b

Browse files
committed
Refactor LOG imports
Add `configuration` module
1 parent bffcee2 commit f8e374b

File tree

12 files changed

+177
-10
lines changed

12 files changed

+177
-10
lines changed

pyklatchat_utils/configuration.py

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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)

pyklatchat_utils/database_utils/db_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
from pyklatchat_utils.database_utils.mongodb_connector import MongoDBConnector
2929
from pyklatchat_utils.database_utils.base_connector import DatabaseConnector, DatabaseTypes
30-
from pyklatchat_utils.logging_utils import LOG
30+
from neon_utils.logger import LOG
3131

3232
try:
3333
from pyklatchat_utils.database_utils.mysql_connector import MySQLConnector

pyklatchat_utils/database_utils/mongo_utils/queries/dao/chats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
MongoLogicalOperators,
3838
)
3939
from pyklatchat_utils.database_utils.mongo_utils.queries.dao.abc import MongoDocumentDAO
40-
from pyklatchat_utils.logging_utils import LOG
40+
from neon_utils.logger import LOG
4141

4242

4343
class ChatsDAO(MongoDocumentDAO):

pyklatchat_utils/database_utils/mongo_utils/queries/dao/configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from pyklatchat_utils.http_exceptions import ItemNotFoundException
3030
from pyklatchat_utils.database_utils.mongo_utils import MongoDocuments, MongoFilter
3131
from pyklatchat_utils.database_utils.mongo_utils.queries.dao.abc import MongoDocumentDAO
32-
from pyklatchat_utils.logging_utils import LOG
32+
from neon_utils.logger import LOG
3333

3434

3535
class ConfigsDAO(MongoDocumentDAO):

pyklatchat_utils/database_utils/mongo_utils/queries/dao/prompts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
MongoLogicalOperators,
3838
)
3939
from pyklatchat_utils.database_utils.mongo_utils.queries.dao.abc import MongoDocumentDAO
40-
from pyklatchat_utils.logging_utils import LOG
40+
from neon_utils.logger import LOG
4141

4242

4343
class PromptStates(IntEnum):

pyklatchat_utils/database_utils/mongo_utils/queries/dao/users.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from typing import Union
3131

3232
from pyklatchat_utils.common import generate_uuid, get_hash
33-
from pyklatchat_utils.logging_utils import LOG
33+
from neon_utils.logger import LOG
3434
from pyklatchat_utils.database_utils.mongo_utils import (
3535
MongoCommands,
3636
MongoDocuments,

pyklatchat_utils/database_utils/mongo_utils/queries/mongo_queries.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from pyklatchat_utils.database_utils.mongo_utils.structures import MongoFilter
3232
from pyklatchat_utils.database_utils.mongo_utils.queries.constants import UserPatterns, ConversationSkins
3333
from pyklatchat_utils.database_utils.mongo_utils.queries.wrapper import MongoDocumentsAPI
34-
from pyklatchat_utils.logging_utils import LOG
34+
from neon_utils.logger import LOG
3535

3636

3737
def get_translations(

pyklatchat_utils/database_utils/mongo_utils/user_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29-
from pyklatchat_utils.logging_utils import LOG
29+
from neon_utils.logger import LOG
3030

3131

3232
def get_existing_nicks_to_id(mongo_controller) -> dict:

pyklatchat_utils/database_utils/mongodb_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
from pyklatchat_utils.database_utils.base_connector import DatabaseConnector, DatabaseTypes
3333
from pyklatchat_utils.database_utils.mongo_utils.structures import MongoQuery, MongoCommands
34-
from pyklatchat_utils.logging_utils import LOG
34+
from neon_utils.logger import LOG
3535

3636

3737
class MongoDBConnector(DatabaseConnector):

pyklatchat_utils/database_utils/mysql_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from typing import Optional
3232
from pyklatchat_utils.database_utils.base_connector import DatabaseConnector, DatabaseTypes
33-
from pyklatchat_utils.logging_utils import LOG
33+
from neon_utils.logger import LOG
3434

3535

3636
class MySQLConnector(DatabaseConnector):

requirements/requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
starlette
22
pymongo
33
neon-sftp
4-
jinja2
4+
jinja2
5+
neon-utils~=1.0

tests/test_configuration.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from unittest import TestCase
2+
3+
4+
class TestConfiguration(TestCase):
5+
def test_klat_configuration(self):
6+
from pyklatchat_utils.configuration import KlatConfigurationBase
7+
# TODO

0 commit comments

Comments
 (0)