Completed
Pull Request — develop (#12)
by Adam
30s
created

recursive_dict_update()   A

Complexity

Conditions 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
dl 0
loc 15
rs 9.4285
c 1
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
# Copyright (c) 2013, 2015, 2016, 2017 Adam.Dybbroe
5
6
# Author(s):
7
8
#   Adam.Dybbroe <[email protected]>
9
#   Panu Lahtinen <[email protected]>
10
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the GNU General Public License as published by
13
# the Free Software Foundation, either version 3 of the License, or
14
# (at your option) any later version.
15
16
# This program is distributed in the hope that it will be useful,
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
# GNU General Public License for more details.
20
21
# You should have received a copy of the GNU General Public License
22
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
24
"""Main module"""
25
26
from pyspectral.version import __version__
27
import logging
28
import os
29
#from six.moves import configparser
30
import yaml
31
from collections import Mapping
32
import pkg_resources
33
34
LOG = logging.getLogger(__name__)
35
36
BUILTIN_CONFIG_FILE = pkg_resources.resource_filename(__name__,
37
                                                      os.path.join('etc', 'pyspectral.yaml'))
38
39
CONFIG_FILE = os.environ.get('PSP_CONFIG_FILE')
40
41
if CONFIG_FILE is not None and (not os.path.exists(CONFIG_FILE) or
42
                                not os.path.isfile(CONFIG_FILE)):
43
    raise IOError(str(CONFIG_FILE) + " pointed to by the environment " +
44
                  "variable PSP_CONFIG_FILE is not a file or does not exist!")
45
46
47
# def get_config():
48
#     """Get config from file"""
49
50
#     conf = configparser.ConfigParser()
51
#     conf.read(BUILTIN_CONFIG_FILE)
52
#     if CONFIG_FILE is not None:
53
#         try:
54
#             conf.read(CONFIG_FILE)
55
#         except configparser.NoSectionError:
56
#             LOG.info('Failed reading configuration file: %s',
57
#                      str(CONFIG_FILE))
58
59
#     return conf
60
61
def recursive_dict_update(d, u):
62
    """Recursive dictionary update using
63
64
    Copied from:
65
66
        http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
67
68
    """
69
    for k, v in u.items():
70
        if isinstance(v, Mapping):
71
            r = recursive_dict_update(d.get(k, {}), v)
72
            d[k] = r
73
        else:
74
            d[k] = u[k]
75
    return d
76
77
78
def get_config():
79
    """Get the configuration from file"""
80
81
    if CONFIG_FILE is not None:
82
        configfile = CONFIG_FILE
83
    else:
84
        configfile = BUILTIN_CONFIG_FILE
85
86
    config = {}
87
    with open(configfile, 'r') as fp_:
88
        config = recursive_dict_update(config, yaml.load(fp_))
89
90
    return config
91