Skip to content

Commit 6c2fe55

Browse files
authored
feat/ovos_conf from ovos_utils (#34)
* feat/ovos_conf from ovos_utils logic moved to ovos_utils OpenVoiceOS/ovos-utils#12 * bump ovos_utils authored-by: jarbasai <jarbasai@mailfence.com>
1 parent 3e0da73 commit 6c2fe55

File tree

17 files changed

+69
-131
lines changed

17 files changed

+69
-131
lines changed

mycroft/client/text/text_client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
from mycroft.messagebus.client import MessageBusClient
3333
from mycroft.messagebus.message import Message
3434
from mycroft.util.log import LOG
35-
from mycroft.configuration import Configuration, BASE_FOLDER
36-
from mycroft.configuration.ovos import is_using_xdg
35+
from mycroft.configuration import Configuration
36+
from ovos_utils.configuration import is_using_xdg, get_xdg_base
3737

3838
import locale
3939

@@ -187,7 +187,7 @@ def load_settings():
187187

188188
# Check XDG_CONFIG_DIR
189189
if config_file is None:
190-
for conf_dir in xdg.BaseDirectory.load_config_paths(BASE_FOLDER):
190+
for conf_dir in xdg.BaseDirectory.load_config_paths(get_xdg_base()):
191191
xdg_file = os.path.join(conf_dir, filename)
192192
if os.path.isfile(xdg_file):
193193
config_file = xdg_file
@@ -230,7 +230,7 @@ def save_settings():
230230
config_file = path
231231
else:
232232
config_file = os.path.join(xdg.BaseDirectory.xdg_config_home,
233-
BASE_FOLDER, filename)
233+
get_xdg_base(), filename)
234234

235235
with io.open(config_file, 'w') as f:
236236
f.write(str(json.dumps(config, ensure_ascii=False)))

mycroft/configuration/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,5 @@
1616
from mycroft.configuration.locale import set_default_lf_lang, setup_locale, \
1717
set_default_tz, set_default_lang, get_default_tz, get_default_lang, \
1818
get_config_tz, get_primary_lang_code, load_languages, load_language
19-
from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG, \
20-
get_xdg_config_locations, BASE_FOLDER, DEFAULT_CONFIG
21-
from mycroft.configuration.ovos import is_using_xdg, get_ovos_config
19+
from mycroft.configuration.locations import SYSTEM_CONFIG, USER_CONFIG, DEFAULT_CONFIG
20+
from ovos_utils.configuration import get_ovos_config, is_using_xdg, get_xdg_config_locations

mycroft/configuration/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from requests import RequestException
2121

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

201201

202202
def _get_system_constraints():

mycroft/configuration/locations.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,28 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import os
15-
from time import sleep
1615
from os.path import join, dirname, expanduser, exists
16+
from time import sleep
1717

1818
import xdg.BaseDirectory
1919

20-
from mycroft.configuration.ovos import get_ovos_config
20+
from ovos_utils.configuration import get_ovos_config, get_xdg_config_locations
2121

2222
# check for path overrides
2323
_ovos_cfg = get_ovos_config()
24-
BASE_FOLDER = _ovos_cfg["base_folder"]
25-
CONFIG_FILE_NAME = _ovos_cfg["config_filename"]
26-
DEFAULT_CONFIG = _ovos_cfg["default_config_path"] or \
27-
join(dirname(__file__), CONFIG_FILE_NAME)
2824

25+
DEFAULT_CONFIG = _ovos_cfg["default_config_path"] or \
26+
join(dirname(__file__), "mycroft.conf")
2927
SYSTEM_CONFIG = os.environ.get('MYCROFT_SYSTEM_CONFIG',
30-
f'/etc/{BASE_FOLDER}/{CONFIG_FILE_NAME}')
28+
f'/etc/{_ovos_cfg["base_folder"]}/{_ovos_cfg["config_filename"]}')
3129
# TODO: remove in 22.02
3230
# Make sure we support the old location still
3331
# Deprecated and will be removed eventually
34-
OLD_USER_CONFIG = join(expanduser('~'), '.' + BASE_FOLDER, CONFIG_FILE_NAME)
35-
USER_CONFIG = join(xdg.BaseDirectory.xdg_config_home, BASE_FOLDER, CONFIG_FILE_NAME)
32+
OLD_USER_CONFIG = join(expanduser('~'), '.' + _ovos_cfg["base_folder"], _ovos_cfg["config_filename"])
33+
USER_CONFIG = join(xdg.BaseDirectory.xdg_config_home, _ovos_cfg["base_folder"], _ovos_cfg["config_filename"])
3634
REMOTE_CONFIG = "mycroft.ai"
3735
WEB_CONFIG_CACHE = os.environ.get('MYCROFT_WEB_CACHE') or \
38-
join(xdg.BaseDirectory.xdg_config_home, BASE_FOLDER, 'web_cache.json')
39-
40-
41-
def get_xdg_config_locations():
42-
# This includes both the user config and
43-
# /etc/xdg/mycroft/mycroft.conf
44-
xdg_paths = list(reversed(
45-
[join(p, CONFIG_FILE_NAME)
46-
for p in xdg.BaseDirectory.load_config_paths(BASE_FOLDER)]
47-
))
48-
return xdg_paths
36+
join(xdg.BaseDirectory.xdg_config_home, _ovos_cfg["base_folder"], 'web_cache.json')
4937

5038

5139
def __ensure_folder_exists(path):

mycroft/configuration/ovos.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -62,60 +62,4 @@
6262
}
6363
}
6464
"""
65-
from importlib.util import find_spec
66-
from os.path import isfile, dirname, join
67-
68-
import xdg.BaseDirectory
69-
from mycroft.util.json_helper import load_commented_json, merge_dict
70-
71-
72-
def get_ovos_config():
73-
config = {"xdg": True,
74-
"base_folder": "mycroft",
75-
"config_filename": "mycroft.conf",
76-
"default_config_path": join(dirname(__file__), "mycroft.conf")}
77-
78-
try:
79-
if isfile("/etc/OpenVoiceOS/ovos.conf"):
80-
config = merge_dict(config,
81-
load_commented_json(
82-
"/etc/OpenVoiceOS/ovos.conf"))
83-
elif isfile("/etc/mycroft/ovos.conf"):
84-
config = merge_dict(config,
85-
load_commented_json("/etc/mycroft/ovos.conf"))
86-
except:
87-
# tolerate bad json TODO proper exception (?)
88-
pass
89-
90-
# This includes both the user config and
91-
# /etc/xdg/OpenVoiceOS/ovos.conf
92-
for p in xdg.BaseDirectory.load_config_paths("OpenVoiceOS"):
93-
if isfile(join(p, "ovos.conf")):
94-
try:
95-
xdg_cfg = load_commented_json(join(p, "ovos.conf"))
96-
config = merge_dict(config, xdg_cfg)
97-
except:
98-
# tolerate bad json TODO proper exception (?)
99-
pass
100-
101-
# let's check for derivatives specific configs
102-
# the assumption is that these cores are exclusive to each other,
103-
# this will never find more than one override
104-
# TODO this works if using dedicated .venvs what about system installs?
105-
cores = config.get("module_overrides") or {}
106-
for k in cores:
107-
if find_spec(k):
108-
config = merge_dict(config, cores[k])
109-
break
110-
else:
111-
subcores = config.get("submodule_mappings") or {}
112-
for k in subcores:
113-
if find_spec(k):
114-
config = merge_dict(config, cores[subcores[k]])
115-
break
116-
117-
return config
118-
119-
120-
def is_using_xdg():
121-
return get_ovos_config().get("xdg", True)
65+
from ovos_utils.configuration import get_ovos_config, is_using_xdg

mycroft/filesystem/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import os
1616
import shutil
1717
from os.path import join, expanduser, isdir
18-
import mycroft.configuration
1918
import xdg.BaseDirectory
19+
from ovos_utils.configuration import is_using_xdg, get_xdg_base
2020

2121

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

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

40-
if mycroft.configuration.is_using_xdg():
40+
if is_using_xdg():
4141
xdg_path = expanduser(
42-
f'{xdg.BaseDirectory.xdg_config_home}/{mycroft.configuration.BASE_FOLDER}/{path}')
42+
f'{xdg.BaseDirectory.xdg_config_home}/{get_xdg_base()}/{path}')
4343
# Migrate from the old location if it still exists
4444
if isdir(path) and not isdir(xdg_path):
4545
shutil.move(path, xdg_path)

mycroft/lock/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#
1515
from signal import getsignal, signal, SIGKILL, SIGINT, SIGTERM, \
1616
SIG_DFL, default_int_handler, SIG_IGN # signals
17-
from mycroft.configuration import BASE_FOLDER
17+
from ovos_utils.configuration import get_xdg_base
1818
import os # Operating System functions
1919

2020
#
@@ -98,7 +98,7 @@ class Lock: # python 3+ 'class Lock'
9898

9999
#
100100
# Class constants
101-
DIRECTORY = get_temp_path(BASE_FOLDER)
101+
DIRECTORY = get_temp_path(get_xdg_base())
102102
FILE = '/{}.pid'
103103

104104
#

mycroft/skills/event_scheduler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
from os.path import isfile, join, expanduser
2424
import xdg.BaseDirectory
2525

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

6969
core_conf = Configuration.get(remote=False)
70-
data_dir = core_conf.get('data_dir', xdg.BaseDirectory.save_data_path(BASE_FOLDER))
70+
data_dir = core_conf.get('data_dir', xdg.BaseDirectory.save_data_path(get_xdg_base()))
7171
old_schedule_path = join(expanduser(data_dir), schedule_file)
7272

7373
self.schedule_file = old_schedule_path
7474
if is_using_xdg():
7575
new_schedule_path = join(
76-
xdg.BaseDirectory.load_first_config(BASE_FOLDER), schedule_file)
76+
xdg.BaseDirectory.load_first_config(get_xdg_base()), schedule_file)
7777
if isfile(old_schedule_path):
7878
shutil.move(old_schedule_path, new_schedule_path)
7979
self.schedule_file = new_schedule_path

mycroft/skills/msm_wrapper.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727
from mycroft.configuration import Configuration
2828
from mycroft.configuration.ovos import is_using_xdg
2929
from combo_lock import ComboLock
30-
from mycroft.util.log import LOG
31-
from mycroft.util.file_utils import get_temp_path
32-
from mycroft.configuration import BASE_FOLDER
3330

3431
from mock_msm import \
3532
MycroftSkillsManager as MockMSM, \
3633
SkillRepo as MockSkillRepo
3734

35+
from ovos_utils.configuration import get_xdg_base
36+
from mycroft.util.file_utils import get_temp_path
37+
from mycroft.util.log import LOG
38+
3839
try:
3940
from msm.exceptions import MsmException
4041
from msm import MycroftSkillsManager, SkillRepo
@@ -70,16 +71,16 @@ def get_skills_directory(conf=None):
7071
# if xdg is disabled, ignore it!
7172
elif not is_using_xdg():
7273
# old style mycroft-core skills path definition
73-
data_dir = conf.get("data_dir") or "/opt/" + BASE_FOLDER
74+
data_dir = conf.get("data_dir") or "/opt/" + get_xdg_base()
7475
folder = conf["skills"].get("msm", {}).get("directory", "skills")
7576
skills_folder = path.join(data_dir, folder)
7677
else:
77-
skills_folder = xdg.BaseDirectory.save_data_path(BASE_FOLDER + '/skills')
78+
skills_folder = xdg.BaseDirectory.save_data_path(get_xdg_base() + '/skills')
7879
# create folder if needed
7980
try:
8081
makedirs(skills_folder, exist_ok=True)
8182
except PermissionError: # old style /opt/mycroft/skills not available
82-
skills_folder = xdg.BaseDirectory.save_data_path(BASE_FOLDER + '/skills')
83+
skills_folder = xdg.BaseDirectory.save_data_path(get_xdg_base() + '/skills')
8384
makedirs(skills_folder, exist_ok=True)
8485

8586
return path.expanduser(skills_folder)
@@ -109,7 +110,7 @@ def build_msm_config(device_config: dict) -> MsmConfig:
109110
msm_config = device_config['skills'].get('msm', {})
110111
msm_repo_config = msm_config.get('repo', {})
111112
enclosure_config = device_config.get('enclosure', {})
112-
data_dir = path.expanduser(device_config.get('data_dir', xdg.BaseDirectory.save_data_path(BASE_FOLDER)))
113+
data_dir = path.expanduser(device_config.get('data_dir', xdg.BaseDirectory.save_data_path(get_xdg_base())))
113114
skills_dir = get_skills_directory(device_config)
114115
old_skills_dir = path.join(data_dir, msm_config.get('directory', "skills"))
115116

mycroft/skills/mycroft_skill/mycroft_skill.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from mycroft.audio import wait_while_speaking
3434
from ovos_utils.enclosure.api import EnclosureAPI
3535
from mycroft.gui import SkillGUI
36-
from mycroft.configuration import Configuration, BASE_FOLDER
36+
from ovos_utils.configuration import is_using_xdg, get_xdg_base
37+
from mycroft.configuration import Configuration
3738
from mycroft.dialog import load_dialogs
3839
from mycroft.filesystem import FileSystemAccess
3940
from mycroft.messagebus.message import Message, dig_for_message
@@ -189,15 +190,15 @@ def _init_settings(self):
189190
# Otherwise save to XDG_CONFIG_DIR
190191
if not self.settings_write_path.joinpath('settings.json').exists():
191192
self.settings_write_path = Path(xdg.BaseDirectory.save_config_path(
192-
BASE_FOLDER, 'skills', basename(self.root_dir)))
193+
get_xdg_base(), 'skills', basename(self.root_dir)))
193194

194195
# To not break existing setups,
195196
# read from skill directory if the settings file exists there
196197
settings_read_path = Path(self.root_dir)
197198

198199
# Then, check XDG_CONFIG_DIR
199200
if not settings_read_path.joinpath('settings.json').exists():
200-
for path in xdg.BaseDirectory.load_config_paths(BASE_FOLDER, 'skills',
201+
for path in xdg.BaseDirectory.load_config_paths(get_xdg_base(), 'skills',
201202
basename(self.root_dir)):
202203
path = Path(path)
203204
# If there is a settings file here, use it

0 commit comments

Comments
 (0)