-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathinstance_loader.py
More file actions
39 lines (33 loc) · 1.18 KB
/
instance_loader.py
File metadata and controls
39 lines (33 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import annotations
import pathlib
import types
import typing as t
from .parsers import ParseError, ParserSet
from .transforms import Transform
class InstanceLoader:
def __init__(
self,
filenames: t.Sequence[str],
default_filetype: str = "json",
data_transform: Transform | None = None,
) -> None:
self._filenames = filenames
self._default_filetype = default_filetype
self._data_transform = (
data_transform if data_transform is not None else Transform()
)
self._parsers = ParserSet(
modify_yaml_implementation=self._data_transform.modify_yaml_implementation
)
def iter_files(self) -> t.Iterator[tuple[pathlib.Path, ParseError | t.Any]]:
for fn in self._filenames:
path = pathlib.Path(fn)
try:
data: t.Any = self._parsers.parse_file(path, self._default_filetype)
except ParseError as err:
data = err
else:
if isinstance(data, types.GeneratorType):
data = list(data)
data = self._data_transform(data)
yield (path, data)