Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Sep 14, 2023
2 parents cd1ee8c + 8f44e07 commit c3324b2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format.

## [0.9.1] -- unreleased
## [0.9.1] -- 2023-06-15

## Added
- `.get()` function on `YAMLConfigManager` object
- `.priority_get()` function on `YAMLConfigManager` object

## [0.9.0] -- 2022-05-04

Expand Down
1 change: 0 additions & 1 deletion yacman/yacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,3 @@ def read_yaml_file(filepath):
return yaml.safe_load(text)
else:
return read_yaml_file(filepath)

18 changes: 12 additions & 6 deletions yacman/yacman1.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ def select_config(
check_exist: bool=True,
on_missing=lambda fp: IOError(fp),
strict_env: bool=False,
config_name=None,
) -> str:
"""
Selects the config file to load.
Expand All @@ -586,27 +587,32 @@ def select_config(
"""

# First priority: given file
if type(config_name) is str:
config_name = f"{config_name} "
else:
config_name = ""

if config_filepath:
config_filepath = os.path.expandvars(config_filepath)
if not check_exist or os.path.isfile(config_filepath):
return os.path.abspath(config_filepath)
_LOGGER.error(f"Config file path isn't a file: {config_filepath}")
_LOGGER.error(f"{config_name}config file path isn't a file: {config_filepath}")
result = on_missing(config_filepath)
if isinstance(result, Exception):
raise result
return os.path.abspath(result)

_LOGGER.debug("No local config file was provided.")
_LOGGER.debug("No local {config_name}config file was provided.")
selected_filepath = None

# Second priority: environment variables (in order)
if config_env_vars:
_LOGGER.debug(f"Checking environment variables: {config_env_vars}")
_LOGGER.debug(f"Checking environment variables '{config_env_vars}' for {config_name}config")

cfg_env_var, cfg_file = get_first_env_var(config_env_vars) or ["", ""]

if not check_exist or os.path.isfile(cfg_file):
_LOGGER.debug(f"Found config file in {cfg_env_var}: {cfg_file}")
_LOGGER.debug(f"Found {config_name}config file in {cfg_env_var}: {cfg_file}")
selected_filepath = cfg_file

else:
Expand All @@ -621,11 +627,11 @@ def select_config(
# Third priority: default filepath
if default_config_filepath:
_LOGGER.info(
f"Using default config. No config found in env var: {str(config_env_vars)}"
f"Using default {config_name}config. You may specify in env var: {str(config_env_vars)}"
)
return default_config_filepath
else:
_LOGGER.info(f"Could not locate config file.")
_LOGGER.info(f"Could not locate {config_name}config file.")
return None
return (
os.path.abspath(selected_filepath) if selected_filepath else selected_filepath
Expand Down

0 comments on commit c3324b2

Please sign in to comment.