Skip to content

Commit 836a090

Browse files
committed
refactor igalic/bools branch
1 parent 426d8c7 commit 836a090

File tree

4 files changed

+102
-61
lines changed

4 files changed

+102
-61
lines changed

libiocage/lib/Datasets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def activate_pool(self, pool, mountpoint=None):
8989
)
9090

9191
def _is_pool_active(self, pool):
92-
return libiocage.lib.helpers.parse_bool(self._get_pool_property(
92+
return libiocage.lib.helpers.parse_user_input(self._get_pool_property(
9393
pool,
9494
self.ZFS_POOL_ACTIVE_PROPERTY
9595
))

libiocage/lib/JailConfig.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -212,27 +212,27 @@ def _set_type(self, value, **kwargs):
212212
self.data["type"] = value
213213

214214
def _get_basejail(self):
215-
return libiocage.lib.helpers.parse_bool(self.data["basejail"])
215+
return libiocage.lib.helpers.parse_user_input(self.data["basejail"])
216216

217217
def _default_basejail(self):
218218
return False
219219

220220
def _set_basejail(self, value, **kwargs):
221221
if self["legacy"]:
222-
self.data["basejail"] = libiocage.lib.helpers.get_str_bool(
222+
self.data["basejail"] = libiocage.lib.helpers.to_string(
223223
value, true="on", false="off")
224224
else:
225-
self.data["basejail"] = libiocage.lib.helpers.get_str_bool(
225+
self.data["basejail"] = libiocage.lib.helpers.to_string(
226226
value, true="yes", false="no")
227227

228228
def _get_clonejail(self):
229-
return libiocage.lib.helpers.parse_bool(self.data["clonejail"])
229+
return libiocage.lib.helpers.parse_user_input(self.data["clonejail"])
230230

231231
def _default_clonejail(self):
232232
return True
233233

234234
def _set_clonejail(self, value, **kwargs):
235-
self.data["clonejail"] = libiocage.lib.helpers.get_str_bool(
235+
self.data["clonejail"] = libiocage.lib.helpers.to_string(
236236
value, true="on", false="off")
237237

238238
def _get_ip4_addr(self):
@@ -307,10 +307,10 @@ def _default_defaultrouter6(self):
307307
return None
308308

309309
def _get_vnet(self):
310-
return libiocage.lib.helpers.parse_bool(self.data["vnet"])
310+
return libiocage.lib.helpers.parse_user_input(self.data["vnet"])
311311

312312
def _set_vnet(self, value, **kwargs):
313-
self.data["vnet"] = libiocage.lib.helpers.get_str_bool(
313+
self.data["vnet"] = libiocage.lib.helpers.to_string(
314314
value, true="on", false="off")
315315

316316
def _get_jail_zfs_dataset(self):
@@ -325,7 +325,7 @@ def _set_jail_zfs_dataset(self, value, **kwargs):
325325
self.data["jail_zfs_dataset"] = " ".join(value)
326326

327327
def _get_jail_zfs(self):
328-
enabled = libiocage.lib.helpers.parse_bool(self.data["jail_zfs"])
328+
enabled = libiocage.lib.helpers.parse_user_input(self.data["jail_zfs"])
329329
if not enabled:
330330
if len(self.jail_zfs_dataset) > 0:
331331
raise libiocage.lib.errors.JailConigZFSIsNotAllowed(
@@ -336,7 +336,7 @@ def _set_jail_zfs(self, value, **kwargs):
336336
if (value is None) or (value == ""):
337337
del self.data["jail_zfs"]
338338
return
339-
self.data["jail_zfs"] = libiocage.lib.helpers.get_str_bool(
339+
self.data["jail_zfs"] = libiocage.lib.helpers.to_string(
340340
value, true="on", false="off")
341341

342342
def _default_jail_zfs(self):

libiocage/lib/RCConf.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,14 @@ def save(self):
8888
self.logger.spam(output[:-1], jail=self.jail, indent=1)
8989

9090
def __setitem__(self, key, value):
91+
92+
val = libiocage.lib.helpers.to_string(
93+
libiocage.lib.helpers.parse_user_input(value),
94+
true="YES",
95+
false="NO"
96+
)
9197

92-
val = libiocage.lib.helpers.parse_user_input(value)
93-
# normalize booleans
94-
if isinstance(value, bool):
95-
dict.__setitem__(self, key,
96-
libiocage.lib.helpers.get_str_bool(
97-
value, true="YES", false="NO"))
98-
else:
99-
dict.__setitem__(self, key, str(val))
98+
dict.__setitem__(self, key, val)
10099

101100
def __getitem__(self, key):
102101
val = dict.__getitem__(self, key)

libiocage/lib/helpers.py

+85-43
Original file line numberDiff line numberDiff line change
@@ -99,82 +99,124 @@ def _prettify_output(output):
9999
# helper function to validate names
100100
def validate_name(name):
101101
validate = re.compile(r'[a-z0-9][a-z0-9\.\-_]{0,31}', re.I)
102-
103102
return bool(validate.fullmatch(name))
104103

105104

106-
# data -> bool | default
107-
def parse_bool(data, default=False):
108-
"""
109-
try to parse booleans from strings
105+
def _parse_none(data):
110106

111-
On success, it returns the parsed boolean
112-
on failure it returns the `default`.
113-
By default, `default` is `False`.
107+
if data is None:
108+
return None
114109

115-
>>> parse_bool("YES")
116-
True
117-
>>> parse_bool("false")
118-
False
119-
>>> parse_bool("/etc/passwd")
120-
False
110+
if data in ["none", "-"]:
111+
return None
121112

122-
Note that "-" gets a special treatment:
113+
raise TypeError("Value is not None")
123114

124-
>>> parse_bool("-")
125-
None
126115

127-
The behavior of the default parameter can be used to create a
128-
pass-thru function:
116+
def _parse_bool(data):
117+
"""
118+
try to parse booleans from strings
129119
130-
>>> default = "/etc/passwd"
131-
>>> parse_bool(default, default)
132-
"/etc/passwd"
120+
On success, it returns the parsed boolean on failure it raises a TypeError.
121+
122+
Usage:
123+
124+
>>> _parse_bool("YES")
125+
True
126+
>>> _parse_bool("false")
127+
False
128+
>>> _parse_bool("/etc/passwd")
129+
Traceback (most recent call last):
130+
File "<stdin>", line 1, in <module>
131+
TypeError: Not a boolean value
132+
133133
"""
134134

135135
if isinstance(data, bool):
136136
return data
137137
if isinstance(data, str):
138-
if data == "-":
139-
return None
140-
elif data.lower() in ["yes", "true", "on"]:
138+
val = data.lower()
139+
if val in ["yes", "true", "on"]:
141140
return True
142-
elif data.lower() in ["no", "false", "off"]:
141+
elif val in ["no", "false", "off"]:
143142
return False
144-
return default
143+
144+
raise TypeError("Value is not a boolean")
145145

146146

147147
def parse_user_input(data):
148148
"""
149-
uses parse_bool() to partially return Boolean and NoneType values
149+
uses _parse_bool() to partially return Boolean and NoneType values
150150
All other types as returned as-is
151151
152-
>>> parse_bool("YES")
152+
>>> parse_user_input("YES")
153153
True
154-
>>> parse_bool("false")
154+
>>> parse_user_input("false")
155155
False
156-
>>> parse_bool(8.4)
156+
>>> parse_user_input("notfalse")
157+
'notfalse'
158+
>>> parse_user_input(8.4)
157159
8.4
158160
"""
159-
return parse_bool(data, data)
160161

162+
try:
163+
return _parse_bool(data)
164+
except TypeError:
165+
pass
166+
167+
try:
168+
return _parse_none(data)
169+
except TypeError:
170+
pass
171+
172+
return data
161173

162-
def get_str_bool(data, true="yes", false="no"):
174+
175+
def to_string(data, true="yes", false="no", none="-"):
163176
"""
164-
return a string boolean value using parse_bool(), of specified style
177+
return a string boolean value using _parse_bool(), of specified style
178+
179+
Args:
180+
181+
true (string):
182+
The expected return value when data is True
183+
184+
false (string):
185+
The expected return value when data is False
186+
187+
none (string):
188+
The expected return value when data is None
189+
190+
Returns:
165191
166-
>>> get_str_bool(True)
167-
"yes"
168-
>>> get_str_bool(False)
169-
"no"
192+
string: Map input according to arguments or stringified input
170193
171-
>>> get_str_bool(True, true="yip", false="nope")
172-
"yip"
173-
>>> get_str_bool(False, true="yip", false="nope")
174-
"nope"
194+
Usage:
195+
196+
>>> to_string(True)
197+
"yes"
198+
>>> to_string(False)
199+
"no"
200+
201+
>>> to_string(True, true="yip", false="nope")
202+
"yip"
203+
>>> to_string(False, true="yip", false="nope")
204+
"nope"
205+
206+
>>> to_string(None)
207+
"-"
175208
"""
176-
return true if parse_bool(data) else false
177209

210+
data = parse_user_input(data)
211+
212+
if data is None:
213+
return none
214+
elif data is True:
215+
return true
216+
elif data is False:
217+
return false
218+
219+
return str(data)
178220

179221
def exec_passthru(command, logger=None):
180222
if isinstance(command, str):

0 commit comments

Comments
 (0)