jean-web/jwebsite/content.py

36 lines
866 B
Python

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()