aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Allan Webber <cwebber@dustycloud.org>2011-06-16 08:21:51 -0500
committerChristopher Allan Webber <cwebber@dustycloud.org>2011-06-16 08:21:51 -0500
commite2c6436e3e7c654a8d4196ac378debf185c74fa4 (patch)
treef1c2ddb4d9615d0d158ed3cac4f8b8a5a39ef852
parent0fcfff5a3a12fe7eeb0cabb862242776334879de (diff)
downloadmediagoblin-e2c6436e3e7c654a8d4196ac378debf185c74fa4.tar.lz
mediagoblin-e2c6436e3e7c654a8d4196ac378debf185c74fa4.tar.xz
mediagoblin-e2c6436e3e7c654a8d4196ac378debf185c74fa4.zip
Configuration file loading via ConfigObj.
Uses ConfigObj to open the config file. Also does validation via the config spec, so defaults are provided, strings are interpolated, types are converted.
-rw-r--r--mediagoblin/config.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/mediagoblin/config.py b/mediagoblin/config.py
new file mode 100644
index 00000000..533dbe19
--- /dev/null
+++ b/mediagoblin/config.py
@@ -0,0 +1,71 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# Copyright (C) 2011 Free Software Foundation, Inc
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import pkg_resources
+
+from configobj import ConfigObj
+from validate import Validator
+
+
+CONFIG_SPEC_PATH = pkg_resources.resource_filename(
+ 'mediagoblin', 'config_spec.ini')
+
+
+def _setup_defaults(config, config_path):
+ """
+ Setup DEFAULTS in a config object from an (absolute) config_path.
+ """
+ config.setdefault('DEFAULT', {})
+ config['DEFAULT']['here'] = os.path.dirname(config_path)
+ config['DEFAULT']['__file__'] = config_path
+
+
+def read_mediagoblin_config(config_path, config_spec=CONFIG_SPEC_PATH):
+ """
+ Read a config object from config_path.
+
+ Does automatic value transformation based on the config_spec.
+ Also provides %(__file__)s and %(here)s values of this file and
+ its directory respectively similar to paste deploy.
+
+ Args:
+ - config_path: path to the config file
+ - config_spec: config file that provides defaults and value types
+ for validation / conversion. Defaults to mediagoblin/config_spec.ini
+
+ Returns:
+ A read ConfigObj object.
+ """
+ config_path = os.path.abspath(config_path)
+
+ config_spec = ConfigObj(
+ CONFIG_SPEC_PATH,
+ encoding='UTF8', list_values=False, _inspec=True)
+
+ _setup_defaults(config_spec, config_path)
+
+ conf = ConfigObj(
+ config_path,
+ configspec=config_spec,
+ interpolation='ConfigParser')
+
+ _setup_defaults(conf, config_path)
+
+ conf.validate(Validator())
+
+ return conf
+