Coverage for an_website / utils / template_loader.py: 100.000%

16 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-15 14:36 +0000

1# This program is free software: you can redistribute it and/or modify 

2# it under the terms of the GNU Affero General Public License as 

3# published by the Free Software Foundation, either version 3 of the 

4# License, or (at your option) any later version. 

5# 

6# This program is distributed in the hope that it will be useful, 

7# but WITHOUT ANY WARRANTY; without even the implied warranty of 

8# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

9# GNU Affero General Public License for more details. 

10# 

11# You should have received a copy of the GNU Affero General Public License 

12# along with this program. If not, see <https://www.gnu.org/licenses/>. 

13 

14"""A Tornado template loader.""" 

15 

16 

17import os.path 

18from importlib.resources.abc import Traversable 

19from typing import override 

20 

21from tornado.template import BaseLoader, Template 

22 

23 

24class TemplateLoader(BaseLoader): 

25 """A Tornado template loader.""" 

26 

27 root: Traversable 

28 

29 def __init__(self, root: Traversable, whitespace: None | str) -> None: 

30 """Initialize the template loader.""" 

31 self.root = root 

32 super().__init__(whitespace=whitespace) 

33 

34 @override 

35 def _create_template(self, name: str) -> Template: 

36 """Create a template from the given name.""" 

37 return Template((self.root / name).read_bytes(), name=name, loader=self) 

38 

39 @override 

40 def resolve_path(self, name: str, parent_path: str | None = None) -> str: 

41 """Resolve the template path. 

42 

43 name is interpreted as relative to parent_path if it's not None. 

44 """ 

45 if ( 

46 # same check as in tornado.template.Loader 

47 parent_path 

48 and not parent_path.startswith("<") 

49 and not parent_path.startswith("/") 

50 and not name.startswith("/") 

51 ): 

52 return os.path.normpath( 

53 os.path.join(os.path.dirname(parent_path), name) 

54 ) 

55 return name