Coverage for foran/report.py: 100.00%

37 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2023-01-02 19:51 +0100

1"""In front or behind (Foran eller bagved)? reporting interface.""" 

2import pathlib 

3from enum import Enum, auto 

4from typing import List, Tuple 

5 

6from foran.status import Status 

7 

8REPORT_STEM = 'foran-eller-bagved' 

9 

10 

11class Format(Enum): 

12 NONE = auto() 

13 TEXT = auto() 

14 

15 

16class Report: 

17 """Report structure.""" 

18 

19 def __init__(self, stem: str = REPORT_STEM, file_format: Format = Format.TEXT): 

20 """Seed the status structure with the git status info and some defaults.""" 

21 self.stem = stem 

22 self.file_format = file_format 

23 

24 

25def report_as(status: Status, report: Report) -> None: 

26 """Side effects ...""" 

27 if report.stem == 'STD_OUT': 

28 print(''.join(generate_report(status))) 

29 return 

30 

31 file_extension = '.txt' if report.file_format == Format.TEXT else '' # TODO(sthagen) HACK A DID ACK 

32 filepath = pathlib.Path(f'{report.stem}{file_extension}') 

33 filepath.parent.mkdir(parents=True, exist_ok=True) 

34 

35 with open(filepath, 'w') as handle: 

36 handle.write(''.join(generate_report(status))) 

37 

38 

39def generate_report_list(label: str, anything: bool, entries: List[str], list_symbol: str = '*') -> list[str]: 

40 """Generic list report generator.""" 

41 if not anything: 

42 return [] 

43 if not entries: 

44 return [f'{label}\n'] 

45 

46 return [f'{label}\n'] + [''.join(f' {list_symbol} {entry}\n' for entry in entries)] 

47 

48 

49def generate_report(status: Status) -> Tuple[str, ...]: 

50 """Convoluted special trickery ... to build partially conditional report lines""" 

51 report = [] 

52 report.append(f'Analysis ({status.when})\n') 

53 report.append(f'State ({status.foran_disp})\n') 

54 report.append(f'Branch ({status.branch})\n') 

55 report.append(f'Commit ({status.commit})\n') 

56 

57 report += generate_report_list('List of local commits:', not status.foran, status.local_commits, '*') 

58 report += generate_report_list('List of locally staged files:', bool(status.local_staged), status.local_staged, '+') 

59 report += generate_report_list('List of locally modified files:', bool(status.local_files), status.local_files, '-') 

60 

61 return tuple(report)