@@ -99,82 +99,124 @@ def _prettify_output(output):
99
99
# helper function to validate names
100
100
def validate_name (name ):
101
101
validate = re .compile (r'[a-z0-9][a-z0-9\.\-_]{0,31}' , re .I )
102
-
103
102
return bool (validate .fullmatch (name ))
104
103
105
104
106
- # data -> bool | default
107
- def parse_bool (data , default = False ):
108
- """
109
- try to parse booleans from strings
105
+ def _parse_none (data ):
110
106
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
114
109
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
121
112
122
- Note that "-" gets a special treatment:
113
+ raise TypeError ( "Value is not None" )
123
114
124
- >>> parse_bool("-")
125
- None
126
115
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
129
119
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
+
133
133
"""
134
134
135
135
if isinstance (data , bool ):
136
136
return data
137
137
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" ]:
141
140
return True
142
- elif data . lower () in ["no" , "false" , "off" ]:
141
+ elif val in ["no" , "false" , "off" ]:
143
142
return False
144
- return default
143
+
144
+ raise TypeError ("Value is not a boolean" )
145
145
146
146
147
147
def parse_user_input (data ):
148
148
"""
149
- uses parse_bool () to partially return Boolean and NoneType values
149
+ uses _parse_bool () to partially return Boolean and NoneType values
150
150
All other types as returned as-is
151
151
152
- >>> parse_bool ("YES")
152
+ >>> parse_user_input ("YES")
153
153
True
154
- >>> parse_bool ("false")
154
+ >>> parse_user_input ("false")
155
155
False
156
- >>> parse_bool(8.4)
156
+ >>> parse_user_input("notfalse")
157
+ 'notfalse'
158
+ >>> parse_user_input(8.4)
157
159
8.4
158
160
"""
159
- return parse_bool (data , data )
160
161
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
161
173
162
- def get_str_bool (data , true = "yes" , false = "no" ):
174
+
175
+ def to_string (data , true = "yes" , false = "no" , none = "-" ):
163
176
"""
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:
165
191
166
- >>> get_str_bool(True)
167
- "yes"
168
- >>> get_str_bool(False)
169
- "no"
192
+ string: Map input according to arguments or stringified input
170
193
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
+ "-"
175
208
"""
176
- return true if parse_bool (data ) else false
177
209
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 )
178
220
179
221
def exec_passthru (command , logger = None ):
180
222
if isinstance (command , str ):
0 commit comments