config.py 5.8 KB

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