Michael Merickel
2018-10-26 4149922e64aecf2a213f8efb120cd2d61fed3eb7
commit | author | age
8e606d 1 from pyramid.compat import string_types
b68aad 2
1c37b5 3 truthy = frozenset(('t', 'true', 'y', 'yes', 'on', '1'))
6b35eb 4 falsey = frozenset(('f', 'false', 'n', 'no', 'off', '0'))
1c37b5 5
0c29cf 6
358dc2 7 def asbool(s):
fe4172 8     """ Return the boolean value ``True`` if the case-lowered value of string
6b35eb 9     input ``s`` is a :term:`truthy string`. If ``s`` is already one of the
MM 10     boolean values ``True`` or ``False``, return it."""
1328b2 11     if s is None:
CM 12         return False
1c37b5 13     if isinstance(s, bool):
1328b2 14         return s
358dc2 15     s = str(s).strip()
1c37b5 16     return s.lower() in truthy
358dc2 17
0c29cf 18
8cd013 19 def aslist_cronly(value):
8e606d 20     if isinstance(value, string_types):
8cd013 21         value = filter(None, [x.strip() for x in value.splitlines()])
8e606d 22     return list(value)
8cd013 23
0c29cf 24
a735d6 25 def aslist(value, flatten=True):
MM 26     """ Return a list of strings, separating the input based on newlines
27     and, if flatten=True (the default), also split on spaces within
28     each line."""
8cd013 29     values = aslist_cronly(value)
a735d6 30     if not flatten:
MM 31         return values
8cd013 32     result = []
CM 33     for value in values:
34         subvalues = value.split()
35         result.extend(subvalues)
36     return result