feat: add as_path on data items
This commit is contained in:
parent
1a32cb2b42
commit
a23da83687
|
|
@ -45,6 +45,18 @@ class ContentDirectory(Content):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
class DataField:
|
||||||
|
def __init__(self, file: "DataFile", value: Any) -> None:
|
||||||
|
self.__file = file
|
||||||
|
self.__value = value
|
||||||
|
|
||||||
|
def as_path(self) -> Path:
|
||||||
|
return self.__file.path.parent / str(self.__value)
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return str(self.__value)
|
||||||
|
|
||||||
|
|
||||||
class DataFile(Content):
|
class DataFile(Content):
|
||||||
def __init__(self, path: Path) -> None:
|
def __init__(self, path: Path) -> None:
|
||||||
super().__init__(path)
|
super().__init__(path)
|
||||||
|
|
@ -52,10 +64,11 @@ class DataFile(Content):
|
||||||
self.__data = load(data_file, Loader)
|
self.__data = load(data_file, Loader)
|
||||||
|
|
||||||
def __getitem__(self, key: Any) -> Any:
|
def __getitem__(self, key: Any) -> Any:
|
||||||
return self.__data.get(key)
|
return DataField(self, self.__data.get(key))
|
||||||
|
|
||||||
def __iter__(self) -> Iterator[Any]:
|
def __iter__(self) -> Iterator[Any]:
|
||||||
yield from self.__data
|
for it in self.__data:
|
||||||
|
yield DataField(self, it)
|
||||||
|
|
||||||
|
|
||||||
class MarkdownFile(Content):
|
class MarkdownFile(Content):
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,18 @@ def test_load_data(datadir: Path) -> None:
|
||||||
content = ContentDirectory(datadir)
|
content = ContentDirectory(datadir)
|
||||||
steven = content.load(Path("otters/steven.yml"))
|
steven = content.load(Path("otters/steven.yml"))
|
||||||
|
|
||||||
assert steven["name"] == "Steven"
|
assert str(steven["name"]) == "Steven"
|
||||||
assert steven["mood"] == "Angry"
|
assert str(steven["mood"]) == "Angry"
|
||||||
|
|
||||||
otter_list = content.load(Path("otters/otter_list.json"))
|
otter_list = content.load(Path("otters/otter_list.json"))
|
||||||
assert list(otter_list) == ["steven", "peter"]
|
assert [str(it) for it in otter_list] == ["steven", "peter"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_data_as_path(datadir: Path) -> None:
|
||||||
|
content = ContentDirectory(datadir)
|
||||||
|
paths = content.load(Path("otters/paths.yml"))
|
||||||
|
|
||||||
|
assert paths["steven"].as_path() == datadir / "otters" / "relative-steven"
|
||||||
|
|
||||||
|
|
||||||
def test_load_markdown(datadir: Path) -> None:
|
def test_load_markdown(datadir: Path) -> None:
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
steven: ./relative-steven
|
||||||
|
|
||||||
Loading…
Reference in New Issue