-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
218 lines (156 loc) · 6.24 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2016.
# Author(s):
# Martin Raspaud <martin.raspaud@smhi.se>
# Adam Dybbroe <adam.dybbroe@smhi.se>
# David Hoese <david.hoese@ssec.wisc.edu>
# This file is part of the satpy.
# satpy is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# satpy is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with satpy. If not, see <http://www.gnu.org/licenses/>.
"""Configuration directory and file handling
"""
import logging
import os
import unittest
from collections import Mapping
import yaml
from six.moves import configparser
LOG = logging.getLogger(__name__)
def recursive_dict_update(d, u):
"""Recursive dictionary update.
Copied from:
http://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
"""
for k, v in u.items():
if isinstance(v, Mapping):
r = recursive_dict_update(d.get(k, {}), v)
d[k] = r
else:
d[k] = u[k]
return d
def config_search_paths(filename, *search_dirs, **kwargs):
"""Get the config path in search_dirs."""
paths = []
paths += [os.path.join(search_dir, filename)
for search_dir in search_dirs
if search_dir is not None]
if kwargs.get("check_exists", True):
paths = [x for x in paths if os.path.isfile(x)]
return paths
class MyParser(configparser.ConfigParser):
"""PFE.
http://stackoverflow.com/questions/3220670/read-all-the-contents-in-ini-file-into-dictionary-with-python
"""
def as_dict(self):
"""Return config file content as a dictionary."""
d__ = dict(self._sections)
for key in d__:
d__[key] = dict(self._defaults, **d__[key])
d__[key].pop('__name__', None)
return d__
class ConfigFinder(object):
"""Helper to find config files and load them in a layered manner."""
def __init__(self, *paths):
"""*paths* to find the config files in.
Config files in first paths get overriden by the config files in the
later paths.
"""
self.paths = paths
def __call__(self, filename):
"""Find and merge configs."""
config_files = config_search_paths(
filename, *self.paths, check_exists=True)
ext = os.path.splitext(filename)[1]
if ext in ['.cfg', '.ini']:
cfg = MyParser()
for config_file in config_files:
cfg.read(config_file)
return cfg.as_dict()
elif ext in ['.yaml', '.yml']:
cfg = {}
for config_file in config_files:
with open(config_file) as fd_:
cfg = recursive_dict_update(cfg, yaml.load(fd_))
return cfg
class TestRecursiveDictUpdate(unittest.TestCase):
"""Test the recursive dict updating."""
def test_recursive_dict_update(self):
dict1 = {'a': 1, 'b': {'c': 3, 'd': 4}}
dict2 = {'a': 2, 'b': {'d': 5}}
self.assertDictEqual(recursive_dict_update(dict1, dict2),
{'a': 2,
'b': {'c': 3, 'd': 5}})
class TestConfigSearch(unittest.TestCase):
"""Test the config file search."""
def test_config_search_path(self):
"""Test the config_search_path function."""
self.assertEqual(config_search_paths('conf.cfg', '/tmp', '.', check_exists=False),
['/tmp/conf.cfg', './conf.cfg'])
self.assertEqual(config_search_paths('conf.cfg', '/tmp', '.'), [])
with open('/tmp/conf.cfg', 'w'):
pass
with open('./conf.cfg', 'w'):
pass
self.assertEqual(config_search_paths('conf.cfg', '/tmp', '.'),
['/tmp/conf.cfg', './conf.cfg'])
os.remove('/tmp/conf.cfg')
os.remove('./conf.cfg')
class TestConfigFinder(unittest.TestCase):
"""Test config_finder."""
def test_yaml(self):
"""Test yaml merging."""
config_finder = ConfigFinder('/tmp', '.')
dict1 = {'a': 1, 'b': {'c': 3, 'd': 4}}
dict2 = {'a': 2, 'b': {'d': 5}}
with open('/tmp/conf.yaml', 'w') as fd_:
yaml.dump(dict1, fd_)
with open('./conf.yaml', 'w') as fd_:
yaml.dump(dict2, fd_)
self.assertDictEqual(config_finder('conf.yaml'),
{'a': 2,
'b': {'c': 3, 'd': 5}})
os.remove('/tmp/conf.yaml')
os.remove('./conf.yaml')
def test_ini(self):
"""Test ini merging."""
config_finder = ConfigFinder('/tmp', '.')
dict1 = {'a': {'e': 7}, 'b': {'c': 3, 'd': 4}}
cfg = configparser.ConfigParser()
for section, content in dict1.items():
cfg.add_section(section)
for key, val in content.items():
cfg.set(section, key, str(val))
with open('/tmp/conf.ini', 'w') as fd_:
cfg.write(fd_)
dict2 = {'b': {'d': 5}}
cfg = configparser.ConfigParser()
for section, content in dict2.items():
cfg.add_section(section)
for key, val in content.items():
cfg.set(section, key, str(val))
with open('./conf.ini', 'w') as fd_:
cfg.write(fd_)
self.assertDictEqual(config_finder('conf.ini'),
{'a': {'e': '7'},
'b': {'c': '3', 'd': '5'}})
os.remove('/tmp/conf.ini')
os.remove('./conf.ini')
def test_suite():
"""The suite."""
loader = unittest.TestLoader()
mysuite = unittest.TestSuite()
mysuite.addTest(loader.loadTestsFromTestCase(TestRecursiveDictUpdate))
mysuite.addTest(loader.loadTestsFromTestCase(TestConfigSearch))
mysuite.addTest(loader.loadTestsFromTestCase(TestConfigFinder))
return mysuite
if __name__ == '__main__':
unittest.main()