Fix #5398 and #5395 - Fix tests failing due to problem creating connection for alembic
[larjonas-mediagoblin.git] / mediagoblin / init / __init__.py
blob38ec126048f8a04e1e26c362cac3155517b47fba
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 import jinja2
19 from mediagoblin.tools import staticdirect
20 from mediagoblin.tools.translate import set_available_locales
21 from mediagoblin.init.config import (
22 read_mediagoblin_config, generate_validation_report)
23 from mediagoblin import mg_globals
24 from mediagoblin.mg_globals import setup_globals
25 from mediagoblin.db.open import setup_connection_and_db_from_config, \
26 check_db_migrations_current, load_models
27 from mediagoblin.tools.pluginapi import hook_runall
28 from mediagoblin.tools.workbench import WorkbenchManager
29 from mediagoblin.storage import storage_system_from_config
31 from mediagoblin.tools.transition import DISABLE_GLOBALS
34 class Error(Exception):
35 pass
38 class ImproperlyConfigured(Error):
39 pass
42 def setup_locales():
43 """Checks which language translations are available and sets them"""
44 set_available_locales()
47 def setup_global_and_app_config(config_path):
48 global_config, validation_result = read_mediagoblin_config(config_path)
49 app_config = global_config['mediagoblin']
50 # report errors if necessary
51 validation_report = generate_validation_report(
52 global_config, validation_result)
53 if validation_report:
54 raise ImproperlyConfigured(validation_report)
56 setup_globals(
57 app_config=app_config,
58 global_config=global_config)
60 return global_config, app_config
63 def setup_database(app):
64 app_config = app.app_config
65 global_config = app.global_config
66 run_migrations = app_config['run_migrations']
68 # Load all models for media types (plugins, ...)
69 load_models(app_config)
70 # Set up the database
71 db = setup_connection_and_db_from_config(
72 app_config, run_migrations, app=app)
73 if run_migrations:
74 #Run the migrations to initialize/update the database.
75 from mediagoblin.gmg_commands.dbupdate import run_all_migrations
76 run_all_migrations(db, app_config, global_config)
77 else:
78 check_db_migrations_current(db)
80 setup_globals(database=db)
82 return db
85 def get_jinja_loader(user_template_path=None, current_theme=None,
86 plugin_template_paths=None):
87 """
88 Set up the Jinja template loaders, possibly allowing for user
89 overridden templates.
91 (In the future we may have another system for providing theming;
92 for now this is good enough.)
93 """
94 path_list = []
96 # Add user path first--this takes precedence over everything.
97 if user_template_path is not None:
98 path_list.append(jinja2.FileSystemLoader(user_template_path))
100 # Any theme directories in the registry
101 if current_theme and current_theme.get('templates_dir'):
102 path_list.append(
103 jinja2.FileSystemLoader(
104 current_theme['templates_dir']))
106 # Add plugin template paths next--takes precedence over
107 # core templates.
108 if plugin_template_paths is not None:
109 path_list.extend((jinja2.FileSystemLoader(path)
110 for path in plugin_template_paths))
112 # Add core templates last.
113 path_list.append(jinja2.PackageLoader('mediagoblin', 'templates'))
115 return jinja2.ChoiceLoader(path_list)
118 def get_staticdirector(app_config):
119 # At minimum, we need the direct_remote_path
120 if not 'direct_remote_path' in app_config \
121 or not 'theme_web_path' in app_config:
122 raise ImproperlyConfigured(
123 "direct_remote_path and theme_web_path must be provided")
125 direct_domains = {None: app_config['direct_remote_path'].strip()}
126 direct_domains['theme'] = app_config['theme_web_path'].strip()
128 # Let plugins load additional paths
129 for plugin_static in hook_runall("static_setup"):
130 direct_domains[plugin_static.name] = "%s/%s" % (
131 app_config['plugin_web_path'].rstrip('/'),
132 plugin_static.name)
134 return staticdirect.StaticDirect(
135 direct_domains)
138 def setup_storage():
139 global_config = mg_globals.global_config
141 key_short = 'publicstore'
142 key_long = "storage:" + key_short
143 public_store = storage_system_from_config(global_config[key_long])
145 key_short = 'queuestore'
146 key_long = "storage:" + key_short
147 queue_store = storage_system_from_config(global_config[key_long])
149 setup_globals(
150 public_store=public_store,
151 queue_store=queue_store)
153 return public_store, queue_store
156 def setup_workbench():
157 app_config = mg_globals.app_config
159 workbench_manager = WorkbenchManager(app_config['workbench_path'])
161 if not DISABLE_GLOBALS:
162 setup_globals(workbench_manager=workbench_manager)
164 return workbench_manager