Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 23cd40c

Browse files
committedJun 13, 2013
Adding replica set support for mongokit
ReplicaSetConnection added Documentation, changelog updated
1 parent 6400a76 commit 23cd40c

File tree

5 files changed

+99
-4
lines changed

5 files changed

+99
-4
lines changed
 

‎AUTHORS

+2
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ Great contributors:
2222
Ozgur <hinoglu **at** gmail.com>
2323
* patch to fix atomic save issue
2424

25+
Liyan Chang <liyan@filepicker.io>
26+
* Replica Set Support
2527

‎CHANGELOG

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Change Log
22
==========
33

4+
5+
To Be Released:
6+
-----
7+
* use MongoClient with MasterSlaveConnection
8+
9+
410
v0.8.3
511
------
612

‎doc/tutorial.txt

+18-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,22 @@ Now, to open a connection to MongoDB::
5555

5656
connection = Connection()
5757

58-
This a wrapped version of pymongo's Connection_ and will attempt to connect to
59-
a MongoDB instance running locally. For more information on how to configure
60-
the Connection, see pymongo_'s documentation.
58+
This a wrapped version of pymongo's Connection_ and will attempt to connect to a MongoDB instance running locally.
59+
60+
e.g. Speficying a host
61+
62+
connection = Connection(host="HOSTNAME", port=PORT)
63+
64+
e.g. Specifiying a Replica Set host
65+
66+
from mongokit import ReplicaSetConnection
67+
68+
connection = ReplicaSetConnection(
69+
host="HOSTNAME:PORT,HOSTNAME:PORT"
70+
replicaset="REPLICA_SET_NAME",
71+
read_preferences=pymongo.read_preferences.ReadPreferance.SECONDARY_PREFERRED)
72+
73+
For more information on how to configure the Connection, see pymongo_'s documentation.
6174

6275
Once you have an active connection, you need to register your the ``BlogPost``
6376
object::
@@ -140,4 +153,5 @@ as well::
140153
my first blog post
141154

142155
pymongo_ makes it very easy to perform complex queries on your data so for more
143-
information, see the :doc:`query` documentation.
156+
information, see the :doc:`query` documentation.
157+

‎mongokit/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from collection import Collection
3939
from connection import Connection, MongoClient
4040
from master_slave_connection import MasterSlaveConnection
41+
from replica_set_connection import ReplicaSetConnection
4142
from pymongo import ASCENDING as INDEX_ASCENDING,\
4243
DESCENDING as INDEX_DESCENDING,\
4344
ALL as INDEX_ALL,\

‎mongokit/replica_set_connection.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
Replica Set integration with for MongoKit
3+
Liyan Chang, liyan@filepicker.io
4+
(same license as Mongokit)
5+
"""
6+
7+
from pymongo.replica_set_connection import ReplicaSetConnection as PymongoReplicaSetConnection
8+
from mongokit.database import Database
9+
from mongokit.connection import CallableMixin, _iterables
10+
11+
class ReplicaSetConnection(PymongoReplicaSetConnection):
12+
""" Replica Set support for MongoKit """
13+
14+
def __init__(self, *args, **kwargs):
15+
""" The ReplicaSetConnection is a wrapper around the
16+
pymongo.replica_set_connection implementation.
17+
"""
18+
19+
self._databases = {}
20+
self._registered_documents = {}
21+
22+
super(ReplicaSetConnection, self).__init__(*args, **kwargs)
23+
24+
"""
25+
All following code is duplicate code copied from connection.py
26+
Any edit to this code should probably be propagated over to
27+
connection.py as well as master_slave_connection.py
28+
"""
29+
30+
def register(self, obj_list):
31+
decorator = None
32+
if not isinstance(obj_list, _iterables):
33+
# we assume that the user used this as a decorator
34+
# using @register syntax or using conn.register(SomeDoc)
35+
# we stock the class object in order to return it later
36+
decorator = obj_list
37+
obj_list = [obj_list]
38+
# cleanup
39+
for dbname, db in self._databases.items():
40+
for colname, col in db._collections.items():
41+
for docname, doc in col._documents.items():
42+
del col._documents[docname]
43+
for obj_name in [obj.__name__ for obj in obj_list]:
44+
if obj_name in col._registered_documents:
45+
del col._registered_documents[obj_name]
46+
# register
47+
for obj in obj_list:
48+
CallableDocument = type(
49+
"Callable%s" % obj.__name__,
50+
(obj, CallableMixin),
51+
{"_obj_class":obj, "__repr__":object.__repr__}
52+
)
53+
self._registered_documents[obj.__name__] = CallableDocument
54+
# if the class object is stored, it means the user used a decorator and
55+
# we must return the class object
56+
if decorator is not None:
57+
return decorator
58+
59+
def __getattr__(self, key):
60+
if key in self._registered_documents:
61+
document = self._registered_documents[key]
62+
try:
63+
return getattr(self[document.__database__][document.__collection__], key)
64+
except AttributeError:
65+
raise AttributeError("%s: __collection__ attribute not found. "
66+
"You cannot specify the `__database__` attribute without "
67+
"the `__collection__` attribute" % key)
68+
else:
69+
if key not in self._databases:
70+
self._databases[key] = Database(self, key)
71+
return self._databases[key]
72+

0 commit comments

Comments
 (0)
This repository has been archived.