in glean_parser/util.py [0:0]
def load_yaml_or_json(path: Path):
"""
Load the content from either a .json or .yaml file, based on the filename
extension.
:param path: `pathlib.Path` object
:rtype object: The tree of objects as a result of parsing the file.
:raises ValueError: The file is neither a .json, .yml or .yaml file.
:raises FileNotFoundError: The file does not exist.
"""
# If in py.test, support bits of literal JSON/YAML content
if TESTING_MODE and isinstance(path, dict):
return yaml_load(yaml.dump(path))
if path.suffix == ".json":
with path.open("r", encoding="utf-8") as fd:
return json.load(fd)
elif path.suffix in (".yml", ".yaml", ".yamlx"):
with path.open("r", encoding="utf-8") as fd:
return yaml_load(fd)
else:
raise ValueError(f"Unknown file extension {path.suffix}")