ppp_core.CoreConfig   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 21
ccs 18
cts 18
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A _parse_modules() 0 10 4
A parse_config() 0 6 1
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 ppp_libmodule.config import Config
8 1
from ppp_libmodule.exceptions import InvalidConfig
9
10 1
class Module(namedtuple('_Module', 'name url coefficient filters method')):
11
    """Represents a modules of the core with its name, URL, and a
12
    coefficient applied to it self-computed pertinence."""
13 1
    def __new__(cls, name, url, coefficient=1, filters=None, **kwargs):
14
        if kwargs: # pragma: no cover
15
            logging.warning('Ignored arguments to module config: %r' % kwargs)
16 1
        if url.startswith('python:'):
17 1
            url = url[len('python:'):]
18 1
            method = 'python'
19
        else:
20 1
            method = 'http'
21 1
        return super(Module, cls).__new__(cls,
22
                                          name=name,
23
                                          url=url,
24
                                          method=method,
25
                                          coefficient=coefficient,
26
                                          filters=filters or {})
27
28 1
    def should_send(self, request):
29
        """Returns whether or not the request should be sent to the
30
        modules, based on the filters."""
31 1
        if self.filters.get('whitelist', None):
32 1
            return request.tree.type in self.filters['whitelist']
33 1
        elif self.filters.get('blacklist', None):
34 1
            return request.tree.type not in self.filters['blacklist']
35
        else:
36 1
            return True
37
38
39 1
class CoreConfig(Config):
40 1
    __slots__ = ('modules', 'nb_passes', 'loglevel', 'verbose_log_url')
41 1
    config_path_variable = 'PPP_CORE_CONFIG'
42
43 1
    def parse_config(self, data):
44 1
        self.modules = self._parse_modules(data.get('modules', {}))
45 1
        self.debug = data.get('debug', False)
46 1
        self.nb_passes = data.get('recursion', {}).get('max_passes', 10)
47 1
        self.loglevel = data.get('log', {}).get('level', 'warning')
48 1
        self.verbose_log_url = data.get('log', {}).get('verbose_database_url', None)
49
50 1
    def _parse_modules(self, data):
51 1
        modules = []
52 1
        for config in data:
53 1
            if 'name' not in config:
54 1
                raise InvalidConfig('Module %r has no name' % config)
55 1
            if 'url' not in config:
56 1
                raise InvalidConfig('Module %s has no set URL.' %
57
                        config['name'])
58 1
            modules.append(Module(**config))
59 1
        return modules
60
61