Completed
Pull Request — develop (#12)
by Adam
52s
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
30
LOG = logging.getLogger(__name__)
31
32
#from six.moves import configparser
33
import yaml
34
from collections import Mapping
35
36
import pkg_resources
37
BUILTIN_CONFIG_FILE = pkg_resources.resource_filename(__name__,
38
                                                      os.path.join('etc', 'pyspectral.yaml'))
39
40
CONFIG_FILE = os.environ.get('PSP_CONFIG_FILE')
41
42
if CONFIG_FILE is not None and (not os.path.exists(CONFIG_FILE) or
43
                                not os.path.isfile(CONFIG_FILE)):
44
    raise IOError(str(CONFIG_FILE) + " pointed to by the environment " +
45
                  "variable PSP_CONFIG_FILE is not a file or does not exist!")
46
47
48
# def get_config():
49
#     """Get config from file"""
50
51
#     conf = configparser.ConfigParser()
52
#     conf.read(BUILTIN_CONFIG_FILE)
53
#     if CONFIG_FILE is not None:
54
#         try:
55
#             conf.read(CONFIG_FILE)
56
#         except configparser.NoSectionError:
57
#             LOG.info('Failed reading configuration file: %s',
58
#                      str(CONFIG_FILE))
59
60
#     return conf
61
62
def recursive_dict_update(d, u):
63
    """Recursive dictionary update using
64
65
    Copied from:
66
67
        http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
68
69
    """
70
    for k, v in u.items():
71
        if isinstance(v, Mapping):
72
            r = recursive_dict_update(d.get(k, {}), v)
73
            d[k] = r
74
        else:
75
            d[k] = u[k]
76
    return d
77
78
79
def get_config():
80
    """Get the configuration from file"""
81
82
    if CONFIG_FILE is not None:
83
        configfile = CONFIG_FILE
84
    else:
85
        configfile = BUILTIN_CONFIG_FILE
86
87
    config = {}
88
    with open(configfile, 'r') as fp_:
89
        config = recursive_dict_update(config, yaml.load(fp_))
90
91
    return config
92