26
26
ConfigurationError ,
27
27
InvalidName ,
28
28
OperationFailure )
29
- from pymongo import read_preferences as rp
29
+ from pymongo .read_preferences import (modes ,
30
+ secondary_ok_commands ,
31
+ ReadPreference )
30
32
31
33
32
34
def _check_name (name ):
@@ -282,7 +284,7 @@ def _command(self, command, value=1,
282
284
283
285
command_name = command .keys ()[0 ].lower ()
284
286
must_use_master = kwargs .pop ('_use_master' , False )
285
- if command_name not in rp . secondary_ok_commands :
287
+ if command_name not in secondary_ok_commands :
286
288
must_use_master = True
287
289
288
290
# Special-case: mapreduce can go to secondaries only if inline
@@ -323,13 +325,13 @@ def _command(self, command, value=1,
323
325
command .update (kwargs )
324
326
325
327
# Warn if must_use_master will override read_preference.
326
- if (extra_opts ['read_preference' ] != rp . ReadPreference .PRIMARY and
328
+ if (extra_opts ['read_preference' ] != ReadPreference .PRIMARY and
327
329
extra_opts ['_must_use_master' ]):
328
330
warnings .warn ("%s does not support %s read preference "
329
331
"and will be routed to the primary instead." %
330
332
(command_name ,
331
- rp . modes [extra_opts ['read_preference' ]]),
332
- UserWarning )
333
+ modes [extra_opts ['read_preference' ]]),
334
+ UserWarning , stacklevel = 3 )
333
335
334
336
cursor = self ["$cmd" ].find (command , ** extra_opts ).limit (- 1 )
335
337
for doc in cursor :
@@ -466,7 +468,8 @@ def drop_collection(self, name_or_collection):
466
468
467
469
self .__connection ._purge_index (self .__name , name )
468
470
469
- self .command ("drop" , unicode (name ), allowable_errors = ["ns not found" ])
471
+ self .command ("drop" , unicode (name ), allowable_errors = ["ns not found" ],
472
+ read_preference = ReadPreference .PRIMARY )
470
473
471
474
def validate_collection (self , name_or_collection ,
472
475
scandata = False , full = False ):
@@ -504,7 +507,8 @@ def validate_collection(self, name_or_collection,
504
507
"%s or Collection" % (basestring .__name__ ,))
505
508
506
509
result = self .command ("validate" , unicode (name ),
507
- scandata = scandata , full = full )
510
+ scandata = scandata , full = full ,
511
+ read_preference = ReadPreference .PRIMARY )
508
512
509
513
valid = True
510
514
# Pre 1.9 results
@@ -553,7 +557,8 @@ def profiling_level(self):
553
557
554
558
.. mongodoc:: profiling
555
559
"""
556
- result = self .command ("profile" , - 1 )
560
+ result = self .command ("profile" , - 1 ,
561
+ read_preference = ReadPreference .PRIMARY )
557
562
558
563
assert result ["was" ] >= 0 and result ["was" ] <= 2
559
564
return result ["was" ]
@@ -593,9 +598,11 @@ def set_profiling_level(self, level, slow_ms=None):
593
598
raise TypeError ("slow_ms must be an integer" )
594
599
595
600
if slow_ms is not None :
596
- self .command ("profile" , level , slowms = slow_ms )
601
+ self .command ("profile" , level , slowms = slow_ms ,
602
+ read_preference = ReadPreference .PRIMARY )
597
603
else :
598
- self .command ("profile" , level )
604
+ self .command ("profile" , level ,
605
+ read_preference = ReadPreference .PRIMARY )
599
606
600
607
def profiling_info (self ):
601
608
"""Returns a list containing current profiling information.
@@ -610,7 +617,8 @@ def error(self):
610
617
Return None if the last operation was error-free. Otherwise return the
611
618
error that occurred.
612
619
"""
613
- error = self .command ("getlasterror" )
620
+ error = self .command ("getlasterror" ,
621
+ read_preference = ReadPreference .PRIMARY )
614
622
error_msg = error .get ("err" , "" )
615
623
if error_msg is None :
616
624
return None
@@ -623,7 +631,8 @@ def last_status(self):
623
631
624
632
Returns a SON object with status information.
625
633
"""
626
- return self .command ("getlasterror" )
634
+ return self .command ("getlasterror" ,
635
+ read_preference = ReadPreference .PRIMARY )
627
636
628
637
def previous_error (self ):
629
638
"""Get the most recent error to have occurred on this database.
@@ -632,7 +641,8 @@ def previous_error(self):
632
641
`Database.reset_error_history`. Returns None if no such errors have
633
642
occurred.
634
643
"""
635
- error = self .command ("getpreverror" )
644
+ error = self .command ("getpreverror" ,
645
+ read_preference = ReadPreference .PRIMARY )
636
646
if error .get ("err" , 0 ) is None :
637
647
return None
638
648
return error
@@ -643,7 +653,8 @@ def reset_error_history(self):
643
653
Calls to `Database.previous_error` will only return errors that have
644
654
occurred since the most recent call to this method.
645
655
"""
646
- self .command ("reseterror" )
656
+ self .command ("reseterror" ,
657
+ read_preference = ReadPreference .PRIMARY )
647
658
648
659
def __iter__ (self ):
649
660
return self
@@ -697,7 +708,8 @@ def _create_or_update_user(
697
708
else :
698
709
command_name = "updateUser"
699
710
700
- self .command (command_name , name , ** opts )
711
+ self .command (command_name , name ,
712
+ read_preference = ReadPreference .PRIMARY , ** opts )
701
713
702
714
def _legacy_add_user (self , name , password , read_only , ** kwargs ):
703
715
"""Uses v1 system to add users, i.e. saving to system.users.
@@ -763,7 +775,8 @@ def add_user(self, name, password=None, read_only=None, **kwargs):
763
775
"read_only and roles together" )
764
776
765
777
try :
766
- uinfo = self .command ("usersInfo" , name )
778
+ uinfo = self .command ("usersInfo" , name ,
779
+ read_preference = ReadPreference .PRIMARY )
767
780
except OperationFailure , exc :
768
781
# MongoDB >= 2.5.3 requires the use of commands to manage
769
782
# users. "No such command" error didn't return an error
@@ -793,6 +806,7 @@ def remove_user(self, name):
793
806
794
807
try :
795
808
self .command ("dropUser" , name ,
809
+ read_preference = ReadPreference .PRIMARY ,
796
810
writeConcern = self ._get_wc_override ())
797
811
except OperationFailure , exc :
798
812
# See comment in add_user try / except above.
@@ -930,7 +944,9 @@ def eval(self, code, *args):
930
944
if not isinstance (code , Code ):
931
945
code = Code (code )
932
946
933
- result = self .command ("$eval" , code , args = args )
947
+ result = self .command ("$eval" , code ,
948
+ read_preference = ReadPreference .PRIMARY ,
949
+ args = args )
934
950
return result .get ("retval" , None )
935
951
936
952
def __call__ (self , * args , ** kwargs ):
0 commit comments