Import "Base" from base instead of models.
[mediagoblin.git] / mediagoblin / init / __init__.py
... / ...
CommitLineData
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
17from beaker.cache import CacheManager
18from beaker.util import parse_cache_config_options
19import jinja2
20
21from mediagoblin import staticdirect
22from mediagoblin.init.config import (
23 read_mediagoblin_config, generate_validation_report)
24from mediagoblin import mg_globals
25from mediagoblin.mg_globals import setup_globals
26from mediagoblin.db.open import setup_connection_and_db_from_config, \
27 check_db_migrations_current
28from mediagoblin.workbench import WorkbenchManager
29from mediagoblin.storage import storage_system_from_config
30
31
32class Error(Exception):
33 pass
34
35
36class ImproperlyConfigured(Error):
37 pass
38
39
40def 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
56def setup_database():
57 app_config = mg_globals.app_config
58
59 # Set up the database
60 connection, db = setup_connection_and_db_from_config(app_config)
61
62 check_db_migrations_current(db)
63
64 setup_globals(
65 db_connection=connection,
66 database=db)
67
68 return connection, db
69
70
71def get_jinja_loader(user_template_path=None):
72 """
73 Set up the Jinja template loaders, possibly allowing for user
74 overridden templates.
75
76 (In the future we may have another system for providing theming;
77 for now this is good enough.)
78 """
79 if user_template_path:
80 return jinja2.ChoiceLoader(
81 [jinja2.FileSystemLoader(user_template_path),
82 jinja2.PackageLoader('mediagoblin', 'templates')])
83 else:
84 return jinja2.PackageLoader('mediagoblin', 'templates')
85
86
87def get_staticdirector(app_config):
88 if 'direct_remote_path' in app_config:
89 return staticdirect.RemoteStaticDirect(
90 app_config['direct_remote_path'].strip())
91 elif 'direct_remote_paths' in app_config:
92 direct_remote_path_lines = app_config[
93 'direct_remote_paths'].strip().splitlines()
94 return staticdirect.MultiRemoteStaticDirect(
95 dict([line.strip().split(' ', 1)
96 for line in direct_remote_path_lines]))
97 else:
98 raise ImproperlyConfigured(
99 "One of direct_remote_path or "
100 "direct_remote_paths must be provided")
101
102
103def setup_storage():
104 global_config = mg_globals.global_config
105
106 key_short = 'publicstore'
107 key_long = "storage:" + key_short
108 public_store = storage_system_from_config(global_config[key_long])
109
110 key_short = 'queuestore'
111 key_long = "storage:" + key_short
112 queue_store = storage_system_from_config(global_config[key_long])
113
114 setup_globals(
115 public_store=public_store,
116 queue_store=queue_store)
117
118 return public_store, queue_store
119
120
121def setup_workbench():
122 app_config = mg_globals.app_config
123
124 workbench_manager = WorkbenchManager(app_config['workbench_path'])
125
126 setup_globals(workbench_manager=workbench_manager)
127
128
129def setup_beaker_cache():
130 """
131 Setup the Beaker Cache manager.
132 """
133 cache_config = mg_globals.global_config['beaker.cache']
134 cache_config = dict(
135 [(u'cache.%s' % key, value)
136 for key, value in cache_config.iteritems()])
137 cache = CacheManager(**parse_cache_config_options(cache_config))
138 setup_globals(cache=cache)
139 return cache