git-batch/gitbatch/utils/__init__.py

50 lines
1023 B
Python
Raw Normal View History

2019-12-02 08:53:35 +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)))
return None
2019-12-02 08:53:35 +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
2019-12-02 08:53:35 +00:00
def to_bool(string):
return bool(strtobool(str(string)))
class Singleton(type):
2020-04-11 11:17:01 +00:00
"""Meta singleton class."""
2019-12-02 08:53:35 +00:00
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
2019-12-02 08:53:35 +00:00
return cls._instances[cls]