Coverage for src/models/flags.py: 100.00%

49 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-10-18 15:02 +0000

1#!/usr/bin/env python3 

2 

3# Standard libraries 

4from typing import Optional 

5 

6# Components 

7from .variables import Variables 

8 

9# Flags class, pylint: disable=too-few-public-methods 

10class Flags: 

11 

12 # Boolean 

13 @staticmethod 

14 def boolean(value: str) -> bool: 

15 return value.lower() in [ 

16 'true', 

17 '1', 

18 ] 

19 

20 # String 

21 @staticmethod 

22 def string(value: bool | str) -> str: 

23 if isinstance(value, bool): 

24 return 'true' if value else 'false' 

25 return str(value) # pragma: no cover 

26 

27 # Exists 

28 @staticmethod 

29 def exists(flag: str, variables: Variables) -> bool: 

30 return flag in variables 

31 

32 # Enabled 

33 @staticmethod 

34 def enabled(flag: str, variables: Variables) -> bool: 

35 return Flags.exists(flag, variables) and Flags.boolean(str(variables[flag])) 

36 

37 # Has 

38 @staticmethod 

39 def has( 

40 option: Optional[str], 

41 flag: str, 

42 variables: Variables, 

43 ) -> bool: 

44 return option is not None or Flags.exists(flag, variables) 

45 

46 # Check (bool) 

47 @staticmethod 

48 def check_bool( 

49 option: Optional[bool | str], 

50 flag: str, 

51 variables: Variables, 

52 default: bool, 

53 ) -> bool: 

54 

55 # Variables 

56 result: bool = default 

57 

58 # Check option (bool) 

59 if option is not None and isinstance(option, bool) and option != default: 

60 result = option 

61 

62 # Check option (str) 

63 elif option is not None and isinstance(option, str): 

64 result = Flags.boolean(option) 

65 

66 # Check variables 

67 elif Flags.exists(flag, variables): 

68 result = Flags.boolean(str(variables[flag])) 

69 

70 # Result 

71 return result 

72 

73 # Check (str) 

74 @staticmethod 

75 def check_str( 

76 option: Optional[str], 

77 flag: str, 

78 variables: Variables, 

79 default: str, 

80 ) -> str: 

81 

82 # Variables 

83 result: str = default 

84 

85 # Check option 

86 if option is not None and option != '': 

87 result = option 

88 

89 # Check variables 

90 elif Flags.exists(flag, variables): 

91 result = str(variables[flag]) 

92 

93 # Result 

94 return result 

95 

96 # Check (strbool) 

97 @staticmethod 

98 def check_strbool( 

99 option: Optional[str], 

100 flag: str, 

101 variables: Variables, 

102 default: str, 

103 const: str, 

104 ) -> str: 

105 

106 # Variables 

107 result: str = default 

108 

109 # Check option 

110 if option is not None and option != '': 

111 result = option 

112 

113 # Check variables 

114 elif Flags.exists(flag, variables): 

115 result = str(variables[flag]) 

116 

117 # Evaluate boolean 

118 if result.lower() in ['true', '1']: 

119 result = const 

120 elif result.lower() in ['false', '0']: 

121 result = '' 

122 

123 # Result 

124 return result