Skip to content

Commit a8314bf

Browse files
committed
Support w=<basestring> for tagging PYTHON-268.
See http://www.mongodb.org/display/DOCS/Data+Center+Awareness for a full explanation.
1 parent 2557752 commit a8314bf

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

pymongo/common.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def validate_boolean(option, value):
3838

3939

4040
def validate_integer(option, value):
41-
"""Validates that 'value' is an integer.
41+
"""Validates that 'value' is an integer (or basestring representation).
4242
"""
4343
if isinstance(value, (int, long)):
4444
return value
@@ -60,13 +60,26 @@ def validate_basestring(option, value):
6060
"instance of basestring" % (option,))
6161

6262

63+
def validate_int_or_basestring(option, value):
64+
"""Validates that 'value' is an integer or string.
65+
"""
66+
if isinstance(value, (int, long)):
67+
return value
68+
elif isinstance(value, basestring):
69+
if value.isdigit():
70+
return int(value)
71+
return value
72+
raise TypeError("Wrong type for %s, value just be an "
73+
"integer or a string" % (option,))
74+
75+
6376
# jounal is an alias for j,
6477
# wtimeoutms is an alias for wtimeout
6578
VALIDATORS = {
6679
'replicaset': validate_basestring,
6780
'slaveok': validate_boolean,
6881
'safe': validate_boolean,
69-
'w': validate_integer,
82+
'w': validate_int_or_basestring,
7083
'wtimeout': validate_integer,
7184
'wtimeoutms': validate_integer,
7285
'fsync': validate_boolean,

pymongo/connection.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,9 @@ def __init__(self, host=None, port=None, max_pool_size=10,
221221
- `j` or `journal`: Block until write operations have been commited
222222
to the journal. Ignored if the server is running without journaling.
223223
Implies safe=True.
224-
- `w`: If this is a replica set the server won't return until
225-
write operations have replicated to this many set members.
224+
- `w`: (integer or string) If this is a replica set write operations
225+
won't return until they have been replicated to the specified
226+
number or tagged set of servers.
226227
Implies safe=True.
227228
- `wtimeout`: Used in conjunction with `j` and/or `w`. Wait this many
228229
milliseconds for journal acknowledgement and/or write replication.
@@ -237,6 +238,8 @@ def __init__(self, host=None, port=None, max_pool_size=10,
237238
attempt to find all members of the set.
238239
239240
.. seealso:: :meth:`end_request`
241+
.. versionchanged:: 2.0.1+
242+
Support `w` = integer or string.
240243
.. versionchanged:: 2.0
241244
`slave_okay` is a pure keyword argument. Added support for safe,
242245
and getlasterror options as keyword arguments.

test/test_common.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ def test_baseobject(self):
4141
cursor = coll.find(slave_okay=True)
4242
self.assertTrue(cursor._Cursor__slave_okay)
4343

44-
c = Connection(slaveok=True, w=1, wtimeout=300, fsync=True, j=True)
44+
c = Connection(slaveok=True, w='majority',
45+
wtimeout=300, fsync=True, j=True)
4546
self.assertTrue(c.slave_okay)
4647
self.assertTrue(c.safe)
47-
d = {'w': 1, 'wtimeout': 300, 'fsync': True, 'j': True}
48+
d = {'w': 'majority', 'wtimeout': 300, 'fsync': True, 'j': True}
4849
self.assertEqual(d, c.get_lasterror_options())
4950
db = c.test
5051
self.assertTrue(db.slave_okay)
@@ -112,9 +113,9 @@ def test_baseobject(self):
112113
self.assertEqual({}, c.get_lasterror_options())
113114
self.assertFalse(c.safe)
114115

115-
db.set_lasterror_options(w=1)
116+
db.set_lasterror_options(w='majority')
116117
self.assertEqual({'j': True}, coll.get_lasterror_options())
117-
self.assertEqual({'w': 1}, db.get_lasterror_options())
118+
self.assertEqual({'w': 'majority'}, db.get_lasterror_options())
118119
self.assertEqual({}, c.get_lasterror_options())
119120
self.assertFalse(c.safe)
120121
db.slave_okay = True

test/test_uri_parser.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ def test_split_options(self):
8181
self.assertRaises(ConfigurationError, split_options, 'foo')
8282
self.assertRaises(ConfigurationError, split_options, 'foo=bar')
8383
self.assertRaises(ConfigurationError, split_options, 'foo=bar;foo')
84-
self.assertRaises(ConfigurationError, split_options, 'w=foo')
85-
self.assertRaises(ConfigurationError, split_options, 'w=5.5')
86-
self.assert_(split_options, 'w=5')
84+
self.assertTrue(isinstance(split_options('w=5')['w'], int))
85+
self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring))
86+
self.assert_(split_options, 'w=foo')
87+
self.assert_(split_options, 'w=majority')
8788
self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo')
8889
self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=5.5')
8990
self.assert_(split_options, 'wtimeoutms=500')

0 commit comments

Comments
 (0)