Skip to content

Commit f1078b6

Browse files
committed
use default user config path
look for .env file also in system config folder
1 parent 6ba918c commit f1078b6

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

docs/specsanalyzer/config.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ The config module contains a mechanics 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:`specs_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:`.specsanalyzer/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/specsanalyzer/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/specsanalyzer/config.yaml` on Linux-based systems, and :file:`%ALLUSERSPROFILE%/specsanalyzer/config.yaml` on Windows. This should provide all necessary default parameters for using the specsanalyzer processor with a given setup. For an example for the setup at the Fritz Haber Institute 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/specsanalyzer/config.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414

1515
package_dir = os.path.dirname(find_spec("specsanalyzer").origin)
1616

17-
USER_CONFIG_PATH = user_config_path(appname="specsscan", appauthor="OpenCOMPES", ensure_exists=True)
17+
USER_CONFIG_PATH = user_config_path(
18+
appname="specsanalyzer",
19+
appauthor="OpenCOMPES",
20+
ensure_exists=True,
21+
)
22+
SYSTEM_CONFIG_PATH = (
23+
Path(os.environ["ALLUSERSPROFILE"]).joinpath("specsanalyzer")
24+
if platform.system() == "Windows"
25+
else Path("/etc/").joinpath("specsanalyzer")
26+
)
1827

1928
# Configure logging
2029
logger = setup_logging("config")
@@ -44,7 +53,8 @@ def parse_config(
4453
user_config (dict | str, optional): user-based config dictionary
4554
or file path. The loaded dictionary is completed with the user-based values,
4655
taking preference over system and default values.
47-
Defaults to the file ".specsanalyzer/config.yaml" in the current user's home directory.
56+
Defaults to the file ".config/specsanalyzer/config.yaml" in the current user's home
57+
directory.
4858
system_config (dict | str, optional): system-wide config dictionary
4959
or file path. The loaded dictionary is completed with the system-wide values,
5060
taking preference over default values.
@@ -87,9 +97,7 @@ def parse_config(
8797
user_dict = user_config
8898
else:
8999
if user_config is None:
90-
user_config = str(
91-
Path.home().joinpath(".specsanalyzer").joinpath("config.yaml"),
92-
)
100+
user_config = str(USER_CONFIG_PATH.joinpath("config.yaml"))
93101
if Path(user_config).exists():
94102
user_dict = load_config(user_config)
95103
if verbose:
@@ -100,16 +108,7 @@ def parse_config(
100108
system_dict = system_config
101109
else:
102110
if system_config is None:
103-
if platform.system() in ["Linux", "Darwin"]:
104-
system_config = str(
105-
Path("/etc/").joinpath("specsanalyzer").joinpath("config.yaml"),
106-
)
107-
elif platform.system() == "Windows":
108-
system_config = str(
109-
Path(os.environ["ALLUSERSPROFILE"])
110-
.joinpath("specsanalyzer")
111-
.joinpath("config.yaml"),
112-
)
111+
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config.yaml"))
113112
if Path(system_config).exists():
114113
system_dict = load_config(system_config)
115114
if verbose:
@@ -261,31 +260,38 @@ def read_env_var(var_name: str) -> str | None:
261260
1. OS environment variables
262261
2. .env file in current directory
263262
3. .env file in user config directory
263+
4. .env file in system config directory
264264
265265
Args:
266266
var_name (str): Name of the environment variable to read
267267
268268
Returns:
269269
str | None: Value of the environment variable or None if not found
270270
"""
271-
# First check OS environment variables
271+
# 1. check OS environment variables
272272
value = os.getenv(var_name)
273273
if value is not None:
274274
logger.debug(f"Found {var_name} in OS environment variables")
275275
return value
276276

277-
# Then check .env in current directory
277+
# 2. check .env in current directory
278278
local_vars = _parse_env_file(Path(".env"))
279279
if var_name in local_vars:
280280
logger.debug(f"Found {var_name} in ./.env file")
281281
return local_vars[var_name]
282282

283-
# Finally check .env in user config directory
283+
# 3. check .env in user config directory
284284
user_vars = _parse_env_file(USER_CONFIG_PATH / ".env")
285285
if var_name in user_vars:
286286
logger.debug(f"Found {var_name} in user config .env file")
287287
return user_vars[var_name]
288288

289+
# 4. check .env in system config directory
290+
system_vars = _parse_env_file(SYSTEM_CONFIG_PATH / ".env")
291+
if var_name in system_vars:
292+
logger.debug(f"Found {var_name} in system config .env file")
293+
return system_vars[var_name]
294+
289295
logger.debug(f"Environment variable {var_name} not found in any location")
290296
return None
291297

0 commit comments

Comments
 (0)