Coverage for lintro / utils / console / constants.py: 89%

18 statements  

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

1"""Console constants and helper functions. 

2 

3This module contains shared constants and utility functions used by 

4console output classes. 

5""" 

6 

7from __future__ import annotations 

8 

9import re 

10from typing import Any 

11 

12# ============================================================================= 

13# Emoji and Formatting Constants 

14# ============================================================================= 

15 

16TOOL_EMOJIS: dict[str, str] = { 

17 "ruff": "🦀", 

18 "pydoclint": "📝", 

19 "hadolint": "🐳", 

20 "yamllint": "📄", 

21 "black": "🖤", 

22 "pytest": "🧪", 

23 "markdownlint": "🔧", 

24 "actionlint": "🔧", 

25 "bandit": "🔧", 

26 "oxlint": "⚡", 

27 "oxfmt": "✨", 

28 "prettier": "💅", 

29} 

30DEFAULT_EMOJI: str = "🔧" 

31BORDER_LENGTH: int = 70 

32INFO_BORDER_LENGTH: int = 70 

33DEFAULT_REMAINING_COUNT: str = "?" 

34 

35# ============================================================================= 

36# Regex Patterns 

37# ============================================================================= 

38 

39# Regex patterns used to parse tool outputs for remaining issue counts 

40RE_CANNOT_AUTOFIX: re.Pattern[str] = re.compile( 

41 r"Found\s+(\d+)\s+issue(?:s)?\s+that\s+cannot\s+be\s+auto-fixed", 

42) 

43RE_REMAINING_OR_CANNOT: re.Pattern[str] = re.compile( 

44 r"(\d+)\s+(?:issue\(s\)\s+)?(?:that\s+cannot\s+be\s+auto-fixed|remaining)", 

45) 

46 

47 

48# ============================================================================= 

49# Helper Functions 

50# ============================================================================= 

51 

52 

53def get_tool_emoji(tool_name: str) -> str: 

54 """Get emoji for a tool. 

55 

56 Args: 

57 tool_name: str: Name of the tool. 

58 

59 Returns: 

60 str: Emoji for the tool. 

61 """ 

62 return TOOL_EMOJIS.get(tool_name, DEFAULT_EMOJI) 

63 

64 

65def get_summary_value( 

66 summary: dict[str, Any] | object, 

67 key: str, 

68 default: int | float = 0, 

69) -> int | float: 

70 """Extract value from summary dict or object. 

71 

72 Args: 

73 summary: Summary data as dict or dataclass. 

74 key: Attribute/key name. 

75 default: Default value if not found. 

76 

77 Returns: 

78 int | float: The extracted value or default. 

79 """ 

80 if isinstance(summary, dict): 

81 value = summary.get(key, default) 

82 return value if isinstance(value, (int, float)) else default 

83 value = getattr(summary, key, default) 

84 return value if isinstance(value, (int, float)) else default