1919
2020from pymongo .auth import MECHANISMS
2121from pymongo .errors import ConfigurationError
22+ from pymongo .write_concern import WriteConcern
2223from bson .binary import (OLD_UUID_SUBTYPE , UUID_SUBTYPE ,
2324 JAVA_LEGACY , CSHARP_LEGACY )
2425
@@ -245,8 +246,7 @@ def validate_read_preference_tags(name, value):
245246 return [tags ]
246247
247248
248-
249- # jounal is an alias for j,
249+ # journal is an alias for j,
250250# wtimeoutms is an alias for wtimeout,
251251VALIDATORS = {
252252 'replicaset' : validate_basestring ,
@@ -310,22 +310,6 @@ def validate(option, value):
310310])
311311
312312
313- class WriteConcern (dict ):
314-
315- def __init__ (self , * args , ** kwargs ):
316- """A subclass of dict that overrides __setitem__ to
317- validate write concern options.
318- """
319- super (WriteConcern , self ).__init__ (* args , ** kwargs )
320-
321- def __setitem__ (self , key , value ):
322- if key not in WRITE_CONCERN_OPTIONS :
323- raise ConfigurationError ("%s is not a valid write "
324- "concern option." % (key ,))
325- key , value = validate (key , value )
326- super (WriteConcern , self ).__setitem__ (key , value )
327-
328-
329313class BaseObject (object ):
330314 """A base class that provides attributes and methods common
331315 to multiple pymongo classes.
@@ -337,20 +321,12 @@ def __init__(self, **options):
337321
338322 self .__read_pref = read_preferences .ReadPreference .PRIMARY
339323 self .__uuid_subtype = OLD_UUID_SUBTYPE
340- self .__write_concern = WriteConcern ()
324+ self .__write_concern = None
341325 self .__set_options (options )
342326
343- def __set_write_concern_option (self , option , value ):
344- """Validates and sets getlasterror options for this
345- object (MongoClient, Database, Collection, etc.)
346- """
347- if value is None :
348- self .__write_concern .pop (option , None )
349- else :
350- self .__write_concern [option ] = value
351-
352327 def __set_options (self , options ):
353328 """Validates and sets all options passed to this object."""
329+ wc_opts = {}
354330 for option , value in options .iteritems ():
355331 if option == 'read_preference' :
356332 self .__read_pref = validate_read_preference (option , value )
@@ -365,25 +341,20 @@ def __set_options(self, options):
365341 elif option == 'uuidrepresentation' :
366342 self .__uuid_subtype = validate_uuid_subtype (option , value )
367343 elif option in WRITE_CONCERN_OPTIONS :
368- if option == ' journal' :
369- self . __set_write_concern_option ( 'j' , value )
370- elif option == ' wtimeoutms' :
371- self . __set_write_concern_option ( ' wtimeout' , value )
344+ if option == " journal" :
345+ wc_opts [ "j" ] = value
346+ elif option == " wtimeoutms" :
347+ wc_opts [ " wtimeout" ] = value
372348 else :
373- self .__set_write_concern_option (option , value )
349+ wc_opts [option ] = value
350+ self .__write_concern = WriteConcern (** wc_opts )
374351
375352 def __set_write_concern (self , value ):
376353 """Property setter for write_concern."""
377354 if not isinstance (value , dict ):
378355 raise ConfigurationError ("write_concern must be an "
379356 "instance of dict or a subclass." )
380- # Make a copy here to avoid users accidentally setting the
381- # same dict on multiple instances.
382- wc = WriteConcern ()
383- for k , v in value .iteritems ():
384- # Make sure we validate each option.
385- wc [k ] = v
386- self .__write_concern = wc
357+ self .__write_concern = WriteConcern (** value )
387358
388359 def __get_write_concern (self ):
389360 """The default write concern for this instance.
@@ -435,7 +406,7 @@ def __get_write_concern(self):
435406 """
436407 # To support dict style access we have to return the actual
437408 # WriteConcern here, not a copy.
438- return self .__write_concern
409+ return self .__write_concern . document
439410
440411 write_concern = property (__get_write_concern , __set_write_concern )
441412
@@ -477,22 +448,7 @@ def _get_wc_override(self):
477448 We don't want to override user write concern options if write concern
478449 is already enabled.
479450 """
480- if self .__write_concern .get ( 'w' ) != 0 :
451+ if self .__write_concern .acknowledged :
481452 return {}
482453 return {'w' : 1 }
483454
484- def _get_write_mode (self , options ):
485- """Get the current write mode.
486-
487- Determines if the current write is acknowledged or not based on the
488- inherited write_concern values, or passed options.
489-
490- :Parameters:
491- - `options`: overriding write concern options.
492-
493- .. versionadded:: 2.3
494- """
495- write_concern = options or self .__write_concern
496- if write_concern .get ('w' ) == 0 :
497- return False , {}
498- return True , write_concern
0 commit comments