__init__.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 jinja2
  17. from mediagoblin.tools import staticdirect
  18. from mediagoblin.tools.translate import set_available_locales
  19. from mediagoblin.init.config import (
  20. read_mediagoblin_config, generate_validation_report)
  21. from mediagoblin import mg_globals
  22. from mediagoblin.mg_globals import setup_globals
  23. from mediagoblin.db.open import setup_connection_and_db_from_config, \
  24. check_db_migrations_current, load_models
  25. from mediagoblin.tools.pluginapi import hook_runall
  26. from mediagoblin.tools.workbench import WorkbenchManager
  27. from mediagoblin.storage import storage_system_from_config
  28. from mediagoblin.tools.transition import DISABLE_GLOBALS
  29. class Error(Exception):
  30. pass
  31. class ImproperlyConfigured(Error):
  32. pass
  33. def setup_locales():
  34. """Checks which language translations are available and sets them"""
  35. set_available_locales()
  36. def setup_global_and_app_config(config_path):
  37. global_config, validation_result = read_mediagoblin_config(config_path)
  38. app_config = global_config['mediagoblin']
  39. # report errors if necessary
  40. validation_report = generate_validation_report(
  41. global_config, validation_result)
  42. if validation_report:
  43. raise ImproperlyConfigured(validation_report)
  44. setup_globals(
  45. app_config=app_config,
  46. global_config=global_config)
  47. return global_config, app_config
  48. def setup_database(app):
  49. app_config = app.app_config
  50. global_config = app.global_config
  51. run_migrations = app_config['run_migrations']
  52. # Load all models for media types (plugins, ...)
  53. load_models(app_config)
  54. # Set up the database
  55. db = setup_connection_and_db_from_config(
  56. app_config, run_migrations, app=app)
  57. if run_migrations:
  58. #Run the migrations to initialize/update the database.
  59. from mediagoblin.gmg_commands.dbupdate import run_all_migrations
  60. run_all_migrations(db, app_config, global_config)
  61. else:
  62. check_db_migrations_current(db)
  63. setup_globals(database=db)
  64. return db
  65. def get_jinja_loader(user_template_path=None, current_theme=None,
  66. plugin_template_paths=None):
  67. """
  68. Set up the Jinja template loaders, possibly allowing for user
  69. overridden templates.
  70. (In the future we may have another system for providing theming;
  71. for now this is good enough.)
  72. """
  73. path_list = []
  74. # Add user path first--this takes precedence over everything.
  75. if user_template_path is not None:
  76. path_list.append(jinja2.FileSystemLoader(user_template_path))
  77. # Any theme directories in the registry
  78. if current_theme and current_theme.get('templates_dir'):
  79. path_list.append(
  80. jinja2.FileSystemLoader(
  81. current_theme['templates_dir']))
  82. # Add plugin template paths next--takes precedence over
  83. # core templates.
  84. if plugin_template_paths is not None:
  85. path_list.extend((jinja2.FileSystemLoader(path)
  86. for path in plugin_template_paths))
  87. # Add core templates last.
  88. path_list.append(jinja2.PackageLoader('mediagoblin', 'templates'))
  89. return jinja2.ChoiceLoader(path_list)
  90. def get_staticdirector(app_config):
  91. # At minimum, we need the direct_remote_path
  92. if not 'direct_remote_path' in app_config \
  93. or not 'theme_web_path' in app_config:
  94. raise ImproperlyConfigured(
  95. "direct_remote_path and theme_web_path must be provided")
  96. direct_domains = {None: app_config['direct_remote_path'].strip()}
  97. direct_domains['theme'] = app_config['theme_web_path'].strip()
  98. # Let plugins load additional paths
  99. for plugin_static in hook_runall("static_setup"):
  100. direct_domains[plugin_static.name] = "%s/%s" % (
  101. app_config['plugin_web_path'].rstrip('/'),
  102. plugin_static.name)
  103. return staticdirect.StaticDirect(
  104. direct_domains)
  105. def setup_storage():
  106. global_config = mg_globals.global_config
  107. key_short = 'publicstore'
  108. key_long = "storage:" + key_short
  109. public_store = storage_system_from_config(global_config[key_long])
  110. key_short = 'queuestore'
  111. key_long = "storage:" + key_short
  112. queue_store = storage_system_from_config(global_config[key_long])
  113. setup_globals(
  114. public_store=public_store,
  115. queue_store=queue_store)
  116. return public_store, queue_store
  117. def setup_workbench():
  118. app_config = mg_globals.app_config
  119. workbench_manager = WorkbenchManager(app_config['workbench_path'])
  120. if not DISABLE_GLOBALS:
  121. setup_globals(workbench_manager=workbench_manager)
  122. return workbench_manager