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