Skip to content

Commit

Permalink
Adding boolify() as validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
fwkz committed Aug 10, 2016
1 parent a8c5cb0 commit 00e9f68
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
55 changes: 55 additions & 0 deletions routersploit/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,61 @@ def test_address_strip_scheme_2(self):
address = "ftp://127.0.0.1"
self.assertEqual(validators.address(address), "127.0.0.1")

def test_boolify_false_1(self):
value = False
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_2(self):
value = "No"
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_3(self):
value = "n"
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_4(self):
value = "OFF"
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_5(self):
value = "0"
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_6(self):
value = "False"
self.assertEqual(validators.boolify(value), False)

def test_boolify_false_7(self):
value = "f"
self.assertEqual(validators.boolify(value), False)

def test_boolify_true_1(self):
value = True
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_2(self):
value = "Yes"
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_3(self):
value = "y"
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_4(self):
value = "oN"
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_5(self):
value = "1"
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_6(self):
value = "tRuE"
self.assertEqual(validators.boolify(value), True)

def test_boolify_true_7(self):
value = "t"
self.assertEqual(validators.boolify(value), True)

if __name__ == '__main__':
unittest.main()
18 changes: 18 additions & 0 deletions routersploit/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import socket
import urlparse
from distutils.util import strtobool

from .exceptions import OptionValidationError

Expand Down Expand Up @@ -37,3 +38,20 @@ def ipv4(address):
raise OptionValidationError("Option have to be valid IP address.")

return address


def boolify(value):
""" Function that will translate common strings into bool values
True -> "True", "t", "yes", "y", "on", "1"
False -> any other string
Objects other than string will be transformed using built-in bool() function.
"""
if isinstance(value, basestring):
try:
return bool(strtobool(value))
except ValueError:
return False
else:
return bool(value)

0 comments on commit 00e9f68

Please sign in to comment.