2021-06-09 18:44:10 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Global utility methods and classes."""
|
|
|
|
|
2023-10-16 10:11:22 +00:00
|
|
|
|
|
|
|
def strtobool(value):
|
|
|
|
"""Convert a string representation of truth to true or false."""
|
|
|
|
|
|
|
|
_map = {
|
|
|
|
"y": True,
|
|
|
|
"yes": True,
|
|
|
|
"t": True,
|
|
|
|
"true": True,
|
|
|
|
"on": True,
|
|
|
|
"1": True,
|
|
|
|
"n": False,
|
|
|
|
"no": False,
|
|
|
|
"f": False,
|
|
|
|
"false": False,
|
|
|
|
"off": False,
|
2023-11-10 13:50:58 +00:00
|
|
|
"0": False,
|
2023-10-16 10:11:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
return _map[str(value).lower()]
|
|
|
|
except KeyError as err:
|
|
|
|
raise ValueError(f'"{value}" is not a valid bool value') from err
|
2021-06-09 18:44:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def to_bool(string):
|
|
|
|
return bool(strtobool(str(string)))
|
|
|
|
|
|
|
|
|
|
|
|
class Singleton(type):
|
|
|
|
"""Meta singleton class."""
|
|
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
if cls not in cls._instances:
|
2023-02-12 14:11:15 +00:00
|
|
|
cls._instances[cls] = super().__call__(*args, **kwargs)
|
2021-06-09 18:44:10 +00:00
|
|
|
return cls._instances[cls]
|