Files
wehub-resource-sync 6b7e6b44f1
Python Build and Type Check / python-ci (ubuntu-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.11) (push) Has been cancelled
Python Build and Type Check / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Integration Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Notebook Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Smoke Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (ubuntu-latest, 3.13) (push) Has been cancelled
Python Unit Tests / python-ci (windows-latest, 3.13) (push) Has been cancelled
gh-pages / build (push) Has been cancelled
Python Publish (pypi) / Upload release to PyPI (push) Has been cancelled
Spellcheck / spellcheck (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:37:31 +08:00

77 lines
2.5 KiB
Python

# Copyright (c) 2024 Microsoft Corporation.
# Licensed under the MIT License
"""In-memory template manager implementation."""
from pathlib import Path
from typing import Any
from graphrag_llm.templating.template_manager import TemplateManager
class FileTemplateManager(TemplateManager):
"""Abstract base class for template managers."""
_encoding: str
_templates_extension: str
_templates_dir: Path
def __init__(
self,
base_dir: str = "templates",
template_extension: str = ".jinja",
encoding: str = "utf-8",
**kwargs: Any,
) -> None:
"""Initialize the template manager.
Args
----
base_dir: str (default="./templates")
The base directory where templates are stored.
template_extension: str (default=".jinja")
The file extension for template files.
encoding: str (default="utf-8")
The encoding used to read template files.
Raises
------
ValueError
If the base directory does not exist or is not a directory.
If the template_extension is an empty string.
"""
self._templates = {}
self._encoding = encoding
self._templates_extension = template_extension
self._templates_dir = Path(base_dir).resolve()
if not self._templates_dir.exists() or not self._templates_dir.is_dir():
msg = f"Templates directory '{base_dir}' does not exist or is not a directory."
raise ValueError(msg)
def get(self, template_name: str) -> str | None:
"""Retrieve a template by its name."""
template_file = (
self._templates_dir / f"{template_name}{self._templates_extension}"
)
if template_file.exists() and template_file.is_file():
return template_file.read_text(encoding=self._encoding)
return None
def register(self, template_name: str, template: str) -> None:
"""Register a new template."""
self._templates[template_name] = template
template_path = (
self._templates_dir / f"{template_name}{self._templates_extension}"
)
template_path.write_text(template, encoding=self._encoding)
def keys(self) -> list[str]:
"""List all registered template names."""
return list(self._templates.keys())
def __contains__(self, template_name: str) -> bool:
"""Check if a template is registered."""
return template_name in self._templates