Coverage for src/castep_linter/error_logging/logger.py: 55%

1"""Module containing code for the high level error logger"""

2 from collections import Counter

3 from dataclasses import dataclass, field

4 from typing import Iterator, List

5

6 from rich.console import Console

7

8 from castep_linter.error_logging import error_types

9 from castep_linter.fortran.fortran_node import FortranNode

10

11

12@dataclass

13 class ErrorLogger:

14 """Container for all errors and messages generated while analysing

15 a Fortran file"""

16

17 filename: str

18 errors: List[error_types.FortranMsgBase] = field(default_factory=list)

19

20 def __iter__(self) -> Iterator[error_types.FortranMsgBase]:

21 return iter(self.errors)

22

23 def add_msg(self, level: str, node: FortranNode, message: str):

24 """Add an error to the error list"""

25 err = error_types.new_fortran_error(level, node, message)

26 self.errors.append(err)

27

28 def print_errors(self, console: Console, level: str = "Warning") -> None:

29 """Print all the contained errors above the level"""

30 severity = error_types.ERROR_SEVERITY[level]

31

32 for err in self.errors:

33 if err.ERROR_SEVERITY >= severity:

34 err.print_err(self.filename, console)

35

36 def count_errors(self):

37 """Count the number of errors in each category"""

38 c = Counter(e.ERROR_SEVERITY for e in self.errors)

39 return {

40 err_str: c[err_severity] for err_str, err_severity in error_types.ERROR_SEVERITY.items()

41 }

42

43 def __len__(self):

44 return len(self.errors)

45

46 @property

47 def has_errors(self):

48 """Does the logger contain any errors"""

49 return len(self) > 0