Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve env vars handling #54

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions ovos_config/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@

def get_ovos_config():
"""
Goes through all possible ovos.conf paths and loads them in order. Default
`base_folder` and `config_filename` are overridden by envvars
`OVOS_CONFIG_BASE_FOLDER` and `OVOS_CONFIG_FILENAME`, respectively.
Goes through all possible ovos.conf paths and loads them in order.
`base_folder`, `config_filename` and "default_config_path" are overridden by envvars
`OVOS_CONFIG_BASE_FOLDER`,`OVOS_CONFIG_FILENAME` and `OVOS_DEFAULT_CONFIG`, respectively.
Submodule overrides are applied to the final config if defined for the
calling module.
eg, if neon is calling this method then neon config overrides are loaded
Expand All @@ -67,15 +67,9 @@ def get_ovos_config():
from ovos_utils.system import is_running_from_module

# populate default values
config = {"xdg": True,
"base_folder": os.environ.get("OVOS_CONFIG_BASE_FOLDER") or
"mycroft",
"config_filename": os.environ.get("OVOS_CONFIG_FILENAME") or
"mycroft.conf"}
try:
config["default_config_path"] = _oloc.find_default_config()
except FileNotFoundError: # not a mycroft device
config["default_config_path"] = join(dirname(__file__), "mycroft.conf")
config = {"base_folder": "mycroft",
"config_filename": "mycroft.conf",
"default_config_path": f"{dirname(__file__)}/mycroft.conf"}

# load ovos.conf
for path in get_ovos_default_config_paths():
Expand All @@ -85,6 +79,11 @@ def get_ovos_config():
# tolerate bad json TODO proper exception (?)
pass

# let's check for os.env overrides, those take precedence over ovos.conf default values
config["base_folder"] = os.environ.get("OVOS_CONFIG_BASE_FOLDER") or config["base_folder"]
config["config_filename"] = os.environ.get("OVOS_CONFIG_FILENAME") or config["config_filename"]
config["default_config_path"] = os.environ.get("OVOS_DEFAULT_CONFIG") or config["default_config_path"]

# 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
Expand All @@ -100,7 +99,7 @@ def get_ovos_config():
if is_running_from_module(k):
config = merge_dict(config, cores[subcores[k]])
break

return config


Expand Down Expand Up @@ -169,6 +168,7 @@ def set_xdg_base(folder_name):
from ovos_utils.log import LOG

LOG.info(f"XDG base folder set to: '{folder_name}'")
os.environ["OVOS_CONFIG_BASE_FOLDER"] = folder_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to look into side effects here since os.env could affect code outside of this module?

save_ovos_config({"base_folder": folder_name})


Expand All @@ -185,6 +185,7 @@ def set_config_filename(file_name, core_folder=None):
if core_folder:
set_xdg_base(core_folder)
LOG.info(f"config filename set to: '{file_name}'")
os.environ["OVOS_CONFIG_FILENAME"] = file_name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to look into side effects here since os.env could affect code outside of this module?

save_ovos_config({"config_filename": file_name})


Expand All @@ -197,7 +198,7 @@ def get_config_filename():
return get_ovos_config().get("config_filename") or "mycroft.conf"


def set_default_config(file_path=None):
def set_default_config(file_path=f"{dirname(__file__)}/mycroft.conf"):
""" full path to default config file to be used
NOTE: this is a full path, not a directory! "config_filename" parameter is not used here

Expand All @@ -207,6 +208,6 @@ def set_default_config(file_path=None):
"""
from ovos_utils.log import LOG

file_path = file_path or _oloc.find_default_config()
LOG.info(f"default config file changed to: {file_path}")
os.environ["OVOS_DEFAULT_CONFIG"] = file_path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to look into side effects here since os.env could affect code outside of this module?

save_ovos_config({"default_config_path": file_path})
Loading