Passed
Push — master ( 61e6a6...28b40d )
by Emmanuel
07:42
created

stakkr.configreader.Config.read()   A

Complexity

Conditions 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 12
nop 1
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 3
rs 9.8
c 0
b 0
f 0
1
# coding: utf-8
2 1
"""Simple Config Reader."""
3
4 1
from collections.abc import Iterable
5 1
from os import path
6 1
from sys import stderr
7 1
import anyconfig
8 1
from jsonschema.exceptions import _Error
9 1
from stakkr.file_utils import get_file, find_project_dir
10
11
12 1
class Config:
13
    """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
14
    Parser of Stakkr.
15
16
    Set default values and validate stakkr.yml with specs
17
    """
18
19 1
    def __init__(self, config_file: str):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
20
        """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
21
        Build list of files to validate a config, set default values
22
        Then the given config file
23
        """
24 1
        self.config_file, self.project_dir = get_config_and_project_dir(config_file)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (84/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
25 1
        self._build_config_files_list()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
26 1
        self._build_config_schemas_list()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
27 1
        self.error = ''
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
28
29 1
    def display_errors(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
30
        """Display errors in STDOUT."""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
31
        from click import style
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
32
33
        msg = 'Failed validating config ('
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
34
        msg += ', '.join(self.config_files)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
35
        msg += '):\n    - {}\n'.format(self.error)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
36
        stderr.write(style(msg, fg='red'))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
37
38 1
    def read(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
39
        """
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
40
        Parse the configs and validate it.
41
42
        It could be either local or from a local services
43
        (first local then packages by alphabetical order).
44
        """
45 1
        schema = anyconfig.multi_load(self.spec_files)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
46 1
        config = anyconfig.multi_load(self.config_files)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
47
        # Make sure the compiled configuration is valid
48 1
        try:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
49 1
            anyconfig.validate(config, schema, safe=False)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
50 1
        except _Error as error:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
51 1
            self.error = '{} ({})'.format(error.message, ' -> '.join(map(str, error.path)))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (91/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
52 1
            return False
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
53
54 1
        config['project_dir'] = path.realpath(path.dirname(self.config_file))
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
55 1
        if config['project_name'] == '':
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
56 1
            config['project_name'] = path.basename(config['project_dir'])
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
57
58 1
        return config
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
59
60 1
    def _build_config_files_list(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
61 1
        self.config_files = [
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
62
            # Stakkr default config
63
            get_file('static', 'config_default.yml'),
64
            '{}/services/*/config_default.yml'.format(self.project_dir)]
65
        # Stakkr main config file finally with user's values
66 1
        self.config_files += [self.config_file]
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
67
68 1
    def _build_config_schemas_list(self):
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
69 1
        self.spec_files = [
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
70
            # Stakkr config validation
71
            get_file('static', 'config_schema.yml'),
72
            '{}/services/*/config_schema.yml'.format(self.project_dir)]
73
74
75 1
def get_config_and_project_dir(config_file: str):
76
    """Guess config file name and project dir"""
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
77 1
    if config_file is not None:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
78 1
        config_file = path.abspath(config_file)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
79 1
        project_dir = path.dirname(config_file)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
80
    else:
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
81
        project_dir = find_project_dir()
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
82
        config_file = '{}/stakkr.yml'.format(project_dir)
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
83
84
    return config_file, project_dir
0 ignored issues
show
Coding Style introduced by
Found indentation with spaces instead of tabs
Loading history...
85