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d.dfddZded.dfddZevgdZd4ed9eRd.eRfddZd4ed9ed.eejfddZd8ed9ed.eefddZddgiZe eRe eRfe4d<idedesdexdeede!dende^delde^dekdelde`deddepdefderdeuidefde^de^de^demdende^de^de^debdebdefde^de^dehdeade"eeeaeodZe eReeegeffe4d<e^eee^eaebe^eetedeld Ze eReeegeffe4d<eyezeqe(eefefeed Ze eReeegeffe4d<ddiZe eReRfe4d<iZe eReeReRffe4d<eeeD]\ZZeD]Zeevr(eeee<qqeZe eReeegeffe4d<eegdZe eRe4d<evdgZd8eRd9ed.eeReffddZd8eRd9ed.eeReffddÄZ &dde eRefde\d.e eReffddDŽZddɄZdd˄Zdd̈́Zevgd΢ZGddЄdeZGdd҄dej ZdS)z9Functions and classes common to multiple pymongo modules.N) OrderedDictabc) AnyCallableDictListMappingMutableMappingNoReturnOptionalSequenceTupleTypeUnion) unquote_plus)SON)UuidRepresentation) CodecOptions TypeRegistry)RawBSONDocument) MECHANISMS)validate_compressorsvalidate_zlib_compression_level) DriverInfoConfigurationError)_validate_event_listeners) ReadConcern) _MONGOS_MODES _ServerMode) ServerApi)DEFAULT_WRITE_CONCERN WriteConcern ORDERED_TYPESiMAX_MESSAGE_SIZEiz3.6 g?<g4@dMAX_IDLE_TIME_MSMAX_IDLE_TIME_SECWAIT_QUEUE_TIMEOUTT);COMMAND_NOT_FOUND_CODES) i@i@UNAUTHORIZED_CODESi'ZmongodbnodereturncCsZ|}d}|d}|dkr|d|t||dd}}|dr)|dd}||fS)z5Split a host:port string into (host, int(port)) pair.ii:Nr)[)rfindint startswith)r5hostportidxr@5/tmp/pip-target-onvjaxws/lib/python/pymongo/common.pypartition_nodes "  rBcCst|\}}||fS)z6Split and normalize a node name from a hello response.)rBlower)r5r=r>r@r@rA clean_nodes  rDkeydummycCstd|f)z1Raise ConfigurationError with the given key name.zUnknown option %sr)rErFr@r@rAraise_config_errorsrG) unspecifiedstandardZ pythonLegacyZ javaLegacyZ csharpLegacyoptionvaluecCt|tr|Std|f)z(Validates that 'value' is True or False.z%s must be True or False) isinstancebool TypeErrorrJrKr@r@rAvalidate_boolean rQcCs2t|tr|dvrtd|f|dkSt||S)z8Validates that value is True, False, 'true', or 'false'.)truefalsez)The value of %s must be 'true' or 'false'rS)rMstr ValueErrorrQrPr@r@rAvalidate_boolean_or_strings  rWcCsNt|tr|St|tr zt|WStytd|fwtd|f)zDValidates that 'value' is an integer (or basestring representation).z"The value of %s must be an integerz+Wrong type for %s, value must be an integerrMr;rUrVrOrPr@r@rAvalidate_integers    rYcCs$t||}|dkrtd|f|S)zFValidate that 'value' is a positive integer, which does not include 0.rz*The value of %s must be a positive integerrYrVrJrKvalr@r@rAvalidate_positive_integer r]cCs$t||}|dkrtd|f|S)z1Validate that 'value' is a positive integer or 0.rz.The value of %s must be a non negative integerrZr[r@r@rAvalidate_non_negative_integerr^r_cCs(|dur|St||}t|d|S)z1Validates that 'value' is file-like and readable.Nr)validate_stringopencloserPr@r@rAvalidate_readables  rdcC|dur|St||S)z4Validate that 'value' is a positive integer or None.Nr]rPr@r@rA!validate_positive_integer_or_none rgcCre)z9Validate that 'value' is a positive integer or 0 or None.N)r_rPr@r@rA%validate_non_negative_integer_or_nonerhricCrL)z/Validates that 'value' is an instance of `str`.z3Wrong type for %s, value must be an instance of str)rMrUrOrPr@r@rArarRracCre)z@Validates that 'value' is an instance of `basestring` or `None`.N)rarPr@r@rAvalidate_string_or_nonerhrjcCsHt|tr|St|trzt|WSty|YSwtd|f)/Validates that 'value' is an integer or string.z7Wrong type for %s, value must be an integer or a stringrXrPr@r@rAvalidate_int_or_basestrings    rlcCsTt|tr|St|tr#zt|}Wn ty|YSwt||Std|f)rkzDWrong type for %s, value must be an non negative integer or a string)rMr;rUrVr_rOr[r@r@rA'validate_non_negative_int_or_basestring s     rmcCsjd|f}zt|}Wntyt|ty t|wd|kr+dks3ntd|f|S)zXValidates that 'value' is a float, or can be converted to one, and is positive. z%s must be an integer or floatrgeAz3%s must be greater than 0 and less than one billion)floatrVrO)rJrKerrmsgr@r@rAvalidate_positive_floats    rpcC|dks|dkr dSt||S)zgValidates that 'value' is 0 or a positive float, or can be converted to 0 or a positive float. r0rprPr@r@rAvalidate_positive_float_or_zero-s rtcCs|dur|St||dSzcValidates a timeout specified in milliseconds returning a value in floating point seconds. N@@rsrPr@r@rAvalidate_timeout_or_none6rwcCs8|dur td|f|dks|dkrdSt||dS)zValidates a timeout specified in milliseconds returning a value in floating point seconds for the case where None is an error and 0 is valid. Setting the timeout to nothing in the URI string is a config error. Nz%s cannot be Nonerrrrv)rrprPr@r@rAvalidate_timeout_or_zero?s rycCs*|dus |dks |dkrdSt||dS)zValidates a timeout specified in milliseconds returning a value in floating point seconds. value=0 and value="0" are treated the same as value=None which means unlimited timeout. NrrrrvrsrPr@r@rA validate_timeout_or_none_or_zeroLsrzcCs|durdSt||dSru)rtrPr@r@rAvalidate_timeoutmsVrxr{cCrq)zBValidates maxStalenessSeconds according to the Max Staleness Spec.r8z-1rfrPr@r@rAvalidate_max_staleness_s r|cCst|ts td|f|S)zValidate a read preference.z%r is not a read preference.)rMrrOrFrKr@r@rAvalidate_read_preferenceg r~cCs|tvr td|f|S)zValidate read preference mode for a MongoClient. .. versionchanged:: 3.5 Returns the original ``value`` instead of the validated read preference mode. z!%s is not a valid read preference)rrVr}r@r@rAvalidate_read_preference_modensrcCs |tvrtd|ttf|S)z&Validate the authMechanism URI option.z%s must be in %s)rrVtuplerPr@r@rAvalidate_auth_mechanismzsrcCs.zt|WStytd|ttfw)z, did you forget to percent-escape the token with quote_plus?zTauth mechanism properties must be key:value pairs like SERVICE_NAME:mongodb, not %s.zA%s is not a supported auth mechanism property. Must be one of %s.r)rarrV_MECHANISM_PROPSrrWr)rJrKpropsoptrEr\r@r@rA"validate_auth_mechanism_propertiess2    rcCs`d}zt|tj}Wntyt|drt|jtj}Ynw|s.t|ts.td|f|S)z#Validate the document_class option.F __origin__zh%s must be dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a sublass of collections.MutableMapping) issubclassrr rOhasattrrr)rJrKZ is_mappingr@r@rAvalidate_document_classs  rcCs&|durt|tstd|tf|S)z"Validate the type_registry option.Nz%s must be an instance of %s)rMrrOrPr@r@rAvalidate_type_registrysrcCst|ts td|f|S)z!Validates that 'value' is a list.z%s must be a list)rMrrOrPr@r@rA validate_listrrcCre)z)Validates that 'value' is a list or None.N)rrPr@r@rAvalidate_list_or_nonerhrcC"t|tjtfstd|fdS)z/Validates that 'value' is a list or a document.zw%s must either be a list or an instance of dict, bson.son.SON, or any other type that inherits from collections.MappingN)rMrrrrOrPr@r@rAvalidate_list_or_mappingsrcCst|tjs td|fdS)z=Validate the type of method arguments that expect a document.zf%s must be an instance of dict, bson.son.SON, or any other type that inherits from collections.MappingN)rMrrrOrPr@r@rAvalidate_is_mappings rcCr)zEValidate the type of method arguments that expect a MongoDB document.z%s must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMappingN)rMrr rrOrPr@r@rAvalidate_is_document_typesrcCs:|dur|St||t|ddkrtd|f|S)zValidate the appname option.Nzutf-8z%s must be <= 128 bytes)ralenencoderVrPr@r@rAvalidate_appname_or_nones  rcC(|dur|St|tstd|f|S) Validate the driver keyword arg.Nz$%s must be an instance of DriverInfo)rMrrOrPr@r@rAvalidate_driver_or_none  rcCr)z$Validate the server_api keyword arg.Nz#%s must be an instance of ServerApi)rMr rOrPr@r@rAvalidate_server_api_or_none%rrcCs&|dur|St|std|f|S)z%Validates that 'value' is a callable.Nz%s must be a callable)callablerVrPr@r@rAvalidate_is_callable_or_none.s r replacementcCsBtd||rt|tstt|}|drtddSdSdS)z Validate a replacement document.r$z'replacement can not include $ operatorsN)rrMrnextiterr<rV)rfirstr@r@rAvalidate_ok_for_replace7s   rupdatecCsLtd||s tdt|t }tt|}|r"|ds$tddSdS)zValidate an update document.rzupdate cannot be emptyrz"update only works with $ operatorsN)rrVrMrrrr<)rZ is_documentrr@r@rAvalidate_ok_for_updateAs   r)strictreplaceignorecCs |tvrtd|ttf|S)zAValidate the Unicode decode error handler option of CodecOptions.z@%s is an invalid Unicode decode error handler. Must be one of %s)_UNICODE_DECODE_ERROR_HANDLERSrVrr}r@r@rA%validate_unicode_decode_error_handlerQs rcCs$|durt|tjstd||S)zValidate the tzinfo optionNz)%s must be an instance of datetime.tzinfo)rMdatetimetzinforOr}r@r@rAvalidate_tzinfo\s rcCs4|dur|Sddlm}t||std|f|S)rNr)AutoEncryptionOptsz,%s must be an instance of AutoEncryptionOpts)Zpymongo.encryption_optionsrrMrO)rJrKrr@r@rA%validate_auto_encryption_opts_or_nonecs   rZtlssslURI_OPTIONS_ALIAS_MAPappnameZ authmechanismZauthmechanismpropertiesZ authsourceZ compressorsconnecttimeoutmsZdirectconnectionheartbeatfrequencymsjournalZlocalthresholdms maxidletimemsZ maxconnectingZ maxpoolsizemaxstalenesssecondsZreadconcernlevelZreadpreferenceZreadpreferencetagsZ replicasetZ retryreadsZ retrywritesZ loadbalancedserverselectiontimeoutmssockettimeoutmsZtlsallowinvalidcertificatesZtlsallowinvalidhostnamesZ tlscafileZtlscertificatekeyfileZtlscertificatekeyfilepasswordZtlsdisableocspendpointcheckZ tlsinsecurew wtimeoutmsZzlibcompressionlevel)ZsrvservicenameZ srvmaxhostsZ timeoutmsURI_OPTIONS_VALIDATOR_MAP) connectZdriverZ server_apifsyncZ minpoolsizeZ tlscrlfileZtz_awareZunicode_decode_error_handlerZuuidrepresentationZwaitqueuemultiplewaitqueuetimeoutmsNONSPEC_OPTIONS_VALIDATOR_MAP) Zdocument_classZ type_registryread_preferenceZevent_listenersrusernamepasswordZserver_selectorZauto_encryption_opts KW_VALIDATORSINTERNAL_URI_OPTION_NAME_MAPURI_OPTIONS_DEPRECATION_MAP VALIDATORS)rrrrrrrTIMEOUT_OPTIONScCs,t||\}}|tvrtd|f||fS)z,Validate optional authentication parameters.z!Unknown authentication option: %s)validate _AUTH_OPTIONSr)rJrKrCr@r@rAvalidate_auth_optionsrcCs&|}t|t}|||}||fS)zGeneric validation function.)rCrgetrG)rJrKrC validatorr@r@rArs  roptionswarnc sttrt}dd}fdd}n i}dd}dd}D]<\}}||}z t|t}|||}Wn tttfyV} z|rKt t | nWYd} ~ q!d} ~ ww||||<q!|S)aValidate each entry in options and raise a warning if it is not valid. Returns a copy of options with invalid entries removed. :Parameters: - `opts`: A dict containing MongoDB URI options. - `warn` (optional): If ``True`` then warnings will be logged and invalid options will be ignored. Otherwise, invalid options will cause errors. cS|SNr@xr@r@rAz'get_validated_options..cs |Sr) cased_keyrrr@rArs cSs|Sr)rCrr@r@rArscSrrr@rr@r@rArrN) rM_CaseInsensitiveDictionaryitemsrrrGrVrOrwarningsrrU) rrZvalidated_optionsZget_normed_keyZget_setter_keyrrKZ normed_keyrexcr@rrAget_validated_optionss*   rcC|dd|dS)NZ escCollectionenxcol_.z.escrZencrypted_fieldsrr@r@rA_esc_coll_name%rcCr)NZ eccCollectionrz.eccrrr@r@rA_ecc_coll_name)rrcCr)NZecocCollectionrz.ecocrrr@r@rA_ecoc_coll_name-rr)rZwtimeoutrrjrc @seZdZdZdededededdf dd Ze defd d Z e defd d Z ddZ e defddZ ddZe defddZdS) BaseObjectzA base class that provides attributes and methods common to multiple pymongo classes. SHOULD NOT BE USED BY DEVELOPERS EXTERNAL TO MONGODB. codec_optionsr write_concern read_concernr6NcCsjt|ts td||_t|tstd|f||_t|ts$td||_t|ts0td||_ dS)NzDcodec_options must be an instance of bson.codec_options.CodecOptionszT%r is not valid for read_preference. See pymongo.read_preferences for valid options.zGwrite_concern must be an instance of pymongo.write_concern.WriteConcernzDread_concern must be an instance of pymongo.read_concern.ReadConcern) rMrrO_BaseObject__codec_optionsr_BaseObject__read_preferencer"_BaseObject__write_concernr_BaseObject__read_concern)selfrrrrr@r@rA__init__<s$     zBaseObject.__init__cC|jS)zdRead only access to the :class:`~bson.codec_options.CodecOptions` of this instance. )rrr@r@rArYszBaseObject.codec_optionscCr)zRead only access to the :class:`~pymongo.write_concern.WriteConcern` of this instance. .. versionchanged:: 3.0 The :attr:`write_concern` attribute is now read only. )rrr@r@rAr`szBaseObject.write_concerncCs|r|jrtS|jS)zBRead only access to the write concern of this instance or session.)Zin_transactionr!rrsessionr@r@rA_write_concern_forjs zBaseObject._write_concern_forcCr)zRead only access to the read preference of this instance. .. versionchanged:: 3.0 The :attr:`read_preference` attribute is now read only. )rrr@r@rArqzBaseObject.read_preferencecCs|r |p|jS|jS)zDRead only access to the read preference of this instance or session.)Z_txn_read_preferencerrr@r@rA_read_preference_forzszBaseObject._read_preference_forcCr)zRead only access to the :class:`~pymongo.read_concern.ReadConcern` of this instance. .. versionadded:: 3.2 )rrr@r@rArrzBaseObject.read_concern)__name__ __module__ __qualname____doc__rrr"rrpropertyrrrrrrr@r@r@rAr5s.  rc@seZdZddZddZddZddZd d Zd d Zd dZ ddZ ddZ d"ddZ ddZ ddZddZd"ddZddZd d!ZdS)#rcOs$i|_i|_|t|i|dSr)%_CaseInsensitiveDictionary__casedkeys _CaseInsensitiveDictionary__datardict)rargskwargsr@r@rArsz#_CaseInsensitiveDictionary.__init__cCs||jvSr)rCr rrEr@r@rA __contains__z'_CaseInsensitiveDictionary.__contains__cCs t|jSr)rr rr@r@rA__len__s z"_CaseInsensitiveDictionary.__len__cCsdd|jDS)Ncss|]}|VqdSrr@).0rEr@r@rA sz6_CaseInsensitiveDictionary.__iter__..)r rr@r@rA__iter__sz#_CaseInsensitiveDictionary.__iter__cstfddDS)Ncsi|] }j|j|qSr@)r r )rkrr@rA sz7_CaseInsensitiveDictionary.__repr__..)rUrr@rrA__repr__sz#_CaseInsensitiveDictionary.__repr__cCs |}||j|<||j|<dSrrCr r )rrErKlc_keyr@r@rA __setitem__s z&_CaseInsensitiveDictionary.__setitem__cC|j|Sr)r rCrr@r@rA __getitem__rz&_CaseInsensitiveDictionary.__getitem__cCs|}|j|=|j|=dSrr)rrErr@r@rA __delitem__s z&_CaseInsensitiveDictionary.__delitem__cCsHt|tjstSt|t|krdS|D] }||||kr!dSqdS)NFT)rMrrNotImplementedrrotherrEr@r@rA__eq__s z!_CaseInsensitiveDictionary.__eq__NcCs|j||Sr)r rrC)rrEdefaultr@r@rArsz_CaseInsensitiveDictionary.getcOs0|}|j|d|jj|g|Ri|Sr)rCr popr )rrEr rrr@r@rAr$sz_CaseInsensitiveDictionary.popcCs"|j\}}|j|}||fSr)r popitemr r$)rrrrKr@r@rAr%s z"_CaseInsensitiveDictionary.popitemcCs|j|jdSr)r clearr rr@r@rAr&s z _CaseInsensitiveDictionary.clearcCs2|}||vr |j|S||j|<||j|<|Sr)rCr r )rrEr#rr@r@rA setdefaults    z%_CaseInsensitiveDictionary.setdefaultcCsDt|tr|D] }|||||<qdS|D]}||||<qdSr)rMrrr r@r@rArs z!_CaseInsensitiveDictionary.updatecCrr)r rCrr@r@rArrz$_CaseInsensitiveDictionary.cased_keyr)rrrrrrrrrrrr"rr$r%r&r'rrr@r@r@rArs"   r)T)rrr collectionsrrtypingrrrrrr r r r r rr urllib.parserZbsonrZ bson.binaryrZbson.codec_optionsrrZ bson.raw_bsonrZ pymongo.authrZpymongo.compression_supportrrZpymongo.driver_inforZpymongo.errorsrZpymongo.monitoringrZpymongo.read_concernrZpymongo.read_preferencesrrZpymongo.server_apir Zpymongo.write_concernr!r"r#__annotations__Z MAX_BSON_SIZEr%r;ZMIN_WIRE_VERSIONZMAX_WIRE_VERSIONZMAX_WRITE_BATCH_SIZEZMIN_SUPPORTED_SERVER_VERSIONZMIN_SUPPORTED_WIRE_VERSIONZMAX_SUPPORTED_WIRE_VERSIONZHEARTBEAT_FREQUENCYZKILL_CURSOR_FREQUENCYZEVENTS_QUEUE_FREQUENCYZSERVER_SELECTION_TIMEOUTZMIN_HEARTBEAT_INTERVALZMIN_SRV_RESCAN_INTERVALZCONNECT_TIMEOUTZ MAX_POOL_SIZEZ MIN_POOL_SIZEZMAX_CONNECTINGr-r.r/ZLOCAL_THRESHOLD_MSZ RETRY_WRITESZ RETRY_READSr2r4Z_MAX_END_SESSIONSZSRV_SERVICE_NAMErUrBrDrGZ UNSPECIFIEDSTANDARDZ PYTHON_LEGACYZ JAVA_LEGACYZ CSHARP_LEGACYrrNrQrWrYr]r_rdrgrirarjrlrmrnrprtrwryrzr{r|r~rrrr frozensetrrrrrrrrrrrrrrrrrrrrrrrrrrrroptnamealiasesaliascopyrrrrrrrrrZWRITE_CONCERN_OPTIONSobjectrrr@r@r@rAs8                       " &!                !"#$+""   $      % V