From 70c2f118ac95e3e40cd1ea30b5f010391a93a06b Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Mon, 2 Feb 2026 16:40:20 +0000 Subject: [PATCH] fix: update template_loader to handle both source and installed modes --- gitignore_generator/template_loader.py | 34 +++++++++++++++++++++----- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/gitignore_generator/template_loader.py b/gitignore_generator/template_loader.py index 1eef935..4c93b44 100644 --- a/gitignore_generator/template_loader.py +++ b/gitignore_generator/template_loader.py @@ -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