Coverage for navigaattori/template_loader.py: 77.14%
25 statements
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 20:46:10 +00:00
« prev ^ index » next coverage.py v7.4.1, created at 2024-02-04 20:46:10 +00:00
1"""Loader function for templates."""
3import pathlib
4import pkgutil
5from typing import Union
7from navigaattori import ENCODING, log
9RESOURCES = ('templates/liitos_vocabulary.yml',)
12def load_resource(resource: str, is_complete_path: bool = False) -> str:
13 """Load the template either from the package resources or an external path."""
14 if is_complete_path: 14 ↛ 15line 14 didn't jump to line 15, because the condition on line 14 was never true
15 log.debug(f'loading resource from external source ({resource})')
16 with open(resource, 'rt', encoding=ENCODING) as handle:
17 return handle.read()
18 else:
19 return pkgutil.get_data(__package__, resource).decode(encoding=ENCODING) # type: ignore
22def eject(argv: Union[list[str], None] = None) -> int:
23 """Eject the templates into the folder given (default EJECTED) and create the folder if it does not exist."""
24 argv = argv if argv else ['']
25 into = argv[0]
26 if not into.strip(): 26 ↛ 27line 26 didn't jump to line 27, because the condition on line 26 was never true
27 into = 'EJECTED'
28 into_path = pathlib.Path(into)
29 (into_path / 'templates').mkdir(parents=True, exist_ok=True)
30 for resource in RESOURCES:
31 write_to = into_path / resource
32 log.debug(f'- ejecting resource ({resource}) to ({write_to})')
33 data = pkgutil.get_data(__package__, resource).decode(encoding=ENCODING) # type: ignore
34 with open(write_to, 'wt', encoding=ENCODING) as target:
35 target.write(data)
37 return 0