Skip to content

Commit

Permalink
add filepath when sourced from file
Browse files Browse the repository at this point in the history
  • Loading branch information
nsheff committed Oct 9, 2024
2 parents bee3df1 + 29f022c commit 62dc575
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
74 changes: 53 additions & 21 deletions yacman/yacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,32 +439,64 @@ def _warn_deprecated(obj):
def load_yaml(filepath):
"""Load a yaml file into a python dict"""

def read_yaml_file(filepath):
"""
Read a YAML file
:param str filepath: path to the file to read
:return dict: read data
"""
with open(filepath, "r") as f:
data = yaml.safe_load(f)
return data
def load_yaml(filepath: Union[str, Path]) -> dict:
"""
Load a local or remote YAML file into a Python dict
:param str filepath: path to the file to read
:raises ConnectionError: if the remote YAML file reading fails
:return dict: loaded yaml data
"""
if is_url(filepath):
_LOGGER.debug(f"Got URL: {filepath}")
try: # python3
from urllib.error import HTTPError
from urllib.request import urlopen
except: # python2
from urllib2 import URLError as HTTPError
from urllib2 import urlopen
try:
response = urlopen(filepath)
except HTTPError as e:
raise e
data = response.read() # a `bytes` object
text = data.decode("utf-8")
return yaml.safe_load(text)
except Exception as e:
raise ConnectionError(
f"Could not load remote file: {filepath}. "
f"Original exception: {getattr(e, 'message', repr(e))}"
)
else:
data = response.read().decode("utf-8")
return yaml.safe_load(data)
else:
with open(os.path.abspath(filepath), "r") as f:
data = yaml.safe_load(f)
return data


def select_config(
config_filepath: str = None,
config_env_vars=None,
default_config_filepath: str = None,
check_exist: bool = True,
on_missing=lambda fp: IOError(fp),
strict_env: bool = False,
config_name=None,
) -> str:
"""
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 type(config_name) is str:
config_name = f"{config_name} "
else:
return read_yaml_file(filepath)

Expand Down
4 changes: 4 additions & 0 deletions yacman/yacman_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ def from_yaml_file(cls, filepath: str, create_file: bool = False, **kwargs):
entries = yaml.load(file_contents, yaml.SafeLoader)
ref = cls(entries, **kwargs)
ref.locker = ThreeLocker(filepath)
ref.filepath = filepath
return ref

def update_from_yaml_file(self, filepath=None):
if filepath is not None: # set filepath to update filepath if uninitialized
if self.filepath is not None:
self.filepath = filepath
self.data.update(load_yaml(filepath))
return

Expand Down

0 comments on commit 62dc575

Please sign in to comment.