Skip to content

Commit 595d726

Browse files
committed
Added TemplateNotFoundError and docstring to TemplateSyntaxError
1 parent fc296e7 commit 595d726

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

adafruit_templateengine.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,19 @@ def __init__(self, template: str, start_position: int, end_position: int):
6868
self.content = template[start_position:end_position]
6969

7070

71+
class TemplateNotFoundError(OSError):
72+
"""Raised when a template file is not found."""
73+
74+
def __init__(self, path: str):
75+
"""Specified template file that was not found."""
76+
super().__init__(f"Template file not found: {path}")
77+
78+
7179
class TemplateSyntaxError(SyntaxError):
7280
"""Raised when a syntax error is encountered in a template."""
7381

7482
def __init__(self, message: str, token: Token):
83+
"""Provided token is not a valid template syntax at the specified position."""
7584
super().__init__(f"{message}\n\n" + self._underline_token_in_template(token))
7685

7786
@staticmethod
@@ -302,7 +311,7 @@ def _resolve_includes(template: str):
302311
# TODO: Restrict include to specific directory
303312

304313
if not _exists_and_is_file(template_path):
305-
raise OSError(f"Template file not found: {template_path}")
314+
raise TemplateNotFoundError(template_path)
306315

307316
# Replace the include with the template content
308317
with open(template_path, "rt", encoding="utf-8") as template_file:
@@ -323,7 +332,7 @@ def _resolve_includes_blocks_and_extends(template: str):
323332
extended_template_path = extends_match.group(0)[12:-4]
324333

325334
if not _exists_and_is_file(extended_template_path):
326-
raise OSError(f"Template file not found: {extended_template_path}")
335+
raise TemplateNotFoundError(extended_template_path)
327336

328337
# Check for circular extends
329338
if extended_template_path in extended_templates:
@@ -833,7 +842,7 @@ def __init__(self, template_path: str, *, language: str = Language.HTML) -> None
833842
"""
834843

835844
if not _exists_and_is_file(template_path):
836-
raise OSError(f"Template file not found: {template_path}")
845+
raise TemplateNotFoundError(template_path)
837846

838847
with open(template_path, "rt", encoding="utf-8") as template_file:
839848
template_string = template_file.read()

docs/api.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
77
.. automodule:: adafruit_templateengine
88
:members:
9-
:inherited-members:

0 commit comments

Comments
 (0)