Skip to content

Commit

Permalink
feat/ovos_conf from ovos_utils
Browse files Browse the repository at this point in the history
logic moved to ovos_utils OpenVoiceOS/ovos-utils#12
  • Loading branch information
JarbasAl committed Dec 3, 2021
1 parent 3828188 commit 71d4f62
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 128 deletions.
8 changes: 4 additions & 4 deletions mycroft/client/text/text_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from mycroft.messagebus.client import MessageBusClient
from mycroft.messagebus.message import Message
from mycroft.util.log import LOG
from mycroft.configuration import Configuration, BASE_FOLDER
from mycroft.configuration.ovos import is_using_xdg
from mycroft.configuration import Configuration
from ovos_utils.configuration import is_using_xdg, get_xdg_base

import locale

Expand Down Expand Up @@ -187,7 +187,7 @@ def load_settings():

# Check XDG_CONFIG_DIR
if config_file is None:
for conf_dir in xdg.BaseDirectory.load_config_paths(BASE_FOLDER):
for conf_dir in xdg.BaseDirectory.load_config_paths(get_xdg_base()):
xdg_file = os.path.join(conf_dir, filename)
if os.path.isfile(xdg_file):
config_file = xdg_file
Expand Down Expand Up @@ -230,7 +230,7 @@ def save_settings():
config_file = path
else:
config_file = os.path.join(xdg.BaseDirectory.xdg_config_home,
BASE_FOLDER, filename)
get_xdg_base(), filename)

with io.open(config_file, 'w') as f:
f.write(str(json.dumps(config, ensure_ascii=False)))
Expand Down
5 changes: 2 additions & 3 deletions mycroft/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@
from mycroft.configuration.locale import set_default_lf_lang, setup_locale, \
set_default_tz, set_default_lang, get_default_tz, get_default_lang, \
get_config_tz, get_primary_lang_code, load_languages, load_language
from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG, \
get_xdg_config_locations, BASE_FOLDER, DEFAULT_CONFIG
from mycroft.configuration.ovos import is_using_xdg, get_ovos_config
from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG, DEFAULT_CONFIG
from ovos_utils.configuration import get_ovos_config, is_using_xdg, get_xdg_config_locations
4 changes: 2 additions & 2 deletions mycroft/configuration/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from requests import RequestException

from mycroft.configuration.locations import *
from mycroft.configuration.ovos import is_using_xdg
from ovos_utils.configuration import is_using_xdg, get_xdg_config_locations
from mycroft.util import camel_case_split
from mycroft.util.json_helper import load_commented_json, merge_dict
from mycroft.util.log import LOG
Expand Down Expand Up @@ -196,7 +196,7 @@ def _log_old_location_deprecation(old_user_config=OLD_USER_CONFIG):
LOG.warning(" Note that this location is deprecated and will" +
" not be used in the future")
LOG.warning(" Please move it to " + join(xdg.BaseDirectory.xdg_config_home,
BASE_FOLDER))
get_xdg_base()))


def _get_system_constraints():
Expand Down
28 changes: 8 additions & 20 deletions mycroft/configuration/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from time import sleep
from os.path import join, dirname, expanduser, exists
from time import sleep

import xdg.BaseDirectory

from mycroft.configuration.ovos import get_ovos_config
from ovos_utils.configuration import get_ovos_config, get_xdg_config_locations

# check for path overrides
_ovos_cfg = get_ovos_config()
BASE_FOLDER = _ovos_cfg["base_folder"]
CONFIG_FILE_NAME = _ovos_cfg["config_filename"]
DEFAULT_CONFIG = _ovos_cfg["default_config_path"] or \
join(dirname(__file__), CONFIG_FILE_NAME)

DEFAULT_CONFIG = _ovos_cfg["default_config_path"] or \
join(dirname(__file__), "mycroft.conf")
SYSTEM_CONFIG = os.environ.get('MYCROFT_SYSTEM_CONFIG',
f'/etc/{BASE_FOLDER}/{CONFIG_FILE_NAME}')
f'/etc/{_ovos_cfg["base_folder"]}/{_ovos_cfg["config_filename"]}')
# TODO: remove in 22.02
# Make sure we support the old location still
# Deprecated and will be removed eventually
OLD_USER_CONFIG = join(expanduser('~'), '.' + BASE_FOLDER, CONFIG_FILE_NAME)
USER_CONFIG = join(xdg.BaseDirectory.xdg_config_home, BASE_FOLDER, CONFIG_FILE_NAME)
OLD_USER_CONFIG = join(expanduser('~'), '.' + _ovos_cfg["base_folder"], _ovos_cfg["config_filename"])
USER_CONFIG = join(xdg.BaseDirectory.xdg_config_home, _ovos_cfg["base_folder"], _ovos_cfg["config_filename"])
REMOTE_CONFIG = "mycroft.ai"
WEB_CONFIG_CACHE = os.environ.get('MYCROFT_WEB_CACHE') or \
join(xdg.BaseDirectory.xdg_config_home, BASE_FOLDER, 'web_cache.json')


def get_xdg_config_locations():
# This includes both the user config and
# /etc/xdg/mycroft/mycroft.conf
xdg_paths = list(reversed(
[join(p, CONFIG_FILE_NAME)
for p in xdg.BaseDirectory.load_config_paths(BASE_FOLDER)]
))
return xdg_paths
join(xdg.BaseDirectory.xdg_config_home, _ovos_cfg["base_folder"], 'web_cache.json')


def __ensure_folder_exists(path):
Expand Down
58 changes: 1 addition & 57 deletions mycroft/configuration/ovos.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,60 +62,4 @@
}
}
"""
from importlib.util import find_spec
from os.path import isfile, dirname, join

import xdg.BaseDirectory
from mycroft.util.json_helper import load_commented_json, merge_dict


def get_ovos_config():
config = {"xdg": True,
"base_folder": "mycroft",
"config_filename": "mycroft.conf",
"default_config_path": join(dirname(__file__), "mycroft.conf")}

try:
if isfile("/etc/OpenVoiceOS/ovos.conf"):
config = merge_dict(config,
load_commented_json(
"/etc/OpenVoiceOS/ovos.conf"))
elif isfile("/etc/mycroft/ovos.conf"):
config = merge_dict(config,
load_commented_json("/etc/mycroft/ovos.conf"))
except:
# tolerate bad json TODO proper exception (?)
pass

# This includes both the user config and
# /etc/xdg/OpenVoiceOS/ovos.conf
for p in xdg.BaseDirectory.load_config_paths("OpenVoiceOS"):
if isfile(join(p, "ovos.conf")):
try:
xdg_cfg = load_commented_json(join(p, "ovos.conf"))
config = merge_dict(config, xdg_cfg)
except:
# tolerate bad json TODO proper exception (?)
pass

# let's check for derivatives specific configs
# the assumption is that these cores are exclusive to each other,
# this will never find more than one override
# TODO this works if using dedicated .venvs what about system installs?
cores = config.get("module_overrides") or {}
for k in cores:
if find_spec(k):
config = merge_dict(config, cores[k])
break
else:
subcores = config.get("submodule_mappings") or {}
for k in subcores:
if find_spec(k):
config = merge_dict(config, cores[subcores[k]])
break

return config


def is_using_xdg():
return get_ovos_config().get("xdg", True)
from ovos_utils.configuration import get_ovos_config, is_using_xdg
8 changes: 4 additions & 4 deletions mycroft/filesystem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import os
import shutil
from os.path import join, expanduser, isdir
import mycroft.configuration
import xdg.BaseDirectory
from ovos_utils.configuration import is_using_xdg, get_xdg_base


class FileSystemAccess:
Expand All @@ -35,11 +35,11 @@ def __init_path(path):
if not isinstance(path, str) or len(path) == 0:
raise ValueError("path must be initialized as a non empty string")

path = expanduser(f'~/.{mycroft.configuration.BASE_FOLDER}/{path}')
path = expanduser(f'~/.{get_xdg_base()}/{path}')

if mycroft.configuration.is_using_xdg():
if is_using_xdg():
xdg_path = expanduser(
f'{xdg.BaseDirectory.xdg_config_home}/{mycroft.configuration.BASE_FOLDER}/{path}')
f'{xdg.BaseDirectory.xdg_config_home}/{get_xdg_base()}/{path}')
# Migrate from the old location if it still exists
if isdir(path) and not isdir(xdg_path):
shutil.move(path, xdg_path)
Expand Down
4 changes: 2 additions & 2 deletions mycroft/lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
from signal import getsignal, signal, SIGKILL, SIGINT, SIGTERM, \
SIG_DFL, default_int_handler, SIG_IGN # signals
from mycroft.configuration import BASE_FOLDER
from ovos_utils.configuration import get_xdg_base
import os # Operating System functions

#
Expand Down Expand Up @@ -98,7 +98,7 @@ class Lock: # python 3+ 'class Lock'

#
# Class constants
DIRECTORY = get_temp_path(BASE_FOLDER)
DIRECTORY = get_temp_path(get_xdg_base())
FILE = '/{}.pid'

#
Expand Down
8 changes: 4 additions & 4 deletions mycroft/skills/event_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from os.path import isfile, join, expanduser
import xdg.BaseDirectory

from mycroft.configuration import Configuration, BASE_FOLDER
from mycroft.configuration.ovos import is_using_xdg
from mycroft.configuration import Configuration
from ovos_utils.configuration import is_using_xdg, get_xdg_base
from mycroft.messagebus.message import Message
from mycroft.util.log import LOG
from mycroft.skills.mycroft_skill.event_container import EventContainer, \
Expand Down Expand Up @@ -67,13 +67,13 @@ def __init__(self, bus, schedule_file='schedule.json', autostart=True):
self.is_running = True

core_conf = Configuration.get(remote=False)
data_dir = core_conf.get('data_dir', xdg.BaseDirectory.save_data_path(BASE_FOLDER))
data_dir = core_conf.get('data_dir', xdg.BaseDirectory.save_data_path(get_xdg_base()))
old_schedule_path = join(expanduser(data_dir), schedule_file)

self.schedule_file = old_schedule_path
if is_using_xdg():
new_schedule_path = join(
xdg.BaseDirectory.load_first_config(BASE_FOLDER), schedule_file)
xdg.BaseDirectory.load_first_config(get_xdg_base()), schedule_file)
if isfile(old_schedule_path):
shutil.move(old_schedule_path, new_schedule_path)
self.schedule_file = new_schedule_path
Expand Down
5 changes: 3 additions & 2 deletions mycroft/skills/msm_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@

import xdg.BaseDirectory
from combo_lock import ComboLock

from mock_msm import \
MycroftSkillsManager as MockMSM, \
SkillRepo as MockSkillRepo

from mycroft.configuration import BASE_FOLDER
from ovos_utils.configuration import get_xdg_base
from mycroft.skills.skill_loader import get_default_skills_directory
from mycroft.util.file_utils import get_temp_path
from mycroft.util.log import LOG
Expand Down Expand Up @@ -77,7 +78,7 @@ def build_msm_config(device_config: dict) -> MsmConfig:
msm_config = device_config['skills'].get('msm', {})
msm_repo_config = msm_config.get('repo', {})
enclosure_config = device_config.get('enclosure', {})
data_dir = path.expanduser(device_config.get('data_dir', xdg.BaseDirectory.save_data_path(BASE_FOLDER)))
data_dir = path.expanduser(device_config.get('data_dir', xdg.BaseDirectory.save_data_path(get_xdg_base())))
skills_dir = get_default_skills_directory(device_config)
old_skills_dir = path.join(data_dir, msm_config.get('directory', "skills"))

Expand Down
5 changes: 3 additions & 2 deletions mycroft/skills/mycroft_skill/mycroft_skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
from mycroft.api import DeviceApi
from mycroft.audio import wait_while_speaking
from mycroft.configuration.ovos import is_using_xdg
from mycroft.configuration import Configuration, BASE_FOLDER
from ovos_utils.configuration import is_using_xdg, get_xdg_base
from mycroft.configuration import Configuration
from mycroft.dialog import load_dialogs
from mycroft.filesystem import FileSystemAccess
from mycroft.gui import SkillGUI
Expand Down Expand Up @@ -244,7 +245,7 @@ def settings_path(self):
return join(self.settings_write_path, 'settings.json')
if not is_xdg:
return self._old_settings_path
return join(xdg.BaseDirectory.save_config_path(BASE_FOLDER, 'skills', self.skill_id), 'settings.json')
return join(xdg.BaseDirectory.save_config_path(get_xdg_base(), 'skills', self.skill_id), 'settings.json')

@property
def _old_settings_path(self):
Expand Down
9 changes: 5 additions & 4 deletions mycroft/skills/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
from xdg.BaseDirectory import xdg_cache_home

from mycroft.api import DeviceApi, is_paired, is_backend_disabled
from mycroft.configuration import Configuration, BASE_FOLDER
from mycroft.configuration import Configuration
from ovos_utils.configuration import get_xdg_base
from mycroft.messagebus.message import Message
from mycroft.util import camel_case_split
from mycroft.util.file_utils import ensure_directory_exists
Expand Down Expand Up @@ -178,7 +179,7 @@ def upload(self):
LOG.debug('settingsmeta.json not uploaded - device is not paired')

if not synced and not self._stopped:
self.upload_timer = Timer(ONE_MINUTE, self.upload)
self.upload_timer = Timer(60, self.upload)
self.upload_timer.daemon = True
self.upload_timer.start()

Expand Down Expand Up @@ -237,7 +238,7 @@ def _issue_api_call(self):


# Path to remote cache
REMOTE_CACHE = Path(xdg_cache_home, BASE_FOLDER, 'remote_skill_settings.json')
REMOTE_CACHE = Path(xdg_cache_home, get_xdg_base(), 'remote_skill_settings.json')


def load_remote_settings_cache():
Expand Down Expand Up @@ -330,7 +331,7 @@ def download(self, message=None):
self.download_timer.cancel()

if self.continue_downloading:
self.download_timer = Timer(ONE_MINUTE, self.download)
self.download_timer = Timer(60, self.download)
self.download_timer.daemon = True
self.download_timer.start()

Expand Down
11 changes: 5 additions & 6 deletions mycroft/skills/skill_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

import xdg.BaseDirectory

from mycroft.configuration import BASE_FOLDER
from mycroft.configuration import Configuration
from mycroft.configuration.ovos import is_using_xdg
from ovos_utils.configuration import is_using_xdg, get_xdg_base
from mycroft.messagebus import Message
from mycroft.skills.mycroft_skill.mycroft_skill import MycroftSkill
from mycroft.skills.settings import SettingsMetaUploader
Expand Down Expand Up @@ -94,7 +93,7 @@ def get_skill_directories(conf=None):
# they should be considered applets rather than full applications
skill_locations = list(reversed(
[os.path.join(p, folder)
for p in xdg.BaseDirectory.load_data_paths(BASE_FOLDER)]
for p in xdg.BaseDirectory.load_data_paths(get_xdg_base())]
))

# load the default skills folder
Expand Down Expand Up @@ -146,15 +145,15 @@ def get_default_skills_directory(conf=None):
# if xdg is disabled, ignore it!
elif not is_using_xdg():
# old style mycroft-core skills path definition
data_dir = conf.get("data_dir") or "/opt/" + BASE_FOLDER
data_dir = conf.get("data_dir") or "/opt/" + get_xdg_base()
skills_folder = path.join(data_dir, folder)
else:
skills_folder = xdg.BaseDirectory.save_data_path(BASE_FOLDER + '/' + folder)
skills_folder = xdg.BaseDirectory.save_data_path(get_xdg_base() + '/' + folder)
# create folder if needed
try:
makedirs(skills_folder, exist_ok=True)
except PermissionError: # old style /opt/mycroft/skills not available
skills_folder = xdg.BaseDirectory.save_data_path(BASE_FOLDER + '/' + folder)
skills_folder = xdg.BaseDirectory.save_data_path(get_xdg_base() + '/' + folder)
makedirs(skills_folder, exist_ok=True)

return path.expanduser(skills_folder)
Expand Down
5 changes: 3 additions & 2 deletions mycroft/skills/skill_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
from json_database import JsonStorage

from mycroft.api import DeviceApi, is_paired
from mycroft.configuration import Configuration, BASE_FOLDER
from mycroft.skills.skill_loader import get_skill_directories
from mycroft.configuration import Configuration
from ovos_utils.configuration import get_xdg_base, is_using_xdg
from mycroft.util.log import LOG


Expand All @@ -34,7 +35,7 @@ class SeleneDeviceSkills(JsonStorage):
"""

def __init__(self, api=None):
path = os.path.join(xdg.BaseDirectory.save_data_path(BASE_FOLDER), 'skills.json')
path = os.path.join(xdg.BaseDirectory.save_data_path(get_xdg_base()), 'skills.json')
super().__init__(path)
if "skills" not in self:
self["skills"] = {}
Expand Down

0 comments on commit 71d4f62

Please sign in to comment.