Work towards getting plugin static linking/serving to work
[mediagoblin.git] / mediagoblin / init / __init__.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 jinja2
18
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
30
31
32 class Error(Exception):
33 pass
34
35
36 class ImproperlyConfigured(Error):
37 pass
38
39
40 def setup_locales():
41 """Checks which language translations are available and sets them"""
42 set_available_locales()
43
44
45 def setup_global_and_app_config(config_path):
46 global_config, validation_result = read_mediagoblin_config(config_path)
47 app_config = global_config['mediagoblin']
48 # report errors if necessary
49 validation_report = generate_validation_report(
50 global_config, validation_result)
51 if validation_report:
52 raise ImproperlyConfigured(validation_report)
53
54 setup_globals(
55 app_config=app_config,
56 global_config=global_config)
57
58 return global_config, app_config
59
60
61 def setup_database():
62 app_config = mg_globals.app_config
63
64 # Load all models for media types (plugins, ...)
65 load_models(app_config)
66
67 # Set up the database
68 db = setup_connection_and_db_from_config(app_config)
69
70 check_db_migrations_current(db)
71
72 setup_globals(database=db)
73
74 return db
75
76
77 def get_jinja_loader(user_template_path=None, current_theme=None,
78 plugin_template_paths=None):
79 """
80 Set up the Jinja template loaders, possibly allowing for user
81 overridden templates.
82
83 (In the future we may have another system for providing theming;
84 for now this is good enough.)
85 """
86 path_list = []
87
88 # Add user path first--this takes precedence over everything.
89 if user_template_path is not None:
90 path_list.append(jinja2.FileSystemLoader(user_template_path))
91
92 # Any theme directories in the registry
93 if current_theme and current_theme.get('templates_dir'):
94 path_list.append(
95 jinja2.FileSystemLoader(
96 current_theme['templates_dir']))
97
98 # Add plugin template paths next--takes precedence over
99 # core templates.
100 if plugin_template_paths is not None:
101 path_list.extend((jinja2.FileSystemLoader(path)
102 for path in plugin_template_paths))
103
104 # Add core templates last.
105 path_list.append(jinja2.PackageLoader('mediagoblin', 'templates'))
106
107 return jinja2.ChoiceLoader(path_list)
108
109
110 def get_staticdirector(app_config):
111 # At minimum, we need the direct_remote_path
112 if not 'direct_remote_path' in app_config \
113 or not 'theme_web_path' in app_config:
114 raise ImproperlyConfigured(
115 "direct_remote_path and theme_web_path must be provided")
116
117 direct_domains = {None: app_config['direct_remote_path'].strip()}
118 direct_domains['theme'] = app_config['theme_web_path'].strip()
119
120 # Let plugins load additional paths
121 for plugin_static in hook_runall("static_setup"):
122 direct_domains[plugin_static['name']] = "%s/%s" % (
123 app_config['plugin_web_path'].rstrip('/'),
124 plugin_static['name'])
125
126 return staticdirect.StaticDirect(
127 direct_domains)
128
129
130 def setup_storage():
131 global_config = mg_globals.global_config
132
133 key_short = 'publicstore'
134 key_long = "storage:" + key_short
135 public_store = storage_system_from_config(global_config[key_long])
136
137 key_short = 'queuestore'
138 key_long = "storage:" + key_short
139 queue_store = storage_system_from_config(global_config[key_long])
140
141 setup_globals(
142 public_store=public_store,
143 queue_store=queue_store)
144
145 return public_store, queue_store
146
147
148 def setup_workbench():
149 app_config = mg_globals.app_config
150
151 workbench_manager = WorkbenchManager(app_config['workbench_path'])
152
153 setup_globals(workbench_manager=workbench_manager)