1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
"""
Configuration loader using 'git-config'.
"""
import logging
import typing as ty
from git_pw import utils
LOG = logging.getLogger(__name__)
def parse_boolean(value: str) -> bool:
"""Parse a boolean config value.
Based on https://git-scm.com/docs/git-config#_values
"""
if value in ('yes', 'on', 'true', '1', ''):
return True
if value in ('no', 'off', 'false', '0'):
return False
LOG.error("'{}' is not a valid boolean value".format(value))
return False
class Config(object):
def __init__(self) -> None:
self._git_config: ty.Dict[str, str] = {}
def __getattribute__(self, name: str) -> str:
# attempt to use any attributes first
try:
value = super(Config, self).__getattribute__(name)
except AttributeError:
value = None
if value:
LOG.debug("Retrieved '{}' setting from cache".format(name))
return value
# fallback to reading from git config otherwise
value = utils.git_config('pw.{}'.format(name))
if value:
LOG.debug("Retrieved '{}' setting from git-config".format(name))
setattr(self, name, value)
return value
CONF = Config()
|