feat: add ContentDirectory class
This commit is contained in:
parent
cb674a9642
commit
b968cde1c2
|
|
@ -1,2 +1,3 @@
|
||||||
**/__pycache__
|
**/__pycache__
|
||||||
|
.coverage
|
||||||
jean_website.egg-info
|
jean_website.egg-info
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,35 @@
|
||||||
|
from functools import cache
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
class Content:
|
||||||
|
def __init__(self, path: Path) -> None:
|
||||||
|
self.__path = path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def path(self) -> Path:
|
||||||
|
return self.__path
|
||||||
|
|
||||||
|
|
||||||
|
class ContentDirectory(Content):
|
||||||
|
def load(self, subpath: Path) -> Content:
|
||||||
|
current: Content = self
|
||||||
|
for part in subpath.parts:
|
||||||
|
if not isinstance(current, ContentDirectory):
|
||||||
|
raise NotADirectoryError(self.path)
|
||||||
|
|
||||||
|
current = current.__load_children(part)
|
||||||
|
|
||||||
|
return current
|
||||||
|
|
||||||
|
@cache # noqa: B019
|
||||||
|
def __load_children(self, name: str) -> Content:
|
||||||
|
child_path = self.path / name
|
||||||
|
|
||||||
|
if not child_path.exists():
|
||||||
|
raise FileNotFoundError(child_path)
|
||||||
|
|
||||||
|
if child_path.is_dir():
|
||||||
|
return ContentDirectory(child_path)
|
||||||
|
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
@ -17,6 +17,13 @@ def mypy(session: Session) -> None:
|
||||||
session.run("mypy")
|
session.run("mypy")
|
||||||
|
|
||||||
|
|
||||||
|
@session(python=["3.10", "3.11"])
|
||||||
|
def unit_tests(session: Session) -> None:
|
||||||
|
"""Run unit tests."""
|
||||||
|
devenv(session)
|
||||||
|
session.run("python", "-m", "pytest", "--cov=jwebsite", "--cov-report=html")
|
||||||
|
|
||||||
|
|
||||||
@session()
|
@session()
|
||||||
def devenv(session: Session) -> None:
|
def devenv(session: Session) -> None:
|
||||||
session.install("-e", ".[dev]")
|
session.install("-e", ".[dev]")
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,9 @@ dependencies = [
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
dev = [
|
dev = [
|
||||||
"nox",
|
"nox",
|
||||||
|
"pytest",
|
||||||
|
"pytest-datadir",
|
||||||
|
"pytest-coverage"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|
@ -49,6 +52,3 @@ select = [
|
||||||
"ASYNC",
|
"ASYNC",
|
||||||
"RUF"
|
"RUF"
|
||||||
]
|
]
|
||||||
|
|
||||||
[tool.pytest.ini_options]
|
|
||||||
asyncio_mode = "auto"
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from pytest import raises
|
||||||
|
|
||||||
|
from jwebsite.content import ContentDirectory
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_directory(datadir: Path) -> None:
|
||||||
|
content = ContentDirectory(datadir)
|
||||||
|
otters = content.load(Path("otters"))
|
||||||
|
assert isinstance(otters, ContentDirectory)
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_errors(datadir: Path) -> None:
|
||||||
|
directory = ContentDirectory(datadir)
|
||||||
|
|
||||||
|
with raises(FileNotFoundError):
|
||||||
|
directory.load(Path("otters/i-dont-exist"))
|
||||||
Loading…
Reference in New Issue