Coverage for navigaattori/eject.py: 92.86%

28 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 20:46:10 +00:00

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

2 

3import pathlib 

4 

5import navigaattori.template_loader as template 

6from navigaattori import ENCODING, log 

7 

8THINGS = { 

9 'liitos-vocabulary-yaml': (LIIIOS_VOCABULARY_YAML := 'templates/liitos_vocabulary.yml'), 

10} 

11 

12 

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

14 """Later Alligator.""" 

15 if not thing: 

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

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

18 return 2 

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

20 if not guesses: 

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

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

23 return 2 

24 if len(guesses) > 1: 24 ↛ 25line 24 didn't jump to line 25, because the condition on line 24 was never true

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

26 return 2 

27 content = template.load_resource(THINGS[guesses[0]], False) 

28 if not out: 

29 print(content) 

30 return 0 

31 

32 out_path = pathlib.Path(out) 

33 out_name = out_path.name 

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

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

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

37 handle.write(content) 

38 return 0