summaryrefslogtreecommitdiff
path: root/dotbot/config.py
blob: a9aafa00e4b1012ffdbbc41ee1b1b75818303e69 (plain)
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
import yaml
import json
from .util import string

class ConfigReader(object):
    def __init__(self, config_file_path):
        self._config = self._read(config_file_path)

    def _read(self, config_file_path):
        try:
            with open(config_file_path) as fin:
                try:
                    data = yaml.safe_load(fin)
                except Exception as e:
                    # try falling back to JSON, but return original exception
                    # if that fails too
                    try:
                        fin.seek(0)
                        data = json.load(fin)
                    except Exception:
                        raise e
            return data
        except Exception as e:
            msg = string.indent_lines(str(e))
            raise ReadingError('Could not read config file:\n%s' % msg)

    def get_config(self):
        return self._config

class ReadingError(Exception):
    pass