Skip to content

Commit

Permalink
remove duplicate functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Sep 14, 2023
1 parent 6735313 commit cd1ee8c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 108 deletions.
3 changes: 1 addition & 2 deletions yacman/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from .alias import *

# For transition
from .yacman1 import YAMLConfigManager

from .yacman1 import YAMLConfigManager, select_config

from .yacman import *
2 changes: 1 addition & 1 deletion yacman/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.1-dev"
__version__ = "0.9.1-dev1"
88 changes: 0 additions & 88 deletions yacman/yacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,91 +437,3 @@ def read_yaml_file(filepath):
else:
return read_yaml_file(filepath)


def get_first_env_var(ev):
"""
Get the name and value of the first set environment variable
:param str | Iterable[str] ev: a list of the environment variable names
:return (str, str): name and the value of the environment variable
"""
if isinstance(ev, str):
ev = [ev]
elif not isinstance(ev, Iterable):
raise TypeError(
f"Env var must be single name or collection of names; got {type(ev)}"
)
# TODO: we should handle the null (not found) case, as client code is
# inclined to unpack, and ValueError guard is vague.
for v in ev:
try:
return v, os.environ[v]
except KeyError:
pass


def select_config(
config_filepath=None,
config_env_vars=None,
default_config_filepath=None,
check_exist=True,
on_missing=lambda fp: IOError(fp),
strict_env=False,
):
"""
Selects the config file to load.
This uses a priority ordering to first choose a config filepath if it's given,
but if not, then look in a priority list of environment variables and choose
the first available filepath to return.
:param str | NoneType config_filepath: direct filepath specification
:param Iterable[str] | NoneType config_env_vars: names of environment
variables to try for config filepaths
:param str default_config_filepath: default value if no other alternative
resolution succeeds
:param bool check_exist: whether to check for path existence as file
:param function(str) -> object on_missing: what to do with a filepath if it
doesn't exist
:param bool strict_env: whether to raise an exception if no file path provided
and environment variables do not point to any files
raise: OSError: when strict environment variables validation is not passed
"""

# First priority: given file
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}")
result = on_missing(config_filepath)
if isinstance(result, Exception):
raise result
return os.path.abspath(result)

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

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

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}")
selected_filepath = cfg_file
if selected_filepath is None and cfg_file and strict_env:
raise OSError(
f"Environment variable ({', '.join(config_env_vars)}) does "
f"not point to any existing file: {cfg_file}"
)
if selected_filepath is None:
# Third priority: default filepath
_LOGGER.info(
f"Using default config. No config found in env var: {str(config_env_vars)}"
)
return default_config_filepath
return (
os.path.abspath(selected_filepath) if selected_filepath else selected_filepath
)
44 changes: 27 additions & 17 deletions yacman/yacman1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from jsonschema import validate as _validate
from jsonschema.exceptions import ValidationError
from ubiquerg import create_lock, expandpath, is_url, make_lock_path, mkabs, remove_lock

from ._version import __version__
_LOGGER = logging.getLogger(__name__)
_LOGGER.debug(f"Using yacman version {__version__}")

# Hack for yaml string indexes
# Credit: Anthon
Expand Down Expand Up @@ -555,14 +556,15 @@ def get_first_env_var(ev):
pass



def select_config(
config_filepath=None,
config_filepath: str=None,
config_env_vars=None,
default_config_filepath=None,
check_exist=True,
default_config_filepath: str=None,
check_exist: bool=True,
on_missing=lambda fp: IOError(fp),
strict_env=False,
):
strict_env: bool=False,
) -> str:
"""
Selects the config file to load.
Expand Down Expand Up @@ -594,29 +596,37 @@ def select_config(
raise result
return os.path.abspath(result)

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

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

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}")
selected_filepath = cfg_file
if selected_filepath is None and cfg_file and strict_env:
raise OSError(
f"Environment variable ({', '.join(config_env_vars)}) does "
f"not point to any existing file: {cfg_file}"
)

else:
if strict_env:
raise OSError(
f"Environment variable ({', '.join(config_env_vars)}) does "
f"not point to any existing file: {cfg_file}"
)
else:
_LOGGER.info(f"Env var '{cfg_env_var}' file not found: '{cfg_file}'.")
if selected_filepath is None:
# Third priority: default filepath
_LOGGER.info(
f"Using default config. No config found in env var: {str(config_env_vars)}"
)
return default_config_filepath
if default_config_filepath:
_LOGGER.info(
f"Using default config. No config found in env var: {str(config_env_vars)}"
)
return default_config_filepath
else:
_LOGGER.info(f"Could not locate config file.")
return None
return (
os.path.abspath(selected_filepath) if selected_filepath else selected_filepath
)
Expand Down

0 comments on commit cd1ee8c

Please sign in to comment.