Added CollectionManager, made connection module

All connection-related functions are now in connection.py.

Created a ConnectionManager class for interacting with a collection
in the database. Top-level document classes have an instance of
a ConnectionManager (Document.collection).

Defined a 'save' method on top-level document's that uses the collection
manager's '_save_document' method to save the document to the database.

Added tests for CollectionManagers -- all unit tests now require a valid
connection to the database, which is set up in the tests' setUp method.
This commit is contained in:
Harry Marr
2009-11-18 19:02:57 +00:00
parent 688fd5af66
commit c99f5c4ec1
9 changed files with 173 additions and 36 deletions

View File

@@ -2,39 +2,11 @@ import document
from document import *
import fields
from fields import *
import connection
from connection import *
from pymongo import Connection
__all__ = document.__all__ + fields.__all__ + ['connect']
__all__ = document.__all__ + fields.__all__ + connection.__all__
__author__ = 'Harry Marr'
__version__ = '0.1'
_connection_settings = {
'host': 'localhost',
'port': 27017,
'pool_size': 1,
}
_connection = None
_db = None
def _get_connection():
if _connection is None:
_connection = Connection(**_connection_settings)
return _connection
def connect(db=None, username=None, password=None, **kwargs):
"""Connect to the database specified by the 'db' argument. Connection
settings may be provided here as well if the database is not running on
the default port on localhost. If authentication is needed, provide
username and password arguments as well.
"""
if db is None:
raise TypeError('"db" argument must be provided to connect()')
_connection_settings.update(kwargs)
connection = _get_connection()
# Get DB from connection and auth if necessary
_db = connection[db]
if username is not None and password is not None:
_db.authenticate(username, password)