Themes are now registered and can have their templates loaded properly
[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 from beaker.cache import CacheManager
18 from beaker.util import parse_cache_config_options
19 import jinja2
20
21 from mediagoblin import staticdirect
22 from mediagoblin.init.config import (
23 read_mediagoblin_config, generate_validation_report)
24 from mediagoblin import mg_globals
25 from mediagoblin.mg_globals import setup_globals
26 from mediagoblin.db.open import setup_connection_and_db_from_config, \
27 check_db_migrations_current, load_models
28 from mediagoblin.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_global_and_app_config(config_path):
41 global_config, validation_result = read_mediagoblin_config(config_path)
42 app_config = global_config['mediagoblin']
43 # report errors if necessary
44 validation_report = generate_validation_report(
45 global_config, validation_result)
46 if validation_report:
47 raise ImproperlyConfigured(validation_report)
48
49 setup_globals(
50 app_config=app_config,
51 global_config=global_config)
52
53 return global_config, app_config
54
55
56 def setup_database():
57 app_config = mg_globals.app_config
58
59 # Load all models for media types (plugins, ...)
60 load_models(app_config)
61
62 # Set up the database
63 connection, db = setup_connection_and_db_from_config(app_config)
64
65 check_db_migrations_current(db)
66
67 setup_globals(
68 db_connection=connection,
69 database=db)
70
71 return connection, db
72
73
74 def get_jinja_loader(user_template_path=None, current_theme=None):
75 """
76 Set up the Jinja template loaders, possibly allowing for user
77 overridden templates.
78
79 (In the future we may have another system for providing theming;
80 for now this is good enough.)
81 """
82 if user_template_path or current_theme:
83 loader_choices = []
84
85 # user template overrides
86 if user_template_path:
87 loader_choices.append(jinja2.FileSystemLoader(user_template_path))
88
89 # Any theme directories in the registry
90 if current_theme and current_theme.get('templates_dir'):
91 loader_choices.append(
92 jinja2.FileSystemLoader(
93 current_theme['templates_dir']))
94
95 # Add the main mediagoblin templates
96 loader_choices.append(
97 jinja2.PackageLoader('mediagoblin', 'templates'))
98
99 return jinja2.ChoiceLoader(loader_choices)
100 else:
101 return jinja2.PackageLoader('mediagoblin', 'templates')
102
103
104 def get_staticdirector(app_config):
105 if 'direct_remote_path' in app_config:
106 return staticdirect.RemoteStaticDirect(
107 app_config['direct_remote_path'].strip())
108 elif 'direct_remote_paths' in app_config:
109 direct_remote_path_lines = app_config[
110 'direct_remote_paths'].strip().splitlines()
111 return staticdirect.MultiRemoteStaticDirect(
112 dict([line.strip().split(' ', 1)
113 for line in direct_remote_path_lines]))
114 else:
115 raise ImproperlyConfigured(
116 "One of direct_remote_path or "
117 "direct_remote_paths must be provided")
118
119
120 def setup_storage():
121 global_config = mg_globals.global_config
122
123 key_short = 'publicstore'
124 key_long = "storage:" + key_short
125 public_store = storage_system_from_config(global_config[key_long])
126
127 key_short = 'queuestore'
128 key_long = "storage:" + key_short
129 queue_store = storage_system_from_config(global_config[key_long])
130
131 setup_globals(
132 public_store=public_store,
133 queue_store=queue_store)
134
135 return public_store, queue_store
136
137
138 def setup_workbench():
139 app_config = mg_globals.app_config
140
141 workbench_manager = WorkbenchManager(app_config['workbench_path'])
142
143 setup_globals(workbench_manager=workbench_manager)
144
145
146 def setup_beaker_cache():
147 """
148 Setup the Beaker Cache manager.
149 """
150 cache_config = mg_globals.global_config['beaker.cache']
151 cache_config = dict(
152 [(u'cache.%s' % key, value)
153 for key, value in cache_config.iteritems()])
154 cache = CacheManager(**parse_cache_config_options(cache_config))
155 setup_globals(cache=cache)
156 return cache