Config.__init__()   B
last analyzed

Complexity

Conditions 6

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.027

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 6
c 3
b 0
f 0
dl 0
loc 13
ccs 10
cts 11
cp 0.9091
crap 6.027
rs 8
1
"""Configuration module."""
2
3 1
import os
4 1
import json
5 1
import logging
6 1
from collections import namedtuple
7 1
from .exceptions import InvalidConfig
8
9
10 1
class Config:
11 1
    __slots__ = ('debug',)
12 1
    def __init__(self, data=None):
13 1
        if not hasattr(self, 'config_path_variable') or \
14
                not hasattr(self, 'parse_config'):
15 1
            raise NotImplementedError('Config class does not implement all '
16
                                      'required attributes.')
17 1
        self.debug = True
18 1
        if not data:
19 1
            try:
20 1
                with open(self.get_config_path()) as fd:
21 1
                    data = json.load(fd)
22 1
            except ValueError as exc:
23
                raise InvalidConfig(*exc.args)
24 1
        self.parse_config(data)
25
26 1
    @classmethod
27
    def get_config_path(cls):
28 1
        path = os.environ.get(cls.config_path_variable, '')
29 1
        if not path:
30 1
            raise InvalidConfig('Could not find config file, please set '
31
                                'environment variable $%s.' %
32
                                cls.config_path_variable)
33 1
        return path
34
35