From a67fec8177c09c4e74ce7f4301b88f4e7ea6e658 Mon Sep 17 00:00:00 2001 From: Elrond Date: Thu, 19 May 2011 01:35:02 +0200 Subject: [PATCH] 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 --- mediagoblin/app.py | 12 ++++---- mediagoblin/celery_setup/from_celery.py | 8 ++---- mediagoblin/db/open.py | 37 +++++++++++++++++++++++++ mediagoblin/db/util.py | 13 --------- 4 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 mediagoblin/db/open.py diff --git a/mediagoblin/app.py b/mediagoblin/app.py index 25a6f541..640ffc45 100644 --- a/mediagoblin/app.py +++ b/mediagoblin/app.py @@ -21,8 +21,7 @@ from paste.deploy.converters import asbool from webob import Request, exc from mediagoblin import routing, util, storage, staticdirect -from mediagoblin.db import models -from mediagoblin.db.util import connect_database_from_config +from mediagoblin.db.open import setup_connection_and_db_from_config from mediagoblin.globals import setup_globals from mediagoblin.celery_setup import setup_celery_from_config @@ -35,7 +34,7 @@ class MediaGoblinApp(object): """ Really basic wsgi app using routes and WebOb. """ - def __init__(self, connection, database_path, + def __init__(self, connection, db, public_store, queue_store, staticdirector, email_sender_address, email_debug_mode, @@ -49,8 +48,7 @@ class MediaGoblinApp(object): # Set up database self.connection = connection - self.db = connection[database_path] - models.register_models(connection) + self.db = db # set up routing self.routing = routing.get_mapper() @@ -118,7 +116,7 @@ class MediaGoblinApp(object): def paste_app_factory(global_config, **app_config): # 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. 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) mgoblin_app = MediaGoblinApp( - connection, app_config.get('db_name', 'mediagoblin'), + connection, db, public_store=public_store, queue_store=queue_store, staticdirector=staticdirector, email_sender_address=app_config.get( diff --git a/mediagoblin/celery_setup/from_celery.py b/mediagoblin/celery_setup/from_celery.py index d35009cb..0669e80c 100644 --- a/mediagoblin/celery_setup/from_celery.py +++ b/mediagoblin/celery_setup/from_celery.py @@ -20,8 +20,7 @@ from paste.deploy.loadwsgi import NicerConfigParser from paste.deploy.converters import asbool from mediagoblin import storage -from mediagoblin.db import models -from mediagoblin.db.util import connect_database_from_config +from mediagoblin.db.open import setup_connection_and_db_from_config from mediagoblin.celery_setup import setup_celery_from_config from mediagoblin.globals import setup_globals from mediagoblin import globals as mgoblin_globals @@ -69,10 +68,7 @@ def setup_self(setup_globals_func=setup_globals): settings_module=OUR_MODULENAME, set_environ=False) - connection = connect_database_from_config(mgoblin_section) - - db = connection[mgoblin_section.get('db_name', 'mediagoblin')] - models.register_models(connection) + connection, db = setup_connection_and_db_from_config(mgoblin_section) # Set up the storage systems. public_store = storage.storage_system_from_paste_config( diff --git a/mediagoblin/db/open.py b/mediagoblin/db/open.py new file mode 100644 index 00000000..cae33394 --- /dev/null +++ b/mediagoblin/db/open.py @@ -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 . + +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) diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py index 407caf05..30615fca 100644 --- a/mediagoblin/db/util.py +++ b/mediagoblin/db/util.py @@ -14,20 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import mongokit - -from paste.deploy.converters import asint # Imports that other modules might use from pymongo import DESCENDING 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 -- 2.25.1