Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"Boolean types." "" 

2 

3from config import Config as C, Names as N 

4from control.html import HtmlElements as H 

5from control.utils import ( 

6 pick as G, 

7 E, 

8 ZERO, 

9 MINONE, 

10) 

11 

12from control.typ.base import TypeBase 

13 

14 

15CT = C.tables 

16 

17BOOLEAN_TYPES = CT.boolTypes 

18 

19NONE_VALUES = {"null", "none", "empty", E, ZERO} 

20FALSE_VALUES = {"no", "false", "off", MINONE} 

21 

22 

23class Bool(TypeBase): 

24 """Base class for boolean types.""" 

25 

26 widgetType = N.bool 

27 

28 def normalize(self, strVal): 

29 return strVal 

30 

31 def fromStr(self, editVal): 

32 return editVal 

33 

34 def toDisplay(self, val, markup=True): 

35 values = G(BOOLEAN_TYPES, self.name) 

36 noneValue = False if len(values) == 2 else None 

37 

38 valueBare = G(values, val, default=G(values, noneValue)) 

39 return ( 

40 val 

41 if markup is None 

42 else H.icon(valueBare, cls="medium", asChar=not markup) 

43 ) 

44 

45 def toEdit(self, val): 

46 return val 

47 

48 def toOrig(self, val): 

49 return val 

50 

51 def widget(self, val): 

52 values = G(BOOLEAN_TYPES, self.name) 

53 noneValue = False if len(values) == 2 else None 

54 refV = G(values, val, default=G(values, noneValue)) 

55 

56 return H.div( 

57 [ 

58 H.iconx( 

59 values[w], 

60 bool=str(w).lower(), 

61 cls=(("active" if values[w] is refV else E) + " medium"), 

62 ) 

63 for w in values 

64 ], 

65 cls="wvalue", 

66 ) 

67 

68 

69class Bool2(Bool): 

70 """Type class for two-valued booleans: True and False""" 

71 

72 def fromStr(self, editVal): 

73 return ( 

74 False 

75 if editVal is None 

76 or (type(editVal) is bool and not editVal) 

77 or (type(editVal) is str and editVal.lower() in NONE_VALUES | FALSE_VALUES) 

78 else True 

79 ) 

80 

81 

82class Bool3(Bool): 

83 """Type class for three-valued booleans: True and False and None""" 

84 

85 def fromStr(self, editVal): 

86 return ( 

87 None 

88 if editVal is None 

89 or (type(editVal) is str and editVal.lower() in NONE_VALUES) 

90 else False 

91 if (type(editVal) is bool and not editVal) 

92 or (type(editVal) is str and editVal.lower() in FALSE_VALUES) 

93 else True 

94 )