Factor out most of the database connection into db/open.py

I needed to split the db connection/opening into open.py,
due to an import loop:
- util.py needs db/util.py:ObjectId
- db/util.py would need db/models.py
- db/models.py needs util.py:slugify
This commit is contained in:
Elrond 2011-05-19 01:35:02 +02:00
parent 86f9b47387
commit a67fec8177
4 changed files with 44 additions and 26 deletions

View File

@ -21,8 +21,7 @@ from paste.deploy.converters import asbool
from webob import Request, exc from webob import Request, exc
from mediagoblin import routing, util, storage, staticdirect from mediagoblin import routing, util, storage, staticdirect
from mediagoblin.db import models from mediagoblin.db.open import setup_connection_and_db_from_config
from mediagoblin.db.util import connect_database_from_config
from mediagoblin.globals import setup_globals from mediagoblin.globals import setup_globals
from mediagoblin.celery_setup import setup_celery_from_config from mediagoblin.celery_setup import setup_celery_from_config
@ -35,7 +34,7 @@ class MediaGoblinApp(object):
""" """
Really basic wsgi app using routes and WebOb. Really basic wsgi app using routes and WebOb.
""" """
def __init__(self, connection, database_path, def __init__(self, connection, db,
public_store, queue_store, public_store, queue_store,
staticdirector, staticdirector,
email_sender_address, email_debug_mode, email_sender_address, email_debug_mode,
@ -49,8 +48,7 @@ class MediaGoblinApp(object):
# Set up database # Set up database
self.connection = connection self.connection = connection
self.db = connection[database_path] self.db = db
models.register_models(connection)
# set up routing # set up routing
self.routing = routing.get_mapper() self.routing = routing.get_mapper()
@ -118,7 +116,7 @@ class MediaGoblinApp(object):
def paste_app_factory(global_config, **app_config): def paste_app_factory(global_config, **app_config):
# Get the database connection # Get the database connection
connection = connect_database_from_config(app_config) connection, db = setup_connection_and_db_from_config(app_config)
# Set up the storage systems. # Set up the storage systems.
public_store = storage.storage_system_from_paste_config( public_store = storage.storage_system_from_paste_config(
@ -143,7 +141,7 @@ def paste_app_factory(global_config, **app_config):
setup_celery_from_config(app_config, global_config) setup_celery_from_config(app_config, global_config)
mgoblin_app = MediaGoblinApp( mgoblin_app = MediaGoblinApp(
connection, app_config.get('db_name', 'mediagoblin'), connection, db,
public_store=public_store, queue_store=queue_store, public_store=public_store, queue_store=queue_store,
staticdirector=staticdirector, staticdirector=staticdirector,
email_sender_address=app_config.get( email_sender_address=app_config.get(

View File

@ -20,8 +20,7 @@ from paste.deploy.loadwsgi import NicerConfigParser
from paste.deploy.converters import asbool from paste.deploy.converters import asbool
from mediagoblin import storage from mediagoblin import storage
from mediagoblin.db import models from mediagoblin.db.open import setup_connection_and_db_from_config
from mediagoblin.db.util import connect_database_from_config
from mediagoblin.celery_setup import setup_celery_from_config from mediagoblin.celery_setup import setup_celery_from_config
from mediagoblin.globals import setup_globals from mediagoblin.globals import setup_globals
from mediagoblin import globals as mgoblin_globals from mediagoblin import globals as mgoblin_globals
@ -69,10 +68,7 @@ def setup_self(setup_globals_func=setup_globals):
settings_module=OUR_MODULENAME, settings_module=OUR_MODULENAME,
set_environ=False) set_environ=False)
connection = connect_database_from_config(mgoblin_section) connection, db = setup_connection_and_db_from_config(mgoblin_section)
db = connection[mgoblin_section.get('db_name', 'mediagoblin')]
models.register_models(connection)
# Set up the storage systems. # Set up the storage systems.
public_store = storage.storage_system_from_paste_config( public_store = storage.storage_system_from_paste_config(

37
mediagoblin/db/open.py Normal file
View File

@ -0,0 +1,37 @@
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011 Free Software Foundation, Inc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import mongokit
from paste.deploy.converters import asint
from mediagoblin.db import models
def connect_database_from_config(app_config):
"""Connect to the main database, take config from app_config"""
port = app_config.get('db_port')
if port:
port = asint(port)
connection = mongokit.Connection(
app_config.get('db_host'), port)
return connection
def setup_connection_and_db_from_config(app_config):
connection = connect_database_from_config(app_config)
database_path = app_config.get('db_name', 'mediagoblin')
db = connection[database_path]
models.register_models(connection)
# Could configure indexes here on db
return (connection, db)

View File

@ -14,20 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import mongokit
from paste.deploy.converters import asint
# Imports that other modules might use # Imports that other modules might use
from pymongo import DESCENDING from pymongo import DESCENDING
from mongokit import ObjectId from mongokit import ObjectId
def connect_database_from_config(app_config):
"""Connect to the main database, take config from app_config"""
port = app_config.get('db_port')
if port:
port = asint(port)
connection = mongokit.Connection(
app_config.get('db_host'), port)
return connection