2020-05-03 21:42:52 +00:00
|
|
|
"""Global utility methods and classes."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_path(path):
|
|
|
|
if path:
|
|
|
|
return os.path.abspath(os.path.expanduser(os.path.expandvars(path)))
|
|
|
|
|
2023-02-12 12:40:45 +00:00
|
|
|
return None
|
|
|
|
|
2020-05-03 21:42:52 +00:00
|
|
|
|
2023-10-16 10:11:09 +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,
|
|
|
|
"0": False
|
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
return _map[str(value).lower()]
|
|
|
|
except KeyError as err:
|
|
|
|
raise ValueError(f'"{value}" is not a valid bool value') from err
|
|
|
|
|
|
|
|
|
2020-05-03 21:42:52 +00:00
|
|
|
def to_bool(string):
|
|
|
|
return bool(strtobool(str(string)))
|
|
|
|
|
|
|
|
|
|
|
|
def trim_prefix(text, prefix):
|
|
|
|
if text.startswith(prefix):
|
|
|
|
return text[len(prefix):]
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
|
|
def to_prerelease(tup):
|
2023-05-30 12:07:30 +00:00
|
|
|
return ".".join(tup)
|
2020-05-03 21:42:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Singleton(type):
|
|
|
|
"""Meta singleton class."""
|
|
|
|
|
|
|
|
_instances = {}
|
|
|
|
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
if cls not in cls._instances:
|
2023-02-12 12:40:45 +00:00
|
|
|
cls._instances[cls] = super().__call__(*args, **kwargs)
|
2020-05-03 21:42:52 +00:00
|
|
|
return cls._instances[cls]
|