Coverage for middle_layer/closeout/domain_layer/services/validate_template_service.py: 100.00%
14 statements
« prev ^ index » next coverage.py v7.10.5, created at 2026-03-09 06:13 +0000
« prev ^ index » next coverage.py v7.10.5, created at 2026-03-09 06:13 +0000
1# Copyright 2024 Associated Universities, Inc.
2#
3# This file is part of Telescope Time Allocation Tools (TTAT).
4#
5# TTAT is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# any later version.
9#
10# TTAT is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with TTAT. If not, see <https://www.gnu.org/licenses/>.
18from typing import Set, Tuple
20from jinja2 import TemplateSyntaxError, UndefinedError
22from allocate.domain_layer.entities.proposal_disposition import ProposalDisposition
23from closeout.domain_layer.entities.template import Template, TemplateRenderer, TemplateRenderError
24from common.application_layer.orm_repositories.orm_repository import ORMRepository
27def validate_template(template_content: str) -> Tuple[bool, Set[str]]:
28 """Validate that the provided template content has correct syntax
30 This function attempts to initialize a template renderer with the provided content
31 to check for syntax errors, but does not attempt to render the template.
33 :param template_content: str representing a template
34 :return: A tuple containing (is_valid: bool, errors: Set[str])
35 where is_valid is True if no syntax errors were found, and
36 errors is a set of error messages if any were found
37 """
38 errors = set()
40 # Try to create a template renderer with the content
41 # This will check for syntax errors without rendering
42 try:
43 renderer = TemplateRenderer(template_content)
44 # If we get here, the template syntax is valid
45 # We don't attempt to render, which would check for undefined variables
47 except TemplateRenderError as e:
48 # TemplateRenderError already formats errors correctly, so just add them to our set
49 for error in e.errors:
50 errors.add(error)
51 """
52 except TemplateSyntaxError as e:
53 # Format syntax errors consistently with TemplateRenderer.__init__
54 error_message = f"Template syntax error: {str(e)}"
55 line_info = f"<syntax error>:{e.lineno}: {error_message}"
56 errors.add(line_info)
57 except Exception as e:
58 # Format other exceptions
59 error_type = type(e).__name__
60 error_message = f"{error_type}: {str(e)}"
61 errors.add(f"<error>: {error_message}")
62 """
64 # Return True if no errors, False otherwise
65 return (len(errors) == 0, errors)