Avoid doing python setup.py sdist, use setup.py develop instead for tox
[mediagoblin.git] / mediagoblin / init / config.py
CommitLineData
e2c6436e 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
e2c6436e
CAW
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
7fadc33b 17import logging
e2c6436e
CAW
18import os
19import pkg_resources
20
4fd487f7 21from configobj import ConfigObj, flatten_errors
e2c6436e
CAW
22from validate import Validator
23
24
7fadc33b
CAW
25_log = logging.getLogger(__name__)
26
27
e2c6436e
CAW
28CONFIG_SPEC_PATH = pkg_resources.resource_filename(
29 'mediagoblin', 'config_spec.ini')
30
31
32def _setup_defaults(config, config_path):
33 """
34 Setup DEFAULTS in a config object from an (absolute) config_path.
35 """
36 config.setdefault('DEFAULT', {})
37 config['DEFAULT']['here'] = os.path.dirname(config_path)
38 config['DEFAULT']['__file__'] = config_path
39
40
41def read_mediagoblin_config(config_path, config_spec=CONFIG_SPEC_PATH):
42 """
43 Read a config object from config_path.
44
45 Does automatic value transformation based on the config_spec.
46 Also provides %(__file__)s and %(here)s values of this file and
47 its directory respectively similar to paste deploy.
48
7fadc33b
CAW
49 Also reads for [plugins] section, appends all config_spec.ini
50 files from said plugins into the general config_spec specification.
51
4fd487f7
CAW
52 This function doesn't itself raise any exceptions if validation
53 fails, you'll have to do something
54
e2c6436e
CAW
55 Args:
56 - config_path: path to the config file
57 - config_spec: config file that provides defaults and value types
58 for validation / conversion. Defaults to mediagoblin/config_spec.ini
59
60 Returns:
4fd487f7
CAW
61 A tuple like: (config, validation_result)
62 ... where 'conf' is the parsed config object and 'validation_result'
63 is the information from the validation process.
e2c6436e
CAW
64 """
65 config_path = os.path.abspath(config_path)
66
7fadc33b
CAW
67 # PRE-READ of config file. This allows us to fetch the plugins so
68 # we can add their plugin specs to the general config_spec.
69 config = ConfigObj(
70 config_path,
71 interpolation='ConfigParser')
72
e5cdd742 73 plugins = config.get("plugins", {}).keys()
7fadc33b
CAW
74 plugin_configs = {}
75
76 for plugin in plugins:
77 try:
78 plugin_config_spec_path = pkg_resources.resource_filename(
79 plugin, "config_spec.ini")
80 if not os.path.exists(plugin_config_spec_path):
81 continue
82
83 plugin_config_spec = ConfigObj(
84 plugin_config_spec_path,
85 encoding='UTF8', list_values=False, _inspec=True)
86 _setup_defaults(plugin_config_spec, config_path)
87
88 if not "plugin_spec" in plugin_config_spec:
89 continue
90
91 plugin_configs[plugin] = plugin_config_spec["plugin_spec"]
92
93 except ImportError:
94 _log.warning(
95 "When setting up config section, could not import '%s'" %
96 plugin)
97
98 # Now load the main config spec
e2c6436e 99 config_spec = ConfigObj(
e5aa1ec6 100 config_spec,
e2c6436e
CAW
101 encoding='UTF8', list_values=False, _inspec=True)
102
7fadc33b
CAW
103 # append the plugin specific sections of the config spec
104 config_spec['plugins'] = plugin_configs
105
e2c6436e
CAW
106 _setup_defaults(config_spec, config_path)
107
4fd487f7 108 config = ConfigObj(
e2c6436e
CAW
109 config_path,
110 configspec=config_spec,
111 interpolation='ConfigParser')
112
4fd487f7
CAW
113 _setup_defaults(config, config_path)
114
115 # For now the validator just works with the default functions,
116 # but in the future if we want to add additional validation/configuration
117 # functions we'd add them to validator.functions here.
243c3843 118 #
4fd487f7
CAW
119 # See also:
120 # http://www.voidspace.org.uk/python/validate.html#adding-functions
121 validator = Validator()
122 validation_result = config.validate(validator, preserve_errors=True)
123
124 return config, validation_result
125
126
9dba44de 127REPORT_HEADER = u"""\
4fd487f7
CAW
128There were validation problems loading this config file:
129--------------------------------------------------------
130"""
131
132
133def generate_validation_report(config, validation_result):
134 """
135 Generate a report if necessary of problems while validating.
136
137 Returns:
138 Either a string describing for a user the problems validating
139 this config or None if there are no problems.
140 """
141 report = []
142
143 # Organize the report
144 for entry in flatten_errors(config, validation_result):
145 # each entry is a tuple
146 section_list, key, error = entry
147
148 if key is not None:
149 section_list.append(key)
150 else:
151 section_list.append(u'[missing section]')
152
153 section_string = u':'.join(section_list)
e2c6436e 154
4fd487f7
CAW
155 if error == False:
156 # We don't care about missing values for now.
157 continue
e2c6436e 158
4fd487f7 159 report.append(u"%s = %s" % (section_string, error))
e2c6436e 160
4fd487f7
CAW
161 if report:
162 return REPORT_HEADER + u"\n".join(report)
163 else:
164 return None