Skip to content

Commit 276b774

Browse files
committed
use default config dir also for user config
find .env files also in system config
1 parent 8d969e0 commit 276b774

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

docs/user_guide/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The config module contains a mechanism to collect configuration parameters from
44
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:
55

66
* ``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.
7-
* ``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.
7+
* ``user_config``: A config file provided by the user, stored as :file:`.config/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.
88
* ``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`
99
* ``default_config``: The default configuration shipped with the package. Typically, all parameters here should be overwritten by any of the other configuration files.
1010

src/sed/core/config.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
package_dir = os.path.dirname(find_spec("sed").origin)
1919

2020
USER_CONFIG_PATH = user_config_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True)
21+
SYSTEM_CONFIG_PATH = (
22+
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed")
23+
if platform.system() == "Windows"
24+
else Path("/etc/").joinpath("sed")
25+
)
2126

2227
# Configure logging
2328
logger = setup_logging("config")
@@ -48,7 +53,7 @@ def parse_config(
4853
user_config (dict | str, optional): user-based config dictionary
4954
or file path. The loaded dictionary is completed with the user-based values,
5055
taking preference over system and default values.
51-
Defaults to the file ".sed/config.yaml" in the current user's home directory.
56+
Defaults to the file ".config/sed/config.yaml" in the current user's home directory.
5257
system_config (dict | str, optional): system-wide config dictionary
5358
or file path. The loaded dictionary is completed with the system-wide values,
5459
taking preference over default values. Defaults to the file "/etc/sed/config.yaml"
@@ -91,9 +96,7 @@ def parse_config(
9196
user_dict = copy.deepcopy(user_config)
9297
else:
9398
if user_config is None:
94-
user_config = str(
95-
Path.home().joinpath(".sed").joinpath("config.yaml"),
96-
)
99+
user_config = str(USER_CONFIG_PATH.joinpath("config.yaml"))
97100
if Path(user_config).exists():
98101
user_dict = load_config(user_config)
99102
if verbose:
@@ -104,14 +107,7 @@ def parse_config(
104107
system_dict = copy.deepcopy(system_config)
105108
else:
106109
if system_config is None:
107-
if platform.system() in ["Linux", "Darwin"]:
108-
system_config = str(
109-
Path("/etc/").joinpath("sed").joinpath("config.yaml"),
110-
)
111-
elif platform.system() == "Windows":
112-
system_config = str(
113-
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed").joinpath("config.yaml"),
114-
)
110+
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config.yaml"))
115111
if Path(system_config).exists():
116112
system_dict = load_config(system_config)
117113
if verbose:
@@ -269,31 +265,38 @@ def read_env_var(var_name: str) -> str | None:
269265
1. OS environment variables
270266
2. .env file in current directory
271267
3. .env file in user config directory
268+
4. .env file in system config directory
272269
273270
Args:
274271
var_name (str): Name of the environment variable to read
275272
276273
Returns:
277274
str | None: Value of the environment variable or None if not found
278275
"""
279-
# First check OS environment variables
276+
# 1. check OS environment variables
280277
value = os.getenv(var_name)
281278
if value is not None:
282279
logger.debug(f"Found {var_name} in OS environment variables")
283280
return value
284281

285-
# Then check .env in current directory
282+
# 2. check .env in current directory
286283
local_vars = _parse_env_file(Path(".env"))
287284
if var_name in local_vars:
288285
logger.debug(f"Found {var_name} in ./.env file")
289286
return local_vars[var_name]
290287

291-
# Finally check .env in user config directory
288+
# 3. check .env in user config directory
292289
user_vars = _parse_env_file(USER_CONFIG_PATH / ".env")
293290
if var_name in user_vars:
294291
logger.debug(f"Found {var_name} in user config .env file")
295292
return user_vars[var_name]
296293

294+
# 4. check .env in system config directory
295+
system_vars = _parse_env_file(SYSTEM_CONFIG_PATH / ".env")
296+
if var_name in system_vars:
297+
logger.debug(f"Found {var_name} in system config .env file")
298+
return system_vars[var_name]
299+
297300
logger.debug(f"Environment variable {var_name} not found in any location")
298301
return None
299302

0 commit comments

Comments
 (0)