From e0598cc7b7cce369bd11530d3d8a917c43262ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20S=C3=A9chet?= Date: Mon, 20 May 2024 08:21:45 +0200 Subject: [PATCH] feat: add DataFile content class --- jwebsite/content.py | 18 ++++++++++++++++++ noxfile.py | 3 +-- pyproject.toml | 1 + tests/test_content.py | 14 ++++++++++++++ tests/test_content/otters/otter_list.json | 1 + tests/test_content/otters/steven.yml | 2 ++ 6 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/test_content/otters/otter_list.json diff --git a/jwebsite/content.py b/jwebsite/content.py index a697818..95a6984 100644 --- a/jwebsite/content.py +++ b/jwebsite/content.py @@ -1,5 +1,8 @@ from functools import cache from pathlib import Path +from typing import Any, Iterator + +from yaml import Loader, load class Content: @@ -31,5 +34,20 @@ class ContentDirectory(Content): if child_path.is_dir(): return ContentDirectory(child_path) + if child_path.is_file() and child_path.suffix in [".yml", ".yaml", ".json"]: + return DataFile(child_path) raise NotImplementedError() + + +class DataFile(Content): + def __init__(self, path: Path) -> None: + super().__init__(path) + with path.open("r", encoding="utf-8") as data_file: + self.__data = load(data_file, Loader) + + def __getitem__(self, key: Any) -> Any: + return self.__data.get(key) + + def __iter__(self) -> Iterator[Any]: + yield from self.__data diff --git a/noxfile.py b/noxfile.py index c2f6dcd..1139d33 100644 --- a/noxfile.py +++ b/noxfile.py @@ -12,8 +12,7 @@ def lint(session: Session) -> None: @session() def mypy(session: Session) -> None: - session.install("-e", ".[dev]") - session.install("mypy") + session.install("-e", ".[dev]", "mypy", "types-PyYAML") session.run("mypy") diff --git a/pyproject.toml b/pyproject.toml index 01ec75a..40396ec 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ dependencies = [ "click", "jinja2", "pymarkdown", + "pyyaml", ] [project.optional-dependencies] diff --git a/tests/test_content.py b/tests/test_content.py index afb78b3..47153a6 100644 --- a/tests/test_content.py +++ b/tests/test_content.py @@ -16,3 +16,17 @@ def test_load_errors(datadir: Path) -> None: with raises(FileNotFoundError): directory.load(Path("otters/i-dont-exist")) + + with raises(NotADirectoryError): + directory.load(Path("otters/steven.yml/child")) + + +def test_load_data(datadir: Path) -> None: + content = ContentDirectory(datadir) + steven = content.load(Path("otters/steven.yml")) + + assert steven["name"] == "Steven" + assert steven["mood"] == "Angry" + + otter_list = content.load(Path("otters/otter_list.json")) + assert list(otter_list) == ["steven", "peter"] diff --git a/tests/test_content/otters/otter_list.json b/tests/test_content/otters/otter_list.json new file mode 100644 index 0000000..f88e25c --- /dev/null +++ b/tests/test_content/otters/otter_list.json @@ -0,0 +1 @@ +["steven", "peter"] diff --git a/tests/test_content/otters/steven.yml b/tests/test_content/otters/steven.yml index e69de29..88de4af 100644 --- a/tests/test_content/otters/steven.yml +++ b/tests/test_content/otters/steven.yml @@ -0,0 +1,2 @@ +name: Steven +mood: Angry