Factor out most of the database connection into db/open.py
[mediagoblin.git] / mediagoblin / celery_setup / from_celery.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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
17 import os
18
19 from paste.deploy.loadwsgi import NicerConfigParser
20 from paste.deploy.converters import asbool
21
22 from mediagoblin import storage
23 from mediagoblin.db.open import setup_connection_and_db_from_config
24 from mediagoblin.celery_setup import setup_celery_from_config
25 from mediagoblin.globals import setup_globals
26 from mediagoblin import globals as mgoblin_globals
27
28
29 OUR_MODULENAME = 'mediagoblin.celery_setup.from_celery'
30
31
32 def setup_self(setup_globals_func=setup_globals):
33 """
34 Transform this module into a celery config module by reading the
35 mediagoblin config file. Set the environment variable
36 MEDIAGOBLIN_CONFIG to specify where this config file is at and
37 what section it uses.
38
39 By default it defaults to 'mediagoblin.ini:app:mediagoblin'.
40
41 The first colon ":" is a delimiter between the filename and the
42 config section, so in this case the filename is 'mediagoblin.ini'
43 and the section where mediagoblin is defined is 'app:mediagoblin'.
44
45 Args:
46 - 'setup_globals_func': this is for testing purposes only. Don't
47 set this!
48 """
49 mgoblin_conf_file, mgoblin_section = os.environ.get(
50 'MEDIAGOBLIN_CONFIG', 'mediagoblin.ini:app:mediagoblin').split(':', 1)
51 if not os.path.exists(mgoblin_conf_file):
52 raise IOError(
53 "MEDIAGOBLIN_CONFIG not set or file does not exist")
54
55 parser = NicerConfigParser(mgoblin_conf_file)
56 parser.read(mgoblin_conf_file)
57 parser._defaults.setdefault(
58 'here', os.path.dirname(os.path.abspath(mgoblin_conf_file)))
59 parser._defaults.setdefault(
60 '__file__', os.path.abspath(mgoblin_conf_file))
61
62 mgoblin_section = dict(parser.items(mgoblin_section))
63 mgoblin_conf = dict(
64 [(section_name, dict(parser.items(section_name)))
65 for section_name in parser.sections()])
66 setup_celery_from_config(
67 mgoblin_section, mgoblin_conf,
68 settings_module=OUR_MODULENAME,
69 set_environ=False)
70
71 connection, db = setup_connection_and_db_from_config(mgoblin_section)
72
73 # Set up the storage systems.
74 public_store = storage.storage_system_from_paste_config(
75 mgoblin_section, 'publicstore')
76 queue_store = storage.storage_system_from_paste_config(
77 mgoblin_section, 'queuestore')
78
79 setup_globals_func(
80 db_connection=connection,
81 database=db,
82 public_store=public_store,
83 email_debug_mode=asbool(mgoblin_section.get('email_debug_mode')),
84 email_sender_address=mgoblin_section.get(
85 'email_sender_address',
86 'notice@mediagoblin.example.org'),
87 queue_store=queue_store)
88
89
90 if os.environ['CELERY_CONFIG_MODULE'] == OUR_MODULENAME:
91 setup_self()