Completed
Push — master ( 23b608...8098ec )
by Thomas
01:16
created

ppp_libmodule.Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%
Metric Value
dl 0
loc 24
ccs 17
cts 18
cp 0.9444
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B Config.__init__() 0 13 6
A Config.get_config_path() 0 8 2
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