Skip to content

Commit 8ff0040

Browse files
authored
Merge pull request #555 from OpenCOMPES/config_renaming
use user platformdir also for user config
2 parents 975a506 + d283e93 commit 8ff0040

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

docs/user_guide/config.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ 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.
8-
* ``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`
7+
* ``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.
8+
* ``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`
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

1111
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).

src/sed/core/config.py

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

2121
USER_CONFIG_PATH = user_config_path(appname="sed", appauthor="OpenCOMPES", ensure_exists=True)
22+
SYSTEM_CONFIG_PATH = (
23+
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed")
24+
if platform.system() == "Windows"
25+
else Path("/etc/").joinpath("sed")
26+
)
2227
ENV_DIR = Path(".env")
2328

2429
# Configure logging
@@ -50,11 +55,11 @@ def parse_config(
5055
user_config (dict | str, optional): user-based config dictionary
5156
or file path. The loaded dictionary is completed with the user-based values,
5257
taking preference over system and default values.
53-
Defaults to the file ".sed/config.yaml" in the current user's home directory.
58+
Defaults to the file ".config/sed/config_v1.yaml" in the current user's home directory.
5459
system_config (dict | str, optional): system-wide config dictionary
5560
or file path. The loaded dictionary is completed with the system-wide values,
56-
taking preference over default values. Defaults to the file "/etc/sed/config.yaml"
57-
on linux, and "%ALLUSERSPROFILE%/sed/config.yaml" on windows.
61+
taking preference over default values. Defaults to the file "/etc/sed/config_v1.yaml"
62+
on linux, and "%ALLUSERSPROFILE%/sed/config_v1.yaml" on windows.
5863
default_config (dict | str, optional): default config dictionary
5964
or file path. The loaded dictionary is completed with the default values.
6065
Defaults to *package_dir*/config/default.yaml".
@@ -94,9 +99,7 @@ def parse_config(
9499
user_dict = copy.deepcopy(user_config)
95100
else:
96101
if user_config is None:
97-
user_config = str(
98-
Path.home().joinpath(".sed").joinpath("config.yaml"),
99-
)
102+
user_config = str(USER_CONFIG_PATH.joinpath("config_v1.yaml"))
100103
if Path(user_config).exists():
101104
user_dict = load_config(user_config)
102105
if verbose:
@@ -107,14 +110,7 @@ def parse_config(
107110
system_dict = copy.deepcopy(system_config)
108111
else:
109112
if system_config is None:
110-
if platform.system() in ["Linux", "Darwin"]:
111-
system_config = str(
112-
Path("/etc/").joinpath("sed").joinpath("config.yaml"),
113-
)
114-
elif platform.system() == "Windows":
115-
system_config = str(
116-
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed").joinpath("config.yaml"),
117-
)
113+
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config_v1.yaml"))
118114
if Path(system_config).exists():
119115
system_dict = load_config(system_config)
120116
if verbose:
@@ -282,31 +278,38 @@ def read_env_var(var_name: str) -> str | None:
282278
1. OS environment variables
283279
2. .env file in current directory
284280
3. .env file in user config directory
281+
4. .env file in system config directory
285282
286283
Args:
287284
var_name (str): Name of the environment variable to read
288285
289286
Returns:
290287
str | None: Value of the environment variable or None if not found
291288
"""
292-
# First check OS environment variables
289+
# 1. check OS environment variables
293290
value = os.getenv(var_name)
294291
if value is not None:
295292
logger.debug(f"Found {var_name} in OS environment variables")
296293
return value
297294

298-
# Then check .env in current directory
295+
# 2. check .env in current directory
299296
local_vars = _parse_env_file(ENV_DIR)
300297
if var_name in local_vars:
301298
logger.debug(f"Found {var_name} in ./.env file")
302299
return local_vars[var_name]
303300

304-
# Finally check .env in user config directory
301+
# 3. check .env in user config directory
305302
user_vars = _parse_env_file(USER_CONFIG_PATH / ".env")
306303
if var_name in user_vars:
307304
logger.debug(f"Found {var_name} in user config .env file")
308305
return user_vars[var_name]
309306

307+
# 4. check .env in system config directory
308+
system_vars = _parse_env_file(SYSTEM_CONFIG_PATH / ".env")
309+
if var_name in system_vars:
310+
logger.debug(f"Found {var_name} in system config .env file")
311+
return system_vars[var_name]
312+
310313
logger.debug(f"Environment variable {var_name} not found in any location")
311314
return None
312315

tests/test_config.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,18 +285,27 @@ def test_env_var_precedence(mock_env_file, tmp_path, monkeypatch): # noqa: ARG0
285285
# Create local .env directory if it doesn't exist
286286
local_env_dir = tmp_path / "local"
287287
local_env_dir.mkdir(exist_ok=True)
288+
system_env_dir = tmp_path / "system"
289+
system_env_dir.mkdir(exist_ok=True)
288290
monkeypatch.setattr("sed.core.config.ENV_DIR", local_env_dir / ".env")
291+
monkeypatch.setattr("sed.core.config.SYSTEM_CONFIG_PATH", system_env_dir)
289292

290293
# Set up test values in different locations
291294
os.environ["TEST_VAR"] = "os_value"
292295

293-
# Save to user config first (lowest precedence)
296+
# Save to system config first (4th precedence)
297+
with open(system_env_dir / ".env", "w") as f:
298+
f.write("TEST_VAR=system_value\n")
299+
300+
# Save to user config first (3rd precedence)
294301
save_env_var("TEST_VAR", "user_value")
295302

296-
# Create local .env file (medium precedence)
303+
# Create local .env file (2nd precedence)
297304
with open(local_env_dir / ".env", "w") as f:
298305
f.write("TEST_VAR=local_value\n")
299306

307+
assert read_env_var("TEST_VAR") == "os_value"
308+
300309
# Remove from OS env to test other precedence levels
301310
monkeypatch.delenv("TEST_VAR", raising=False)
302311
assert read_env_var("TEST_VAR") == "local_value"
@@ -305,8 +314,12 @@ def test_env_var_precedence(mock_env_file, tmp_path, monkeypatch): # noqa: ARG0
305314
(local_env_dir / ".env").unlink()
306315
assert read_env_var("TEST_VAR") == "user_value"
307316

308-
# Remove user config and should get None
317+
# Remove user config and should get system value
309318
(mock_env_file / ".env").unlink()
319+
assert read_env_var("TEST_VAR") == "system_value"
320+
321+
# Remove system config and should get None
322+
(system_env_dir / ".env").unlink()
310323
assert read_env_var("TEST_VAR") is None
311324

312325

0 commit comments

Comments
 (0)