Coverage for liitos/eject.py: 100.00%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-13 17:41:25 +00:00

1"""Eject templates and configurations.""" 

2 

3import pathlib 

4 

5import liitos.template as tpl 

6from liitos import ENCODING, log 

7 

8THINGS = { 

9 'bookmatter-pdf': (BOOKMATTER_TEMPLATE := 'templates/bookmatter.tex.in'), 

10 'driver-pdf': (DRIVER_TEMPLATE := 'templates/driver.tex.in'), 

11 'metadata-pdf': (METADATA_TEMPLATE := 'templates/metadata.tex.in'), 

12 'publisher-pdf': (PUBLISHER_TEMPLATE := 'templates/publisher.tex.in'), 

13 'setup-pdf': (SETUP_TEMPLATE := 'templates/setup.tex.in'), 

14 'approvals-yaml': (APPROVALS_YAML := 'templates/approvals.yml'), 

15 'changes-yaml': (CHANGES_YAML := 'templates/changes.yml'), 

16 'layout-yaml': (LAYOUT_YAML := 'templates/layout.yml'), 

17 'meta-base-yaml': (META_YAML := 'templates/meta.yml'), 

18 'meta-patch-yaml': (META_PATCH_YAML := 'templates/meta-patch.yml'), 

19 'mkdocs-yaml': (MKDOCS_PATCH_YAML := 'templates/mkdocs.yml.in'), 

20 'vocabulary-yaml': (VOCABULARY_YAML := 'templates/vocabulary.yml'), 

21} 

22 

23 

24def this(thing: str, out: str = '') -> int: 

25 """Later Alligator.""" 

26 if not thing: 

27 log.error('eject of template with no name requested') 

28 log.info(f'templates known: ({", ".join(sorted(THINGS))})') 

29 return 2 

30 guesses = sorted(entry for entry in THINGS if entry.startswith(thing)) 

31 if not guesses: 

32 log.error(f'eject of unknown template ({thing}) requested') 

33 log.info(f'templates known: ({", ".join(sorted(THINGS))})') 

34 return 2 

35 if len(guesses) > 1: 

36 log.error(f'eject of ambiguous template ({thing}) requested - matches ({", ".join(guesses)})') 

37 return 2 

38 content = tpl.load_resource(THINGS[guesses[0]], False) 

39 if not out: 

40 print(content) 

41 return 0 

42 

43 out_path = pathlib.Path(out) 

44 out_name = out_path.name 

45 if not THINGS[guesses[0]].endswith(out_name): 

46 log.warning(f'requested writing ({THINGS[guesses[0]]}) to file ({out_name})') 

47 with open(out_path, 'wt', encoding=ENCODING) as handle: 

48 handle.write(content) 

49 return 0