Coverage for control/typ/bool.py : 72%

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." ""
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)
12from control.typ.base import TypeBase
15CT = C.tables
17BOOLEAN_TYPES = CT.boolTypes
19NONE_VALUES = {"null", "none", "empty", E, ZERO}
20FALSE_VALUES = {"no", "false", "off", MINONE}
23class Bool(TypeBase):
24 """Base class for boolean types."""
26 widgetType = N.bool
28 def normalize(self, strVal):
29 return strVal
31 def fromStr(self, editVal):
32 return editVal
34 def toDisplay(self, val, markup=True):
35 values = G(BOOLEAN_TYPES, self.name)
36 noneValue = False if len(values) == 2 else None
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 )
45 def toEdit(self, val):
46 return val
48 def toOrig(self, val):
49 return val
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))
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 )
69class Bool2(Bool):
70 """Type class for two-valued booleans: True and False"""
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 )
82class Bool3(Bool):
83 """Type class for three-valued booleans: True and False and None"""
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 )