Coverage for liitos/template.py: 100.00%
23 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-16 00:43:25 +00:00
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-16 00:43:25 +00:00
1"""Loader function for templates."""
3import pathlib
4import pkgutil
5from typing import Union
7from liitos import ENCODING, PathLike
9RESOURCES = (
10 'templates/approvals.yml',
11 'templates/bookmatter.tex.in',
12 'templates/changes.yml',
13 'templates/driver.tex.in',
14 'templates/layout.yml',
15 'templates/meta.yml',
16 'templates/meta-patch.yml',
17 'templates/metadata.tex.in',
18 'templates/mkdocs.yml.in',
19 'templates/publisher.tex.in',
20 'templates/setup.tex.in',
21 'templates/vocabulary.yml',
22)
25def load_resource(resource: PathLike, is_complete_path: bool = False) -> str:
26 """Load the template either from the package resources or an external path."""
27 if is_complete_path:
28 with open(resource, 'rt', encoding=ENCODING) as handle:
29 return handle.read()
30 else:
31 return pkgutil.get_data(__package__, str(resource)).decode(encoding=ENCODING) # type: ignore
34def eject(argv: Union[list[str], None] = None) -> int:
35 """Eject the templates into the folder given (default EJECTED) and create the folder if it does not exist."""
36 argv = argv if argv else ['']
37 into = argv[0]
38 if not into.strip():
39 into = 'EJECTED'
40 into_path = pathlib.Path(into)
41 (into_path / 'templates').mkdir(parents=True, exist_ok=True)
42 for resource in RESOURCES:
43 write_to = into_path / resource
44 data = pkgutil.get_data(__package__, resource).decode(encoding=ENCODING) # type: ignore
45 with open(write_to, 'wt', encoding=ENCODING) as target:
46 target.write(data)
48 return 0