fix: update template_loader to handle both source and installed modes
Some checks failed
CI / test (3.10) (push) Failing after 11s
CI / test (3.11) (push) Failing after 10s
CI / test (3.12) (push) Failing after 10s
CI / lint (push) Failing after 9s

This commit is contained in:
2026-02-02 16:40:20 +00:00
parent da06ba9a62
commit 70c2f118ac

View File

@@ -1,6 +1,7 @@
"""Template loader for gitignore-generator."""
import json
import os
from pathlib import Path
from typing import Dict, List, Optional, Set
@@ -15,10 +16,22 @@ class TemplateLoader:
self._templates_index: Optional[Dict] = None
self._custom_templates: Dict[str, str] = {}
def _get_templates_dir(self) -> Path:
"""Get the templates directory, handling both source and installed modes."""
templates_dir = config.template_dir
if templates_dir.exists() and (templates_dir / "templates.json").exists():
return templates_dir
module_dir = Path(__file__).parent
package_templates = module_dir / "templates"
if package_templates.exists() and (package_templates / "templates.json").exists():
return package_templates
return templates_dir
def _load_templates_index(self) -> Dict:
"""Load the templates index file."""
if self._templates_index is None:
index_path = config.template_dir / "templates.json"
templates_dir = self._get_templates_dir()
index_path = templates_dir / "templates.json"
if index_path.exists():
with open(index_path, "r") as f:
self._templates_index = json.load(f)
@@ -29,7 +42,8 @@ class TemplateLoader:
def _save_templates_index(self) -> None:
"""Save the templates index file."""
index_path = config.template_dir / "templates.json"
templates_dir = self._get_templates_dir()
index_path = templates_dir / "templates.json"
with open(index_path, "w") as f:
json.dump(self._templates_index, f, indent=2)
@@ -72,6 +86,14 @@ class TemplateLoader:
for template_file in custom_dir.glob("*.gitignore"):
self._custom_templates[template_file.stem] = str(template_file)
def _get_template_path(self, template_name: str, category: str) -> Optional[Path]:
"""Get the path to a template file."""
templates_dir = self._get_templates_dir()
template_path = templates_dir / category / f"{template_name}.gitignore"
if template_path.exists():
return template_path
return None
def load_template(self, template_name: str, category: Optional[str] = None) -> Optional[str]:
"""Load a template by name."""
self._load_custom_templates()
@@ -89,8 +111,8 @@ class TemplateLoader:
categories = ["languages", "ides"]
for cat in categories:
template_path = config.template_dir / cat / f"{template_name}.gitignore"
if template_path.exists():
template_path = self._get_template_path(template_name, cat)
if template_path is not None:
with open(template_path, "r") as f:
return f.read()
@@ -139,8 +161,8 @@ class TemplateLoader:
return True
for cat in ["languages", "ides"]:
template_path = config.template_dir / cat / f"{template_name}.gitignore"
if template_path.exists():
template_path = self._get_template_path(template_name, cat)
if template_path is not None:
return True
return False