Skip to content

Commit 0e95156

Browse files
committed
Update PyMongo 3.0+ support, remove MongoKit support
1 parent d9d048f commit 0e95156

2 files changed

Lines changed: 7 additions & 45 deletions

File tree

mrq/context.py

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import gevent
33
import gevent.pool
44
import urlparse
5-
import re
65
import time
76
import pymongo
87
from .utils import LazyObject, load_class_by_path
@@ -145,45 +144,19 @@ def versiontuple(v):
145144
elif config_obj == "0":
146145
return None
147146

148-
try:
149-
# MongoKit's Connection object is just a wrapped MongoClient.
150-
from mongokit import Connection as MongoClient # pylint: disable=import-error
151-
from mongokit import ReplicaSetConnection as MongoReplicaSetClient # pylint: disable=import-error
152-
except ImportError:
153-
from pymongo import MongoClient
154-
from pymongo import MongoReplicaSetClient
147+
from pymongo import MongoClient
155148

156-
mongo_parsed = re.match(
157-
r"mongodb://((\w+):(\w+)@)?([\w\.:,-]+)/([\w-]+)(\?.*)?",
158-
config_obj
159-
).groups()
149+
mongo_parsed = pymongo.uri_parser.parse_uri(config_obj)
160150

161-
mongo_hosts = mongo_parsed[3]
162-
mongo_name = mongo_parsed[4]
163-
mongo_options = mongo_parsed[5]
151+
mongo_hosts = mongo_parsed["nodelist"]
152+
mongo_name = mongo_parsed["database"]
164153

165154
log.debug("%s: Connecting to MongoDB at %s/%s..." % (attr, mongo_hosts, mongo_name))
166155

167156
kwargs = {}
168-
options = {}
169-
if mongo_options:
170-
options = {
171-
k: v[0]
172-
for k, v in urlparse.parse_qs(mongo_options[1:]).iteritems()
173-
}
174-
175-
# We automatically switch to MongoReplicaSetClient when getting a replicaSet option.
176-
# This should cover most use-cases before pymongo 3.0.0 which switches automatically.
177-
# http://api.mongodb.org/python/current/examples/high_availability.html#mongoreplicasetclient
178-
if options.get("replicaSet") and versiontuple(pymongo.version) < versiontuple("3.0.0"):
179-
try:
180-
from mongokit import ReplicaSetConnection as MongoReplicaSetClient # pylint: disable=import-error
181-
except ImportError:
182-
from pymongo import MongoReplicaSetClient
183-
184-
db = MongoReplicaSetClient(config_obj, **kwargs)[mongo_name]
185-
else:
186-
db = MongoClient(config_obj, **kwargs)[mongo_name]
157+
158+
db = MongoClient(config_obj, **kwargs)[mongo_name]
159+
187160
log.debug("%s: ... connected." % (attr))
188161

189162
return db

mrq/monkey.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ def mrq_monkey_patched(self, *args, **kwargs):
104104
if hasattr(Collection, method) and getattr(Collection, method).__name__ != "mrq_monkey_patched":
105105
setattr(Collection, method, gen_monkey_patch(Collection, method))
106106

107-
# MongoKit completely replaces the code from PyMongo's find() function, so we
108-
# need to monkey-patch that as well.
109-
try:
110-
from mongokit.collection import Collection as MongoKitCollection # pylint: disable=import-error
111-
for method in ["find"]:
112-
if getattr(MongoKitCollection, method).__name__ != "mrq_monkey_patched":
113-
setattr(MongoKitCollection, method, gen_monkey_patch(MongoKitCollection, method))
114-
115-
except ImportError:
116-
pass
117-
118107

119108
# https://code.google.com/p/gevent/issues/detail?id=108
120109
def patch_import():

0 commit comments

Comments
 (0)