params/metrics: cache remote files (#9932)

This commit is contained in:
skshetry
2023-09-11 19:13:47 +05:45
committed by GitHub
parent f2673acb53
commit 33b6c1d876
9 changed files with 26 additions and 26 deletions
+2 -1
View File
@@ -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
+7 -10
View File
@@ -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):
+3 -3
View File
@@ -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):
+2 -2
View File
@@ -20,10 +20,10 @@ PARSERS.update(
)
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
+4 -2
View File
@@ -69,10 +69,12 @@ class EncodingError(ParseError):
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)
+2 -2
View File
@@ -10,8 +10,8 @@ class JSONFileCorruptedError(ParseError):
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):
+2 -2
View File
@@ -15,8 +15,8 @@ class PythonFileCorruptedError(ParseError):
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):
+2 -2
View File
@@ -10,8 +10,8 @@ class TOMLFileCorruptedError(ParseError):
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):
+2 -2
View File
@@ -17,8 +17,8 @@ class YAMLFileCorruptedError(YAMLError):
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"):