1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 from beaker
.cache
import CacheManager
18 from beaker
.util
import parse_cache_config_options
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
28 from mediagoblin
.workbench
import WorkbenchManager
29 from mediagoblin
.storage
import storage_system_from_config
32 class Error(Exception):
36 class ImproperlyConfigured(Error
):
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
)
47 raise ImproperlyConfigured(validation_report
)
50 app_config
=app_config
,
51 global_config
=global_config
)
53 return global_config
, app_config
57 app_config
= mg_globals
.app_config
60 connection
, db
= setup_connection_and_db_from_config(app_config
)
62 check_db_migrations_current(db
)
65 db_connection
=connection
,
71 def get_jinja_loader(user_template_path
=None):
73 Set up the Jinja template loaders, possibly allowing for user
76 (In the future we may have another system for providing theming;
77 for now this is good enough.)
79 if user_template_path
:
80 return jinja2
.ChoiceLoader(
81 [jinja2
.FileSystemLoader(user_template_path
),
82 jinja2
.PackageLoader('mediagoblin', 'templates')])
84 return jinja2
.PackageLoader('mediagoblin', 'templates')
87 def 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
]))
98 raise ImproperlyConfigured(
99 "One of direct_remote_path or "
100 "direct_remote_paths must be provided")
104 global_config
= mg_globals
.global_config
106 key_short
= 'publicstore'
107 key_long
= "storage:" + key_short
108 public_store
= storage_system_from_config(global_config
[key_long
])
110 key_short
= 'queuestore'
111 key_long
= "storage:" + key_short
112 queue_store
= storage_system_from_config(global_config
[key_long
])
115 public_store
=public_store
,
116 queue_store
=queue_store
)
118 return public_store
, queue_store
121 def setup_workbench():
122 app_config
= mg_globals
.app_config
124 workbench_manager
= WorkbenchManager(app_config
['workbench_path'])
126 setup_globals(workbench_manager
=workbench_manager
)
129 def setup_beaker_cache():
131 Setup the Beaker Cache manager.
133 cache_config
= mg_globals
.global_config
['beaker.cache']
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
)