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