Skip to content

Commit

Permalink
use default user config path
Browse files Browse the repository at this point in the history
look for .env file also in system config folder
  • Loading branch information
rettigl committed Jan 16, 2025
1 parent 6ba918c commit f1078b6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/specsanalyzer/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The config module contains a mechanics 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:`specs_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:`.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.
* ``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.
* ``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`
* ``default_config``: The default configuration shipped with the package. Typically, all parameters here should be overwritten by any of the other configuration files.

Expand Down
42 changes: 24 additions & 18 deletions src/specsanalyzer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,16 @@

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

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

# Configure logging
logger = setup_logging("config")
Expand Down Expand Up @@ -44,7 +53,8 @@ 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 ".specsanalyzer/config.yaml" in the current user's home directory.
Defaults to the file ".config/specsanalyzer/config.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.
Expand Down Expand Up @@ -87,9 +97,7 @@ def parse_config(
user_dict = user_config
else:
if user_config is None:
user_config = str(
Path.home().joinpath(".specsanalyzer").joinpath("config.yaml"),
)
user_config = str(USER_CONFIG_PATH.joinpath("config.yaml"))
if Path(user_config).exists():
user_dict = load_config(user_config)
if verbose:
Expand All @@ -100,16 +108,7 @@ def parse_config(
system_dict = system_config
else:
if system_config is None:
if platform.system() in ["Linux", "Darwin"]:
system_config = str(
Path("/etc/").joinpath("specsanalyzer").joinpath("config.yaml"),
)
elif platform.system() == "Windows":
system_config = str(
Path(os.environ["ALLUSERSPROFILE"])
.joinpath("specsanalyzer")
.joinpath("config.yaml"),
)
system_config = str(SYSTEM_CONFIG_PATH.joinpath("config.yaml"))
if Path(system_config).exists():
system_dict = load_config(system_config)
if verbose:
Expand Down Expand Up @@ -261,31 +260,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 f1078b6

Please sign in to comment.