mirror of
https://github.com/thegeeklab/docker-tidy.git
synced 2024-11-16 17:50:39 +00:00
49 lines
1.0 KiB
Python
49 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Global utility methods and classes."""
|
|
|
|
|
|
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,
|
|
"0": False,
|
|
}
|
|
|
|
try:
|
|
return _map[str(value).lower()]
|
|
except KeyError as err:
|
|
raise ValueError(f'"{value}" is not a valid bool value') from err
|
|
|
|
|
|
def to_bool(string):
|
|
return bool(strtobool(str(string)))
|
|
|
|
|
|
def dict_intersect(d1, d2):
|
|
return {
|
|
k: dict_intersect(d1[k], d2[k]) if isinstance(d1[k], dict) else d1[k]
|
|
for k in d1.keys() & d2.keys()
|
|
}
|
|
|
|
|
|
class Singleton(type):
|
|
"""Singleton metaclass."""
|
|
|
|
_instances = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
if cls not in cls._instances:
|
|
cls._instances[cls] = super().__call__(*args, **kwargs)
|
|
return cls._instances[cls]
|