git-linter/git_lint/git_lint_config.py

37 lines
1.1 KiB
Python
Raw Normal View History

2016-09-23 20:11:06 +00:00
import sys
import os.path
import gettext
import ConfigParser
2016-09-23 22:27:15 +00:00
2016-09-23 20:11:06 +00:00
_ = gettext.gettext
2016-09-23 22:27:15 +00:00
2016-09-23 20:11:06 +00:00
def _find_config_file(options, base):
2016-09-23 22:27:15 +00:00
if 'config' in options:
2016-09-23 20:11:06 +00:00
config = options['config']
configpath = os.path.abspath(config)
if not os.path.isfile(configpath):
sys.exit(_('Configuration file not found: {}\n').format(config))
return configpath
home = os.path.join(os.environ.get('HOME'))
possibles = (os.path.join(base, '.git-lint'),
os.path.join(base, '.git-lint/config'),
os.path.join(home, '.git-lint'),
os.path.join(home, '.git-lint/config'))
2016-09-23 22:27:15 +00:00
matches = [p for p in possibles if os.path.isfile(p)]
2016-09-23 20:11:06 +00:00
if len(matches) == 0:
2016-09-23 22:27:15 +00:00
sys.exit(_('No configuration file found'))
2016-09-23 20:11:06 +00:00
return matches[0]
2016-09-23 22:27:15 +00:00
2016-09-23 20:11:06 +00:00
def get_config(options, base):
path = find_config_file(options, base)
configloader = ConfigParser.SafeConfigParser()
configloader.read(path)
configloader.set('DEFAULT', 'repdir', base)
return {section: {k, v for (k, v) in configloader.items(section)}
for section in configloader.sections()}