Merge branch 'master' of git://gitorious.org/mediagoblin/mediagoblin
[mediagoblin.git] / mediagoblin / init / __init__.py
CommitLineData
1b579e18 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
1b579e18
E
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/>.
42ef819c
E
16
17import jinja2
0533f117 18
e8d4e582 19from mediagoblin.tools import staticdirect
826919c9 20from mediagoblin.tools.translate import set_available_locales
fe289be4
E
21from mediagoblin.init.config import (
22 read_mediagoblin_config, generate_validation_report)
7664b4db 23from mediagoblin import mg_globals
cca5d55d 24from mediagoblin.mg_globals import setup_globals
415077a7 25from mediagoblin.db.open import setup_connection_and_db_from_config, \
b8295953 26 check_db_migrations_current, load_models
626a093c 27from mediagoblin.tools.workbench import WorkbenchManager
dccef262 28from mediagoblin.storage import storage_system_from_config
c85c9dc7
E
29
30
243c3843
NY
31class Error(Exception):
32 pass
33
34
35class ImproperlyConfigured(Error):
36 pass
42ef819c
E
37
38
6ef75af5
SS
39def setup_locales():
40 """Checks which language translations are available and sets them"""
826919c9 41 set_available_locales()
6ef75af5
SS
42
43
fe289be4
E
44def setup_global_and_app_config(config_path):
45 global_config, validation_result = read_mediagoblin_config(config_path)
46 app_config = global_config['mediagoblin']
47 # report errors if necessary
48 validation_report = generate_validation_report(
49 global_config, validation_result)
50 if validation_report:
51 raise ImproperlyConfigured(validation_report)
52
cca5d55d
E
53 setup_globals(
54 app_config=app_config,
55 global_config=global_config)
56
fe289be4
E
57 return global_config, app_config
58
3f4b5e4a
E
59
60def setup_database():
61 app_config = mg_globals.app_config
62
b8295953
E
63 # Load all models for media types (plugins, ...)
64 load_models(app_config)
65
3f4b5e4a 66 # Set up the database
bc142abc 67 db = setup_connection_and_db_from_config(app_config)
3f4b5e4a 68
415077a7 69 check_db_migrations_current(db)
3f4b5e4a 70
bc142abc 71 setup_globals(database=db)
3f4b5e4a 72
bc142abc 73 return db
3f4b5e4a
E
74
75
8545dd50
WKG
76def get_jinja_loader(user_template_path=None, current_theme=None,
77 plugin_template_paths=None):
42ef819c
E
78 """
79 Set up the Jinja template loaders, possibly allowing for user
80 overridden templates.
81
82 (In the future we may have another system for providing theming;
83 for now this is good enough.)
84 """
8545dd50
WKG
85 path_list = []
86
87 # Add user path first--this takes precedence over everything.
88 if user_template_path is not None:
89 path_list.append(jinja2.FileSystemLoader(user_template_path))
90
91 # Any theme directories in the registry
92 if current_theme and current_theme.get('templates_dir'):
93 path_list.append(
94 jinja2.FileSystemLoader(
95 current_theme['templates_dir']))
96
97 # Add plugin template paths next--takes precedence over
98 # core templates.
99 if plugin_template_paths is not None:
100 path_list.extend((jinja2.FileSystemLoader(path)
101 for path in plugin_template_paths))
102
103 # Add core templates last.
104 path_list.append(jinja2.PackageLoader('mediagoblin', 'templates'))
105
106 return jinja2.ChoiceLoader(path_list)
c85c9dc7
E
107
108
109def get_staticdirector(app_config):
9a422c1f
CAW
110 # At minimum, we need the direct_remote_path
111 if not 'direct_remote_path' in app_config \
112 or not 'theme_web_path' in app_config:
c85c9dc7 113 raise ImproperlyConfigured(
9a422c1f
CAW
114 "direct_remote_path and theme_web_path must be provided")
115
116 direct_domains = {None: app_config['direct_remote_path'].strip()}
117 direct_domains['theme'] = app_config['theme_web_path'].strip()
7664b4db 118
5377114c 119 return staticdirect.StaticDirect(
9a422c1f 120 direct_domains)
00eda826 121
7664b4db 122
dccef262 123def setup_storage():
ed797069 124 global_config = mg_globals.global_config
dccef262 125
ed797069
E
126 key_short = 'publicstore'
127 key_long = "storage:" + key_short
56fc7186 128 public_store = storage_system_from_config(global_config[key_long])
ed797069
E
129
130 key_short = 'queuestore'
131 key_long = "storage:" + key_short
56fc7186 132 queue_store = storage_system_from_config(global_config[key_long])
dccef262
E
133
134 setup_globals(
243c3843
NY
135 public_store=public_store,
136 queue_store=queue_store)
dccef262
E
137
138 return public_store, queue_store
139
140
7664b4db
E
141def setup_workbench():
142 app_config = mg_globals.app_config
143
144 workbench_manager = WorkbenchManager(app_config['workbench_path'])
145
243c3843 146 setup_globals(workbench_manager=workbench_manager)