mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
Consolidate boolean/mk_boolean conversion functions into a single location
Consolidate the module_utils, constants, and config functions that convert values into booleans into a single function in module_utils. Port code to use the module_utils.validate.convert_bool.boolean function isntead of mk_boolean.
This commit is contained in:
@@ -28,10 +28,6 @@
|
||||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
BOOLEANS_TRUE = ['y', 'yes', 'on', '1', 'true', 1, True]
|
||||
BOOLEANS_FALSE = ['n', 'no', 'off', '0', 'false', 0, False]
|
||||
BOOLEANS = BOOLEANS_TRUE + BOOLEANS_FALSE
|
||||
|
||||
SIZE_RANGES = {
|
||||
'Y': 1 << 80,
|
||||
'Z': 1 << 70,
|
||||
@@ -178,6 +174,8 @@ from ansible.module_utils.six import (
|
||||
)
|
||||
from ansible.module_utils.six.moves import map, reduce, shlex_quote
|
||||
from ansible.module_utils._text import to_native, to_bytes, to_text
|
||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS, BOOLEANS_FALSE, BOOLEANS_TRUE, boolean
|
||||
|
||||
|
||||
PASSWORD_MATCH = re.compile(r'^(?:.+[-_\s])?pass(?:[-_\s]?(?:word|phrase|wrd|wd)?)(?:[-_\s].+)?$', re.I)
|
||||
|
||||
@@ -1658,8 +1656,7 @@ class AnsibleModule(object):
|
||||
lowered_choices = None
|
||||
if param[k] == 'False':
|
||||
lowered_choices = _lenient_lowercase(choices)
|
||||
FALSEY = frozenset(BOOLEANS_FALSE)
|
||||
overlap = FALSEY.intersection(choices)
|
||||
overlap = BOOLEANS_FALSE.intersection(choices)
|
||||
if len(overlap) == 1:
|
||||
# Extract from a set
|
||||
(param[k],) = overlap
|
||||
@@ -1667,8 +1664,7 @@ class AnsibleModule(object):
|
||||
if param[k] == 'True':
|
||||
if lowered_choices is None:
|
||||
lowered_choices = _lenient_lowercase(choices)
|
||||
TRUTHY = frozenset(BOOLEANS_TRUE)
|
||||
overlap = TRUTHY.intersection(choices)
|
||||
overlap = BOOLEANS_TRUE.intersection(choices)
|
||||
if len(overlap) == 1:
|
||||
(param[k],) = overlap
|
||||
|
||||
@@ -2045,16 +2041,13 @@ class AnsibleModule(object):
|
||||
|
||||
def boolean(self, arg):
|
||||
''' return a bool for the arg '''
|
||||
if arg is None or isinstance(arg, bool):
|
||||
if arg is None:
|
||||
return arg
|
||||
if isinstance(arg, string_types):
|
||||
arg = arg.lower()
|
||||
if arg in BOOLEANS_TRUE:
|
||||
return True
|
||||
elif arg in BOOLEANS_FALSE:
|
||||
return False
|
||||
else:
|
||||
self.fail_json(msg='%s is not a valid boolean. Valid booleans include: %s' % (to_text(arg), ','.join(['%s' % x for x in BOOLEANS])))
|
||||
|
||||
try:
|
||||
return boolean(arg)
|
||||
except TypeError as e:
|
||||
self.fail_json(msg=to_native(e))
|
||||
|
||||
def jsonify(self, data):
|
||||
for encoding in ("utf-8", "latin-1"):
|
||||
|
||||
@@ -23,8 +23,9 @@ import sys
|
||||
import copy
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
|
||||
HAS_DOCKER_PY = True
|
||||
HAS_DOCKER_PY_2 = False
|
||||
|
||||
@@ -29,7 +29,7 @@ import re
|
||||
import shlex
|
||||
import time
|
||||
|
||||
from ansible.module_utils.basic import BOOLEANS_TRUE, BOOLEANS_FALSE, get_exception
|
||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
from ansible.module_utils.six import string_types, text_type
|
||||
from ansible.module_utils.six.moves import zip
|
||||
|
||||
@@ -163,8 +163,7 @@ class CommandRunner(object):
|
||||
def add_conditional(self, condition):
|
||||
try:
|
||||
self.conditionals.add(Conditional(condition))
|
||||
except AttributeError:
|
||||
exc = get_exception()
|
||||
except AttributeError as exc:
|
||||
raise AddConditionError(msg=str(exc), condition=condition)
|
||||
|
||||
def run(self):
|
||||
|
||||
0
lib/ansible/module_utils/parsing/__init__.py
Normal file
0
lib/ansible/module_utils/parsing/__init__.py
Normal file
26
lib/ansible/module_utils/parsing/convert_bool.py
Normal file
26
lib/ansible/module_utils/parsing/convert_bool.py
Normal file
@@ -0,0 +1,26 @@
|
||||
# Copyright: 2017, Ansible Project
|
||||
# License: GNU General Public License v3 or later (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt )
|
||||
|
||||
from ansible.module_utils.six import binary_type, text_type
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
|
||||
BOOLEANS_TRUE = frozenset(('y', 'yes', 'on', '1', 'true', 't', 1, 1.0, True))
|
||||
BOOLEANS_FALSE = frozenset(('n', 'no', 'off', '0', 'false', 'f', 0, 0.0, False))
|
||||
BOOLEANS = BOOLEANS_TRUE.union(BOOLEANS_FALSE)
|
||||
|
||||
|
||||
def boolean(value, strict=True):
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
|
||||
normalized_value = value
|
||||
if isinstance(value, (text_type, binary_type)):
|
||||
normalized_value = to_text(value, errors='surrogate_or_strict').lower()
|
||||
|
||||
if normalized_value in BOOLEANS_TRUE:
|
||||
return True
|
||||
elif normalized_value in BOOLEANS_FALSE or not strict:
|
||||
return False
|
||||
|
||||
raise TypeError('%s is not a valid boolean. Valid booleans include: %s' % (to_text(value), ', '.join(repr(i) for i in BOOLEANS)))
|
||||
@@ -32,7 +32,6 @@ import os
|
||||
import re
|
||||
from uuid import UUID
|
||||
|
||||
from ansible.module_utils.basic import BOOLEANS
|
||||
from ansible.module_utils.six import text_type, binary_type
|
||||
|
||||
FINAL_STATUSES = ('ACTIVE', 'ERROR')
|
||||
|
||||
Reference in New Issue
Block a user