Skip to content

Commit 321f2fe

Browse files
committed
use user platformdir also for user config
change user and system config files to config_v1.yaml add system wide .env
1 parent 1752d34 commit 321f2fe

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
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

2328
# Configure logging
2429
logger = setup_logging("config")
@@ -49,11 +54,11 @@ def parse_config(
4954
user_config (dict | str, optional): user-based config dictionary
5055
or file path. The loaded dictionary is completed with the user-based values,
5156
taking preference over system and default values.
52-
Defaults to the file ".sed/config.yaml" in the current user's home directory.
57+
Defaults to the file ".config/sed/config_v1.yaml" in the current user's home directory.
5358
system_config (dict | str, optional): system-wide config dictionary
5459
or file path. The loaded dictionary is completed with the system-wide values,
55-
taking preference over default values. Defaults to the file "/etc/sed/config.yaml"
56-
on linux, and "%ALLUSERSPROFILE%/sed/config.yaml" on windows.
60+
taking preference over default values. Defaults to the file "/etc/sed/config_v1.yaml"
61+
on linux, and "%ALLUSERSPROFILE%/sed/config_v1.yaml" on windows.
5762
default_config (dict | str, optional): default config dictionary
5863
or file path. The loaded dictionary is completed with the default values.
5964
Defaults to *package_dir*/config/default.yaml".
@@ -93,9 +98,7 @@ def parse_config(
9398
user_dict = copy.deepcopy(user_config)
9499
else:
95100
if user_config is None:
96-
user_config = str(
97-
Path.home().joinpath(".sed").joinpath("config.yaml"),
98-
)
101+
user_config = str(USER_CONFIG_PATH.joinpath("config_v1.yaml"))
99102
if Path(user_config).exists():
100103
user_dict = load_config(user_config)
101104
if verbose:
@@ -106,14 +109,7 @@ def parse_config(
106109
system_dict = copy.deepcopy(system_config)
107110
else:
108111
if system_config is None:
109-
if platform.system() in ["Linux", "Darwin"]:
110-
system_config = str(
111-
Path("/etc/").joinpath("sed").joinpath("config.yaml"),
112-
)
113-
elif platform.system() == "Windows":
114-
system_config = str(
115-
Path(os.environ["ALLUSERSPROFILE"]).joinpath("sed").joinpath("config.yaml"),
116-
)
112+
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config_v1.yaml"))
117113
if Path(system_config).exists():
118114
system_dict = load_config(system_config)
119115
if verbose:
@@ -281,31 +277,38 @@ def read_env_var(var_name: str) -> str | None:
281277
1. OS environment variables
282278
2. .env file in current directory
283279
3. .env file in user config directory
280+
4. .env file in system config directory
284281
285282
Args:
286283
var_name (str): Name of the environment variable to read
287284
288285
Returns:
289286
str | None: Value of the environment variable or None if not found
290287
"""
291-
# First check OS environment variables
288+
# 1. check OS environment variables
292289
value = os.getenv(var_name)
293290
if value is not None:
294291
logger.debug(f"Found {var_name} in OS environment variables")
295292
return value
296293

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

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

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

0 commit comments

Comments
 (0)