Skip to content

Commit

Permalink
update util function for parse-bool
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara committed May 7, 2024
1 parent 1b666b2 commit a95a453
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions ansible_risk_insight/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@
import ansible_risk_insight.logger as logger


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)
bool_values_true = frozenset(("y", "yes", "on", "1", "true", "t", 1, 1.0, True))
bool_values_false = frozenset(("n", "no", "off", "0", "false", "f", 0, 0.0, False))
bool_values = bool_values_true.union(bool_values_false)

try:
codecs.lookup_error("surrogateescape")
HAS_SURROGATEESCAPE = True
_has_surrogateescape = True
except LookupError:
HAS_SURROGATEESCAPE = False
_has_surrogateescape = False

_COMPOSED_ERROR_HANDLERS = frozenset((None, "surrogate_or_replace", "surrogate_or_strict", "surrogate_then_replace"))
_surrogate_error_handlers = frozenset((None, "surrogate_or_replace", "surrogate_or_strict", "surrogate_then_replace"))


def lock_file(fpath, timeout=10):
Expand Down Expand Up @@ -755,12 +755,12 @@ def is_test_object(path: str):
return path.startswith("tests/integration/") or path.startswith("molecule/")


def to_str(obj, encoding="utf-8", errors=None, nonstring="simplerepr"):
def to_str(obj, encoding="utf-8", errors=None):
if isinstance(obj, str):
return obj

if errors in _COMPOSED_ERROR_HANDLERS:
if HAS_SURROGATEESCAPE:
if errors in _surrogate_error_handlers:
if _has_surrogateescape:
errors = "surrogateescape"
elif errors == "surrogate_or_strict":
errors = "strict"
Expand All @@ -770,23 +770,14 @@ def to_str(obj, encoding="utf-8", errors=None, nonstring="simplerepr"):
if isinstance(obj, bytes):
return obj.decode(encoding, errors)

if nonstring == "simplerepr":
try:
value = str(obj)
except UnicodeError:
try:
value = str(obj)
value = repr(obj)
except UnicodeError:
try:
value = repr(obj)
except UnicodeError:
# Giving up
return ""
elif nonstring == "passthru":
return obj
elif nonstring == "empty":
return ""
elif nonstring == "strict":
raise TypeError("obj must be a string type")
else:
raise TypeError("Invalid value %s for to_text's nonstring parameter" % nonstring)
# Giving up
return ""

return to_str(value, encoding, errors)

Expand All @@ -799,9 +790,9 @@ def parse_bool(value, strict=True):
if isinstance(value, (str, bytes)):
normalized_value = to_str(value, errors="surrogate_or_strict").lower().strip()

if normalized_value in BOOLEANS_TRUE:
if normalized_value in bool_values_true:
return True
elif normalized_value in BOOLEANS_FALSE or not strict:
elif normalized_value in bool_values_false or not strict:
return False

raise TypeError("The value '%s' is not a valid boolean. Valid booleans include: %s" % (to_str(value), ", ".join(repr(i) for i in BOOLEANS)))
raise TypeError("The value '%s' is not a valid boolean. Valid booleans include: %s" % (to_str(value), ", ".join(repr(i) for i in bool_values)))

0 comments on commit a95a453

Please sign in to comment.