fix: resolve CI linting and type checking errors
Some checks failed
CI / test (push) Has been cancelled
Some checks failed
CI / test (push) Has been cancelled
This commit is contained in:
@@ -1,12 +1,10 @@
|
|||||||
"""Template engine for ScaffoldForge."""
|
"""Template engine for ScaffoldForge."""
|
||||||
|
|
||||||
import os
|
|
||||||
import re
|
import re
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Dict, List, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from jinja2 import (
|
from jinja2 import (
|
||||||
BaseLoader,
|
|
||||||
Environment,
|
Environment,
|
||||||
FileSystemLoader,
|
FileSystemLoader,
|
||||||
PackageLoader,
|
PackageLoader,
|
||||||
@@ -27,12 +25,12 @@ class TemplateEngine:
|
|||||||
"""
|
"""
|
||||||
self.template_dir = template_dir
|
self.template_dir = template_dir
|
||||||
self.env = self._create_environment()
|
self.env = self._create_environment()
|
||||||
self._loaded_templates: Dict[str, Dict[str, Any]] = {}
|
self._loaded_templates: dict[str, dict[str, str]] = {}
|
||||||
|
|
||||||
def _create_environment(self) -> Environment:
|
def _create_environment(self) -> Environment:
|
||||||
"""Create Jinja2 environment with appropriate loader."""
|
"""Create Jinja2 environment with appropriate loader."""
|
||||||
if self.template_dir and Path(self.template_dir).exists():
|
if self.template_dir and Path(self.template_dir).exists():
|
||||||
loader = FileSystemLoader(self.template_dir)
|
loader: FileSystemLoader | PackageLoader = FileSystemLoader(self.template_dir)
|
||||||
else:
|
else:
|
||||||
loader = PackageLoader("scaffoldforge", "templates")
|
loader = PackageLoader("scaffoldforge", "templates")
|
||||||
|
|
||||||
@@ -45,7 +43,7 @@ class TemplateEngine:
|
|||||||
|
|
||||||
def load_templates(
|
def load_templates(
|
||||||
self, language: str, template_name: str = "default"
|
self, language: str, template_name: str = "default"
|
||||||
) -> Dict[str, str]:
|
) -> dict[str, str]:
|
||||||
"""Load templates for a specific language and template type.
|
"""Load templates for a specific language and template type.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -59,7 +57,7 @@ class TemplateEngine:
|
|||||||
if key in self._loaded_templates:
|
if key in self._loaded_templates:
|
||||||
return self._loaded_templates[key]
|
return self._loaded_templates[key]
|
||||||
|
|
||||||
templates = {}
|
templates: dict[str, str] = {}
|
||||||
template_dir = Path(__file__) / language / template_name
|
template_dir = Path(__file__) / language / template_name
|
||||||
|
|
||||||
if template_dir.exists():
|
if template_dir.exists():
|
||||||
@@ -88,14 +86,14 @@ class TemplateEngine:
|
|||||||
template_path = f"{language}/{template_type}/{filename}"
|
template_path = f"{language}/{template_type}/{filename}"
|
||||||
try:
|
try:
|
||||||
template = self.env.get_template(template_path)
|
template = self.env.get_template(template_path)
|
||||||
return template.filename
|
return template.filename or ""
|
||||||
except Exception:
|
except Exception:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def render(
|
def render(
|
||||||
self,
|
self,
|
||||||
template_name: str,
|
template_name: str,
|
||||||
context: Dict[str, Any],
|
context: dict[str, Any],
|
||||||
language: str = "python",
|
language: str = "python",
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Render a template with the given context.
|
"""Render a template with the given context.
|
||||||
@@ -117,7 +115,7 @@ class TemplateEngine:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise ValueError(f"Failed to render template {template_name}: {e}")
|
raise ValueError(f"Failed to render template {template_name}: {e}")
|
||||||
|
|
||||||
def render_string(self, template_content: str, context: Dict[str, Any]) -> str:
|
def render_string(self, template_content: str, context: dict[str, Any]) -> str:
|
||||||
"""Render a template string directly.
|
"""Render a template string directly.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -131,7 +129,7 @@ class TemplateEngine:
|
|||||||
return template.render(**context)
|
return template.render(**context)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_available_templates(language: str) -> List[str]:
|
def list_available_templates(language: str) -> list[str]:
|
||||||
"""List available templates for a language.
|
"""List available templates for a language.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -144,14 +142,14 @@ class TemplateEngine:
|
|||||||
if not templates_dir.exists():
|
if not templates_dir.exists():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
templates = []
|
templates: list[str] = []
|
||||||
for item in templates_dir.iterdir():
|
for item in templates_dir.iterdir():
|
||||||
if item.is_dir():
|
if item.is_dir():
|
||||||
templates.append(item.name)
|
templates.append(item.name)
|
||||||
return sorted(templates)
|
return sorted(templates)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def list_available_languages() -> List[str]:
|
def list_available_languages() -> list[str]:
|
||||||
"""List all available programming languages.
|
"""List all available programming languages.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@@ -161,13 +159,13 @@ class TemplateEngine:
|
|||||||
if not templates_dir.exists():
|
if not templates_dir.exists():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
languages = []
|
languages: list[str] = []
|
||||||
for item in templates_dir.iterdir():
|
for item in templates_dir.iterdir():
|
||||||
if item.is_dir() and not item.name.startswith("_"):
|
if item.is_dir() and not item.name.startswith("_"):
|
||||||
languages.append(item.name)
|
languages.append(item.name)
|
||||||
return sorted(languages)
|
return sorted(languages)
|
||||||
|
|
||||||
def get_template_context(self, issue_data: IssueData) -> Dict[str, Any]:
|
def get_template_context(self, issue_data: IssueData) -> dict[str, Any]:
|
||||||
"""Generate template context from issue data.
|
"""Generate template context from issue data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
|||||||
Reference in New Issue
Block a user