diff --git a/dvc/dependency/param.py b/dvc/dependency/param.py index d38f6b60c9..917f4a02e3 100644 --- a/dvc/dependency/param.py +++ b/dvc/dependency/param.py @@ -39,8 +39,9 @@ def read_param_file( path: str, key_paths: Optional[List[str]] = None, flatten: bool = False, + **load_kwargs, ) -> Any: - config = load_path(path, fs) + config = load_path(path, fs, **load_kwargs) if not key_paths: return config diff --git a/dvc/repo/metrics/show.py b/dvc/repo/metrics/show.py index 8a30d75587..f4847f67dd 100644 --- a/dvc/repo/metrics/show.py +++ b/dvc/repo/metrics/show.py @@ -55,30 +55,27 @@ def _extract_metrics(metrics, path: str): ret[key] = m else: logger.debug( - ( - "Could not parse '%s' metric from '%s'" - "due to its unsupported type: '%s'" - ), + "Could not parse %r metric from %r due to its unsupported type: %r", key, path, - type(val), + type(val).__name__, ) return ret -def _read_metric(fs: "FileSystem", path: str) -> Any: - val = load_path(path, fs) +def _read_metric(fs: "FileSystem", path: str, **load_kwargs) -> Any: + val = load_path(path, fs, **load_kwargs) val = _extract_metrics(val, path) return val or {} def _read_metrics( - fs: "FileSystem", metrics: Iterable[str] + fs: "FileSystem", metrics: Iterable[str], **load_kwargs ) -> Iterator[Tuple[str, Union[Exception, Any]]]: for metric in metrics: try: - yield metric, _read_metric(fs, metric) + yield metric, _read_metric(fs, metric, **load_kwargs) except Exception as exc: # noqa: BLE001 # pylint:disable=broad-exception-caught logger.debug(exc) yield metric, exc @@ -157,7 +154,7 @@ def _gather_metrics( data = {} fs = repo.dvcfs - for fs_path, result in _read_metrics(fs, files): + for fs_path, result in _read_metrics(fs, files, cache=True): repo_path = fs_path.lstrip(fs.root_marker) repo_os_path = os.sep.join(fs.path.parts(repo_path)) if not isinstance(result, Exception): diff --git a/dvc/repo/params/show.py b/dvc/repo/params/show.py index 742881c57c..21c1d43a78 100644 --- a/dvc/repo/params/show.py +++ b/dvc/repo/params/show.py @@ -103,11 +103,11 @@ def _collect_vars(repo, params, stages=None) -> Dict: def _read_params( - fs: "FileSystem", params: Dict[str, List[str]] + fs: "FileSystem", params: Dict[str, List[str]], **load_kwargs ) -> Iterator[Tuple[str, Union[Exception, Any]]]: for file_path, key_paths in params.items(): try: - yield file_path, read_param_file(fs, file_path, key_paths) + yield file_path, read_param_file(fs, file_path, key_paths, **load_kwargs) except Exception as exc: # noqa: BLE001 # pylint:disable=broad-exception-caught logger.debug(exc) yield file_path, exc @@ -136,7 +136,7 @@ def _gather_params( data: Dict[str, FileResult] = {} fs = repo.dvcfs - for fs_path, result in _read_params(fs, files_keypaths): + for fs_path, result in _read_params(fs, files_keypaths, cache=True): repo_path = fs_path.lstrip(fs.root_marker) repo_os_path = os.sep.join(fs.path.parts(repo_path)) if not isinstance(result, Exception): diff --git a/dvc/utils/serialize/__init__.py b/dvc/utils/serialize/__init__.py index 5afbab435e..437fd92abb 100644 --- a/dvc/utils/serialize/__init__.py +++ b/dvc/utils/serialize/__init__.py @@ -20,10 +20,10 @@ ) -def load_path(fs_path, fs): +def load_path(fs_path, fs, **kwargs): suffix = fs.path.suffix(fs_path).lower() loader = LOADERS[suffix] - return loader(fs_path, fs=fs) + return loader(fs_path, fs=fs, **kwargs) DUMPERS: DefaultDict[str, DumperFn] = defaultdict( # noqa: F405 diff --git a/dvc/utils/serialize/_common.py b/dvc/utils/serialize/_common.py index 3b715d90ad..bbf635c3f0 100644 --- a/dvc/utils/serialize/_common.py +++ b/dvc/utils/serialize/_common.py @@ -69,10 +69,12 @@ def __init__(self, path: "StrPath", encoding: str): super().__init__(path, f"is not valid {encoding}") -def _load_data(path: "StrPath", parser: ParserFn, fs: Optional["FileSystem"] = None): +def _load_data( + path: "StrPath", parser: ParserFn, fs: Optional["FileSystem"] = None, **kwargs +): open_fn = fs.open if fs else open encoding = "utf-8" - with open_fn(path, encoding=encoding) as fd: # type: ignore[operator] + with open_fn(path, encoding=encoding, **kwargs) as fd: # type: ignore[operator] with reraise(UnicodeDecodeError, EncodingError(path, encoding)): return parser(fd.read(), path) diff --git a/dvc/utils/serialize/_json.py b/dvc/utils/serialize/_json.py index c6e9ac231a..300d93adcd 100644 --- a/dvc/utils/serialize/_json.py +++ b/dvc/utils/serialize/_json.py @@ -10,8 +10,8 @@ def __init__(self, path): super().__init__(path, "JSON file structure is corrupted") -def load_json(path, fs=None): - return _load_data(path, parser=parse_json, fs=fs) +def load_json(path, fs=None, **kwargs): + return _load_data(path, parser=parse_json, fs=fs, **kwargs) def parse_json(text, path, **kwargs): diff --git a/dvc/utils/serialize/_py.py b/dvc/utils/serialize/_py.py index 3c470bb568..647fa00c80 100644 --- a/dvc/utils/serialize/_py.py +++ b/dvc/utils/serialize/_py.py @@ -15,8 +15,8 @@ def __init__(self, path, message="Python file structure is corrupted"): super().__init__(path, message) -def load_py(path, fs=None): - return _load_data(path, parser=parse_py, fs=fs) +def load_py(path, fs=None, **kwargs): + return _load_data(path, parser=parse_py, fs=fs, **kwargs) def parse_py(text, path): diff --git a/dvc/utils/serialize/_toml.py b/dvc/utils/serialize/_toml.py index 1d21ae4ff0..2b7c38c9d2 100644 --- a/dvc/utils/serialize/_toml.py +++ b/dvc/utils/serialize/_toml.py @@ -10,8 +10,8 @@ def __init__(self, path): super().__init__(path, "TOML file structure is corrupted") -def load_toml(path, fs=None): - return _load_data(path, parser=parse_toml, fs=fs) +def load_toml(path, fs=None, **kwargs): + return _load_data(path, parser=parse_toml, fs=fs, **kwargs) def _parse_toml(text, path): diff --git a/dvc/utils/serialize/_yaml.py b/dvc/utils/serialize/_yaml.py index b8c4a55e00..6de2ae4ade 100644 --- a/dvc/utils/serialize/_yaml.py +++ b/dvc/utils/serialize/_yaml.py @@ -17,8 +17,8 @@ def __init__(self, path): super().__init__(path, "YAML file structure is corrupted") -def load_yaml(path, fs=None): - return _load_data(path, parser=parse_yaml, fs=fs) +def load_yaml(path, fs=None, **kwargs): + return _load_data(path, parser=parse_yaml, fs=fs, **kwargs) def parse_yaml(text, path, typ="safe"):