2014-10-27 20:04:00 -04:00
|
|
|
import yaml
|
2016-01-13 11:29:12 -05:00
|
|
|
import json
|
2016-01-13 13:46:41 -05:00
|
|
|
import os.path
|
2015-04-24 05:47:42 -04:00
|
|
|
from .util import string
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
class ConfigReader(object):
|
|
|
|
def __init__(self, config_file_path):
|
|
|
|
self._config = self._read(config_file_path)
|
|
|
|
|
|
|
|
def _read(self, config_file_path):
|
|
|
|
try:
|
2016-01-13 13:46:41 -05:00
|
|
|
_, ext = os.path.splitext(config_file_path)
|
2014-03-19 23:07:30 -04:00
|
|
|
with open(config_file_path) as fin:
|
2016-01-13 13:46:41 -05:00
|
|
|
if ext == '.json':
|
|
|
|
data = json.load(fin)
|
|
|
|
else:
|
2016-01-13 11:29:12 -05:00
|
|
|
data = yaml.safe_load(fin)
|
2014-03-19 23:07:30 -04:00
|
|
|
return data
|
2015-04-24 05:47:42 -04:00
|
|
|
except Exception as e:
|
|
|
|
msg = string.indent_lines(str(e))
|
|
|
|
raise ReadingError('Could not read config file:\n%s' % msg)
|
2014-03-19 23:07:30 -04:00
|
|
|
|
|
|
|
def get_config(self):
|
|
|
|
return self._config
|
|
|
|
|
|
|
|
class ReadingError(Exception):
|
|
|
|
pass
|