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