2016-01-13 11:29:12 -05:00
|
|
|
import json
|
2016-01-13 13:46:41 -05:00
|
|
|
import os.path
|
2022-04-30 21:46:09 -04:00
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2015-04-24 05:47:42 -04:00
|
|
|
from .util import string
|
2014-03-19 23:07:30 -04:00
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
|
2023-09-09 20:39:45 -04:00
|
|
|
class ConfigReader:
|
2014-03-19 23:07:30 -04:00
|
|
|
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:
|
2022-01-30 18:48:30 -05:00
|
|
|
if ext == ".json":
|
2016-01-13 13:46:41 -05:00
|
|
|
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))
|
2022-01-30 18:48:30 -05:00
|
|
|
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
|
|
|
|
|
2022-01-30 18:48:30 -05:00
|
|
|
|
2014-03-19 23:07:30 -04:00
|
|
|
class ReadingError(Exception):
|
|
|
|
pass
|