Skip to content

Commit

Permalink
Feat/optional ovos config (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Apr 10, 2023
1 parent 2d056f1 commit 8b50157
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
python -m pip install build wheel
- name: Install core repo
run: |
pip install .
pip install .[extras]
- name: Install test dependencies
run: |
pip install pytest pytest-timeout pytest-cov
Expand Down
141 changes: 95 additions & 46 deletions ovos_utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,105 @@
xdg_data_dirs,
xdg_cache_home
)
from ovos_utils.log import LOG

from ovos_config.locations import (
get_xdg_config_dirs,
get_xdg_data_dirs,
get_xdg_data_save_path,
get_xdg_config_save_path,
get_xdg_cache_save_path,
find_default_config,
find_user_config,
get_config_locations,
get_webcache_location,
get_xdg_config_locations
)
from os.path import expanduser, isfile
from os import makedirs
import json

from ovos_config.locale import get_default_lang
from ovos_config.meta import (
get_ovos_config,
get_ovos_default_config_paths,
is_using_xdg,
get_xdg_base,
set_xdg_base,
set_config_filename,
set_default_config,
get_config_filename
)

from ovos_config.config import (
read_mycroft_config,
update_mycroft_config
)
# if ovos-config not installed, these methods are still needed internally

def get_xdg_base():
try:
from ovos_config.meta import get_xdg_base as _get
return _get()
except ImportError:
return "mycroft"


def get_xdg_data_save_path():
try:
from ovos_config.locations import get_xdg_data_save_path as _get
return _get()
except ImportError:
return expanduser("~/.local/share/mycroft")


def get_xdg_data_dirs():
try:
from ovos_config.locations import get_xdg_data_dirs as _get
return _get()
except ImportError:
return [expanduser("~/.local/share/mycroft")]


def get_default_lang():
try:
from ovos_config.locale import get_default_lang as _get
return _get()
except ImportError:
return read_mycroft_config().get("lang", "en-us")


def read_mycroft_config():
try:
from ovos_config import Configuration
return Configuration()
except ImportError:
pass
path = expanduser(f"~/.config/mycroft/mycroft.conf")
if isfile(path):
with open(path) as f:
return json.load(f)
return {
# TODO - default cfg
"lang": "en-us"
}

from ovos_config.models import (
LocalConf,
ReadOnlyConfig,
MycroftUserConfig,
MycroftDefaultConfig,
MycroftSystemConfig,
MycroftXDGConfig
)

from ovos_config.meta import save_ovos_config as save_ovos_core_config
def update_mycroft_config(config, path=None, bus=None):
try:
from ovos_config.config import update_mycroft_config as _update
_update(config, path, bus)
except ImportError:
pass
# save in default user location
path = expanduser(f"~/.config/mycroft")
makedirs(path, exist_ok=True)
with open(f"{path}/mycroft.conf", "w") as f:
json.dump(config, f, indent=2)

LOG.warning("configuration moved to the `ovos_config` package. This submodule "
"will be removed in ovos_utils 0.1.0")

# ovos-config not installed, optional compat imports below don't matter
try:
from ovos_config.locations import (
get_xdg_config_dirs,
get_xdg_config_save_path,
get_xdg_cache_save_path,
find_default_config,
find_user_config,
get_config_locations,
get_webcache_location,
get_xdg_config_locations)
from ovos_config.meta import (
get_ovos_config,
get_ovos_default_config_paths,
is_using_xdg,
set_xdg_base,
set_config_filename,
set_default_config,
get_config_filename
)
from ovos_config.models import (
LocalConf,
ReadOnlyConfig,
MycroftUserConfig,
MycroftDefaultConfig,
MycroftSystemConfig,
MycroftXDGConfig
)
from ovos_config.meta import save_ovos_config as save_ovos_core_config
except ImportError:
from ovos_utils.log import LOG

def set_config_name(name, core_folder=None):
# TODO deprecate, was only out in a couple versions
LOG.warning("This reference is deprecated, use "
"`ovos_config.meta.set_config_filename`")
# renamed to match HolmesV
set_config_filename(name, core_folder)
LOG.warning("configuration moved to the `ovos_config` package. This submodule "
"will be removed in ovos_utils 0.1.0")
4 changes: 2 additions & 2 deletions ovos_utils/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pathlib import Path

from ovos_utils.bracket_expansion import expand_options
from ovos_config.config import Configuration
from ovos_utils.configuration import read_mycroft_config
from ovos_utils.file_utils import resolve_resource_file
from ovos_utils.lang import translate_word
from ovos_utils.log import LOG
Expand Down Expand Up @@ -146,7 +146,7 @@ def get_dialog(phrase, lang=None, context=None):

if not lang:
try:
conf = Configuration()
conf = read_mycroft_config()
lang = conf.get('lang')
except FileNotFoundError:
lang = "en-us"
Expand Down
19 changes: 10 additions & 9 deletions ovos_utils/file_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import collections
import csv
import re
import os
from os import walk
from os.path import splitext, join, dirname
import re
import tempfile
from ovos_utils.bracket_expansion import expand_options
from ovos_utils.log import LOG
from ovos_utils.system import search_mycroft_core_location
import time
from os import walk
from os.path import dirname
from os.path import splitext, join

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from ovos_utils.bracket_expansion import expand_options
from ovos_utils.configuration import read_mycroft_config
from ovos_utils.log import LOG
from ovos_utils.system import search_mycroft_core_location


def get_temp_path(*args):
Expand Down Expand Up @@ -103,8 +105,7 @@ def resolve_resource_file(res_name, root_path=None, config=None):
str: path to resource or None if no resource found
"""
if config is None:
from ovos_config.config import Configuration
config = Configuration()
config = read_mycroft_config()

# First look for fully qualified file (e.g. a user setting)
if os.path.isfile(res_name):
Expand Down
4 changes: 2 additions & 2 deletions ovos_utils/fingerprinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os.path import join, isfile
from ovos_utils.system import is_installed, is_running_from_module, has_screen, \
get_desktop_environment, search_mycroft_core_location, is_process_running
from ovos_utils.configuration import read_mycroft_config


class MycroftPlatform(str, Enum):
Expand All @@ -27,8 +28,7 @@ def detect_platform():

def get_config_fingerprint(config=None):
if not config:
from ovos_config.config import Configuration
config = Configuration()
config = read_mycroft_config()
conf = config
listener_conf = conf.get("listener", {})
skills_conf = conf.get("skills", {})
Expand Down
4 changes: 2 additions & 2 deletions ovos_utils/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ovos_utils.log import LOG
from ovos_utils.messagebus import wait_for_reply, get_mycroft_bus, Message
from ovos_utils.system import is_installed, has_screen, is_process_running
from ovos_config import Configuration
from ovos_utils.configuration import read_mycroft_config


def can_display():
Expand Down Expand Up @@ -461,7 +461,7 @@ class GUIInterface:
"""

def __init__(self, skill_id, bus=None, remote_server=None, config=None):
self.config = config or Configuration().get("gui", {})
self.config = config or read_mycroft_config().get("gui", {})
if remote_server:
self.config["remote-server"] = remote_server
self._bus = bus
Expand Down
7 changes: 3 additions & 4 deletions ovos_utils/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from os.path import join



class LOG:
"""
Custom logger class that acts like logging.Logger
Expand Down Expand Up @@ -49,7 +48,7 @@ def __init__(cls, name='OVOS'):
@classmethod
def init(cls, config=None):

from ovos_config.meta import get_xdg_base
from ovos_utils.configuration import get_xdg_base
from ovos_utils.xdg_utils import xdg_state_home

config = config or {}
Expand Down Expand Up @@ -148,9 +147,9 @@ def init_service_logger(service_name):
# this is makes all logs from this service be configured to write to service_name.log file
# if this is not called in every __main__.py entrypoint logs will be written
# to a generic OVOS.log file shared across all services
from ovos_config import Configuration
from ovos_utils.configuration import read_mycroft_config

_cfg = Configuration()
_cfg = read_mycroft_config()
_log_level = _cfg.get("log_level", "INFO")
_logs_conf = _cfg.get("logs") or {}
_logs_conf["level"] = _log_level
Expand Down
9 changes: 4 additions & 5 deletions ovos_utils/messagebus.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from inspect import signature
from threading import Event

from ovos_config.config import Configuration
from ovos_config.locale import get_default_lang
from ovos_utils.configuration import read_mycroft_config, get_default_lang
from pyee import BaseEventEmitter

from ovos_utils import create_loop
Expand Down Expand Up @@ -314,7 +313,7 @@ def get_mycroft_bus(host: str = None, port: int = None, route: str = None,
"""
Returns a connection to the mycroft messagebus
"""
config = Configuration().get('websocket') or dict()
config = read_mycroft_config().get('websocket') or dict()
host = host or config.get('host') or _DEFAULT_WS_CONFIG['host']
port = port or config.get('port') or _DEFAULT_WS_CONFIG['port']
route = route or config.get('route') or _DEFAULT_WS_CONFIG['route']
Expand Down Expand Up @@ -716,7 +715,7 @@ def __init__(self, trigger_message, name=None, bus=None, config=None):
name(str): name identifier for .conf settings
bus (WebsocketClient): mycroft messagebus websocket
"""
config = config or Configuration()
config = config or read_mycroft_config()
self.trigger_message = trigger_message
self.name = name or self.__class__.__name__
self.bus = bus or get_mycroft_bus()
Expand Down Expand Up @@ -859,7 +858,7 @@ def __init__(self, query_message, name=None, timeout=5, bus=None,
self.query_message.context["source"] = self.name
self.name = name or self.__class__.__name__
self.bus = bus or get_mycroft_bus()
config = config or Configuration()
config = config or read_mycroft_config()
self.config = config.get(self.name, {})
self.timeout = timeout
self.query = None
Expand Down
7 changes: 2 additions & 5 deletions ovos_utils/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import requests

# backwards compat - was only out there for a couple alpha version
# TODO - remove next stable
from ovos_utils.process_utils import RuntimeRequirements as NetworkRequirements

from ovos_utils.log import LOG
from ovos_utils.configuration import read_mycroft_config


_DEFAULT_TEST_CONFIG = {
Expand All @@ -22,8 +20,7 @@

def get_network_tests_config():
"""Get network_tests object from mycroft.configuration."""
from ovos_config import Configuration
config = Configuration()
config = read_mycroft_config()
return config.get("network_tests", _DEFAULT_TEST_CONFIG)


Expand Down
4 changes: 2 additions & 2 deletions ovos_utils/process_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@


from ovos_utils.log import LOG
from ovos_utils.configuration import get_xdg_base
from ovos_utils.file_utils import get_temp_path


@dataclass
Expand Down Expand Up @@ -291,8 +293,6 @@ class PIDLock: # python 3+ 'class Lock'
"""
@classmethod
def init(cls):
from ovos_config.meta import get_xdg_base
from ovos_utils.file_utils import get_temp_path
cls.DIRECTORY = cls.DIRECTORY or get_temp_path(get_xdg_base())
#
# Class constants
Expand Down
4 changes: 2 additions & 2 deletions ovos_utils/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@


from ovos_utils.log import LOG
from ovos_utils.configuration import read_mycroft_config


def get_ipc_directory(domain=None, config=None):
Expand All @@ -23,8 +24,7 @@ def get_ipc_directory(domain=None, config=None):
str: a path to the IPC directory
"""
if config is None:
from ovos_config.config import Configuration
config = Configuration()
config = read_mycroft_config()
path = config.get("ipc_path")
if not path:
# If not defined, use /tmp/mycroft/ipc
Expand Down
Loading

0 comments on commit 8b50157

Please sign in to comment.