Coverage for foran/report.py: 100.00%

37 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-02-04 18:05:08 +00:00

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

2 

3import pathlib 

4from enum import Enum, auto 

5from typing import List, Tuple 

6 

7from foran.status import Status 

8 

9REPORT_STEM = 'foran-eller-bagved' 

10 

11 

12class Format(Enum): 

13 NONE = auto() 

14 TEXT = auto() 

15 

16 

17class Report: 

18 """Report structure.""" 

19 

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

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

22 self.stem = stem 

23 self.file_format = file_format 

24 

25 

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

27 """Side effects ...""" 

28 if report.stem == 'STD_OUT': 

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

30 return 

31 

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

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

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

35 

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

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

38 

39 

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

41 """Generic list report generator.""" 

42 if not anything: 

43 return [] 

44 if not entries: 

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

46 

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

48 

49 

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

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

52 report = [] 

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

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

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

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

57 

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

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

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

61 

62 return tuple(report)