Coverage for lintro / enums / hadolint_enums.py: 94%

36 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-04-03 18:53 +0000

1"""Hadolint enums for formats and failure thresholds.""" 

2 

3from __future__ import annotations 

4 

5from enum import StrEnum, auto 

6 

7from loguru import logger 

8 

9 

10class HadolintFormat(StrEnum): 

11 """Supported output formats for Hadolint.""" 

12 

13 TTY = auto() 

14 JSON = auto() 

15 CHECKSTYLE = auto() 

16 CODECLIMATE = auto() 

17 GITLAB_CODECLIMATE = auto() 

18 GNU = auto() 

19 CODACY = auto() 

20 SONARQUBE = auto() 

21 SARIF = auto() 

22 

23 

24class HadolintFailureThreshold(StrEnum): 

25 """Hadolint failure thresholds used to gate exit status.""" 

26 

27 ERROR = auto() 

28 WARNING = auto() 

29 INFO = auto() 

30 STYLE = auto() 

31 IGNORE = auto() 

32 NONE = auto() 

33 

34 

35def normalize_hadolint_format(value: str | HadolintFormat) -> HadolintFormat: 

36 """Normalize user input to a HadolintFormat. 

37 

38 Args: 

39 value: Existing enum member or string name of the format. 

40 

41 Returns: 

42 HadolintFormat: Canonical enum value, defaulting to ``TTY`` when 

43 parsing fails. 

44 """ 

45 if isinstance(value, HadolintFormat): 

46 return value 

47 try: 

48 return HadolintFormat[value.upper()] 

49 except (KeyError, AttributeError) as e: 

50 logger.debug( 

51 f"Invalid HadolintFormat value '{value}': {e}. Defaulting to TTY.", 

52 ) 

53 return HadolintFormat.TTY 

54 

55 

56def normalize_hadolint_threshold( 

57 value: str | HadolintFailureThreshold, 

58) -> HadolintFailureThreshold: 

59 """Normalize user input to a HadolintFailureThreshold. 

60 

61 Args: 

62 value: Existing enum member or string name of the threshold. 

63 

64 Returns: 

65 HadolintFailureThreshold: Canonical enum value, defaulting to ``INFO`` 

66 when parsing fails. 

67 """ 

68 if isinstance(value, HadolintFailureThreshold): 

69 return value 

70 try: 

71 return HadolintFailureThreshold[value.upper()] 

72 except (KeyError, AttributeError) as e: 

73 logger.debug( 

74 f"Invalid HadolintFailureThreshold value '{value}': {e}. " 

75 "Defaulting to INFO.", 

76 ) 

77 return HadolintFailureThreshold.INFO