A folder that may contain both hand-written and generated files.
Generated files are tracked in a .gitignore manifest inside the folder (so git
ignores them, and the next run knows which files it owns). Files that exist but are
not listed in the manifest are considered hand-written and never overwritten.
If the folder already contains a hand-written .gitignore, the manifest goes into
.mknodes-generated instead (and the files are not git-ignored).
Source code in mkdocs_mknodes/writers/base.py
| class GeneratedFolder:
"""A folder that may contain both hand-written and generated files.
Generated files are tracked in a `.gitignore` manifest inside the folder (so git
ignores them, and the next run knows which files it owns). Files that exist but are
not listed in the manifest are considered hand-written and never overwritten.
If the folder already contains a hand-written `.gitignore`, the manifest goes into
`.mknodes-generated` instead (and the files are not git-ignored).
"""
def __init__(self, path: pathlib.Path, *, owned: bool = False) -> None:
"""Constructor.
Args:
path: The folder
owned: Whether the whole folder is generated (no hand-written files inside)
"""
self.path = path
self.owned = owned
self.manifest_path = self._get_manifest_path()
self.previous = self._read_manifest()
self.written: set[str] = set()
self.skipped: list[str] = []
self.changed: list[str] = []
def _get_manifest_path(self) -> pathlib.Path:
gitignore = self.path / MANIFEST_NAME
if not gitignore.exists() or _is_manifest(gitignore):
return gitignore
return self.path / ".mknodes-generated"
def _read_manifest(self) -> set[str]:
if not self.manifest_path.is_file() or not _is_manifest(self.manifest_path):
return set()
lines = self.manifest_path.read_text(encoding="utf-8").splitlines()
return {
line.removeprefix("/")
for line in lines
if line and not line.startswith("#") and line != f"/{self.manifest_path.name}"
}
def is_hand_written(self, rel_path: str) -> bool:
if self.owned:
return False
return (self.path / rel_path).exists() and rel_path not in self.previous
def write(self, rel_path: str, content: str | bytes) -> bool:
"""Write a generated file unless a hand-written file exists at that path.
Returns True if the file was written (or is already up to date).
"""
rel_path = pathlib.PurePosixPath(rel_path).as_posix()
if self.is_hand_written(rel_path):
logger.warning("Not overwriting hand-written file %s", self.path / rel_path)
self.skipped.append(rel_path)
return False
target = self.path / rel_path
data = content.encode() if isinstance(content, str) else content
if not target.is_file() or target.read_bytes() != data:
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
self.changed.append(rel_path)
self.written.add(rel_path)
return True
def finish(self) -> list[str]:
"""Delete stale generated files and write the manifest. Returns removed paths."""
removed = []
for rel_path in sorted(self.previous - self.written):
target = self.path / rel_path
if target.is_file():
target.unlink()
removed.append(rel_path)
_remove_empty_parents(target.parent, self.path)
if self.written or self.manifest_path.exists():
self.path.mkdir(parents=True, exist_ok=True)
lines = [MANIFEST_MARKER, f"/{self.manifest_path.name}"]
lines += [f"/{p}" for p in sorted(self.written)]
text = "\n".join(lines) + "\n"
if not self.manifest_path.is_file() or self.manifest_path.read_text() != text:
self.manifest_path.write_text(text, encoding="utf-8")
return removed
|
__init__
__init__(path: Path, *, owned: bool = False) -> None
Constructor.
Parameters:
| Name |
Type |
Description |
Default |
path
|
Path
|
|
required
|
owned
|
bool
|
Whether the whole folder is generated (no hand-written files inside)
|
False
|
Source code in mkdocs_mknodes/writers/base.py
| def __init__(self, path: pathlib.Path, *, owned: bool = False) -> None:
"""Constructor.
Args:
path: The folder
owned: Whether the whole folder is generated (no hand-written files inside)
"""
self.path = path
self.owned = owned
self.manifest_path = self._get_manifest_path()
self.previous = self._read_manifest()
self.written: set[str] = set()
self.skipped: list[str] = []
self.changed: list[str] = []
|
finish
Delete stale generated files and write the manifest. Returns removed paths.
Source code in mkdocs_mknodes/writers/base.py
| def finish(self) -> list[str]:
"""Delete stale generated files and write the manifest. Returns removed paths."""
removed = []
for rel_path in sorted(self.previous - self.written):
target = self.path / rel_path
if target.is_file():
target.unlink()
removed.append(rel_path)
_remove_empty_parents(target.parent, self.path)
if self.written or self.manifest_path.exists():
self.path.mkdir(parents=True, exist_ok=True)
lines = [MANIFEST_MARKER, f"/{self.manifest_path.name}"]
lines += [f"/{p}" for p in sorted(self.written)]
text = "\n".join(lines) + "\n"
if not self.manifest_path.is_file() or self.manifest_path.read_text() != text:
self.manifest_path.write_text(text, encoding="utf-8")
return removed
|
write
write(rel_path: str, content: str | bytes) -> bool
Write a generated file unless a hand-written file exists at that path.
Returns True if the file was written (or is already up to date).
Source code in mkdocs_mknodes/writers/base.py
| def write(self, rel_path: str, content: str | bytes) -> bool:
"""Write a generated file unless a hand-written file exists at that path.
Returns True if the file was written (or is already up to date).
"""
rel_path = pathlib.PurePosixPath(rel_path).as_posix()
if self.is_hand_written(rel_path):
logger.warning("Not overwriting hand-written file %s", self.path / rel_path)
self.skipped.append(rel_path)
return False
target = self.path / rel_path
data = content.encode() if isinstance(content, str) else content
if not target.is_file() or target.read_bytes() != data:
target.parent.mkdir(parents=True, exist_ok=True)
target.write_bytes(data)
self.changed.append(rel_path)
self.written.add(rel_path)
return True
|