Let's specifically import string_list()s in the config_spec.
[mediagoblin.git] / mediagoblin / config.py
CommitLineData
e2c6436e
CAW
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2011 Free Software Foundation, Inc
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import os
18import pkg_resources
19
20from configobj import ConfigObj
21from validate import Validator
22
23
24CONFIG_SPEC_PATH = pkg_resources.resource_filename(
25 'mediagoblin', 'config_spec.ini')
26
27
28def _setup_defaults(config, config_path):
29 """
30 Setup DEFAULTS in a config object from an (absolute) config_path.
31 """
32 config.setdefault('DEFAULT', {})
33 config['DEFAULT']['here'] = os.path.dirname(config_path)
34 config['DEFAULT']['__file__'] = config_path
35
36
37def read_mediagoblin_config(config_path, config_spec=CONFIG_SPEC_PATH):
38 """
39 Read a config object from config_path.
40
41 Does automatic value transformation based on the config_spec.
42 Also provides %(__file__)s and %(here)s values of this file and
43 its directory respectively similar to paste deploy.
44
45 Args:
46 - config_path: path to the config file
47 - config_spec: config file that provides defaults and value types
48 for validation / conversion. Defaults to mediagoblin/config_spec.ini
49
50 Returns:
51 A read ConfigObj object.
52 """
53 config_path = os.path.abspath(config_path)
54
55 config_spec = ConfigObj(
56 CONFIG_SPEC_PATH,
57 encoding='UTF8', list_values=False, _inspec=True)
58
59 _setup_defaults(config_spec, config_path)
60
61 conf = ConfigObj(
62 config_path,
63 configspec=config_spec,
64 interpolation='ConfigParser')
65
66 _setup_defaults(conf, config_path)
67
68 conf.validate(Validator())
69
70 return conf
71