Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pymongo/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ def validate_timeout_or_none(option, value):
def validate_read_preference(dummy, value):
"""Validate read preference for a ReplicaSetConnection.
"""
if value not in read_preferences.modes:
if value in read_preferences.modes:
return value

# Also allow string form of enum for uri_parser
try:
return read_preferences.mongos_enum(value)
except ValueError:
raise ConfigurationError("Not a valid read preference")
return value


def validate_tag_sets(dummy, value):
Expand Down Expand Up @@ -157,6 +162,7 @@ def validate_tag_sets(dummy, value):
'connecttimeoutms': validate_timeout_or_none,
'sockettimeoutms': validate_timeout_or_none,
'ssl': validate_boolean,
'readpreference': validate_read_preference,
'read_preference': validate_read_preference,
'tag_sets': validate_tag_sets,
'secondaryacceptablelatencyms': validate_positive_float,
Expand Down Expand Up @@ -253,7 +259,7 @@ def __set_options(self, options):
for option, value in options.iteritems():
if option in ('slave_okay', 'slaveok'):
self.__slave_okay = validate_boolean(option, value)
elif option == 'read_preference':
elif option in ('read_preference', "readpreference"):
self.__read_pref = validate_read_preference(option, value)
elif option == 'tag_sets':
self.__tag_sets = validate_tag_sets(option, value)
Expand Down
3 changes: 3 additions & 0 deletions pymongo/read_preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ class ReadPreference:
def mongos_mode(mode):
return _mongos_modes[mode]

def mongos_enum(enum):
return _mongos_modes.index(enum)


def select_primary(members):
for member in members:
Expand Down
6 changes: 6 additions & 0 deletions test/test_uri_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
split_options,
parse_uri)
from pymongo.errors import ConfigurationError, InvalidURI
from pymongo import ReadPreference


class TestURI(unittest.TestCase):
Expand Down Expand Up @@ -268,5 +269,10 @@ def test_parse_uri(self):
parse_uri("mongodb://fred:foobar@localhost/"
"test.yield_historical.in?slaveok=true"))

res = copy.deepcopy(orig)
res['options'] = {'readpreference': ReadPreference.SECONDARY}
self.assertEqual(res,
parse_uri("mongodb://localhost/?readPreference=secondary"))

if __name__ == "__main__":
unittest.main()