Skip to content

Commit

Permalink
use user platformdir also for user config
Browse files Browse the repository at this point in the history
change user and system config files to config_v1.yaml
add system wide .env
  • Loading branch information
rettigl committed Jan 20, 2025
1 parent 1752d34 commit 321f2fe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
4 changes: 2 additions & 2 deletions docs/user_guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ The config module contains a mechanism to collect configuration parameters from
It will load an (optional) provided config file, or alternatively use a passed python dictionary as initial config dictionary, and subsequently look for the following additional config files to load:

* ``folder_config``: A config file of name :file:`sed_config.yaml` in the current working directory. This is mostly intended to pass calibration parameters of the workflow between different notebook instances.
* ``user_config``: A config file provided by the user, stored as :file:`.sed/config.yaml` in the current user's home directly. This is intended to give a user the option for individual configuration modifications of system settings.
* ``system_config``: A config file provided by the system administrator, stored as :file:`/etc/sed/config.yaml` on Linux-based systems, and :file:`%ALLUSERSPROFILE%/sed/config.yaml` on Windows. This should provide all necessary default parameters for using the sed processor with a given setup. For an example for an mpes setup, see :ref:`example_config`
* ``user_config``: A config file provided by the user, stored as :file:`.config/sed/config_v1.yaml` in the current user's home directly. This is intended to give a user the option for individual configuration modifications of system settings.
* ``system_config``: A config file provided by the system administrator, stored as :file:`/etc/sed/config_v1.yaml` on Linux-based systems, and :file:`%ALLUSERSPROFILE%/sed/config_v1.yaml` on Windows. This should provide all necessary default parameters for using the sed processor with a given setup. For an example for an mpes setup, see :ref:`example_config`
* ``default_config``: The default configuration shipped with the package. Typically, all parameters here should be overwritten by any of the other configuration files.

The config mechanism returns the combined dictionary, and reports the loaded configuration files. In order to disable or overwrite any of the configuration files, they can be also given as optional parameters (path to a file, or python dictionary).
Expand Down
37 changes: 20 additions & 17 deletions src/sed/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
package_dir = os.path.dirname(find_spec("sed").origin)

USER_CONFIG_PATH = user_config_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True)
SYSTEM_CONFIG_PATH = (
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed")
if platform.system() == "Windows"
else Path("/etc/").joinpath("sed")
)

# Configure logging
logger = setup_logging("config")
Expand Down Expand Up @@ -49,11 +54,11 @@ def parse_config(
user_config (dict | str, optional): user-based config dictionary
or file path. The loaded dictionary is completed with the user-based values,
taking preference over system and default values.
Defaults to the file ".sed/config.yaml" in the current user's home directory.
Defaults to the file ".config/sed/config_v1.yaml" in the current user's home directory.
system_config (dict | str, optional): system-wide config dictionary
or file path. The loaded dictionary is completed with the system-wide values,
taking preference over default values. Defaults to the file "/etc/sed/config.yaml"
on linux, and "%ALLUSERSPROFILE%/sed/config.yaml" on windows.
taking preference over default values. Defaults to the file "/etc/sed/config_v1.yaml"
on linux, and "%ALLUSERSPROFILE%/sed/config_v1.yaml" on windows.
default_config (dict | str, optional): default config dictionary
or file path. The loaded dictionary is completed with the default values.
Defaults to *package_dir*/config/default.yaml".
Expand Down Expand Up @@ -93,9 +98,7 @@ def parse_config(
user_dict = copy.deepcopy(user_config)
else:
if user_config is None:
user_config = str(
Path.home().joinpath(".sed").joinpath("config.yaml"),
)
user_config = str(USER_CONFIG_PATH.joinpath("config_v1.yaml"))
if Path(user_config).exists():
user_dict = load_config(user_config)
if verbose:
Expand All @@ -106,14 +109,7 @@ def parse_config(
system_dict = copy.deepcopy(system_config)
else:
if system_config is None:
if platform.system() in ["Linux", "Darwin"]:
system_config = str(
Path("/etc/").joinpath("sed").joinpath("config.yaml"),
)
elif platform.system() == "Windows":
system_config = str(
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed").joinpath("config.yaml"),
)
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config_v1.yaml"))
if Path(system_config).exists():
system_dict = load_config(system_config)
if verbose:
Expand Down Expand Up @@ -281,31 +277,38 @@ def read_env_var(var_name: str) -> str | None:
1. OS environment variables
2. .env file in current directory
3. .env file in user config directory
4. .env file in system config directory
Args:
var_name (str): Name of the environment variable to read
Returns:
str | None: Value of the environment variable or None if not found
"""
# First check OS environment variables
# 1. check OS environment variables
value = os.getenv(var_name)
if value is not None:
logger.debug(f"Found {var_name} in OS environment variables")
return value

# Then check .env in current directory
# 2. check .env in current directory
local_vars = _parse_env_file(Path(".env"))
if var_name in local_vars:
logger.debug(f"Found {var_name} in ./.env file")
return local_vars[var_name]

# Finally check .env in user config directory
# 3. check .env in user config directory
user_vars = _parse_env_file(USER_CONFIG_PATH / ".env")
if var_name in user_vars:
logger.debug(f"Found {var_name} in user config .env file")
return user_vars[var_name]

# 4. check .env in system config directory
system_vars = _parse_env_file(SYSTEM_CONFIG_PATH / ".env")
if var_name in system_vars:
logger.debug(f"Found {var_name} in system config .env file")
return system_vars[var_name]

logger.debug(f"Environment variable {var_name} not found in any location")
return None

Expand Down

0 comments on commit 321f2fe

Please sign in to comment.