It's 2012 all up in here
[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
c85c9dc7 21from mediagoblin import staticdirect
fe289be4
E
22from mediagoblin.init.config import (
23 read_mediagoblin_config, generate_validation_report)
7664b4db 24from mediagoblin import mg_globals
cca5d55d 25from mediagoblin.mg_globals import setup_globals
415077a7
E
26from mediagoblin.db.open import setup_connection_and_db_from_config, \
27 check_db_migrations_current
7664b4db 28from mediagoblin.workbench import WorkbenchManager
dccef262 29from mediagoblin.storage import storage_system_from_config
c85c9dc7
E
30
31
243c3843
NY
32class Error(Exception):
33 pass
34
35
36class ImproperlyConfigured(Error):
37 pass
42ef819c
E
38
39
fe289be4
E
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
cca5d55d
E
49 setup_globals(
50 app_config=app_config,
51 global_config=global_config)
52
fe289be4
E
53 return global_config, app_config
54
3f4b5e4a
E
55
56def setup_database():
57 app_config = mg_globals.app_config
58
3f4b5e4a
E
59 # Set up the database
60 connection, db = setup_connection_and_db_from_config(app_config)
61
415077a7 62 check_db_migrations_current(db)
3f4b5e4a
E
63
64 setup_globals(
243c3843
NY
65 db_connection=connection,
66 database=db)
3f4b5e4a
E
67
68 return connection, db
69
70
42ef819c
E
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')
c85c9dc7
E
85
86
87def get_staticdirector(app_config):
285ffedd 88 if 'direct_remote_path' in app_config:
c85c9dc7
E
89 return staticdirect.RemoteStaticDirect(
90 app_config['direct_remote_path'].strip())
285ffedd 91 elif 'direct_remote_paths' in app_config:
c85c9dc7
E
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")
7664b4db
E
101
102
dccef262 103def setup_storage():
ed797069 104 global_config = mg_globals.global_config
dccef262 105
ed797069
E
106 key_short = 'publicstore'
107 key_long = "storage:" + key_short
56fc7186 108 public_store = storage_system_from_config(global_config[key_long])
ed797069
E
109
110 key_short = 'queuestore'
111 key_long = "storage:" + key_short
56fc7186 112 queue_store = storage_system_from_config(global_config[key_long])
dccef262
E
113
114 setup_globals(
243c3843
NY
115 public_store=public_store,
116 queue_store=queue_store)
dccef262
E
117
118 return public_store, queue_store
119
120
7664b4db
E
121def setup_workbench():
122 app_config = mg_globals.app_config
123
124 workbench_manager = WorkbenchManager(app_config['workbench_path'])
125
243c3843 126 setup_globals(workbench_manager=workbench_manager)
0533f117
CAW
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