o Sb!@s"dZddlZddlZddlmZddlmZmZmZddl m Z ddl m Z ddl mZmZmZmZmZmZmZmZmZddlmZed d gZegd eBZeegd BZd dZd%ddZddZ d&ddZ deedefddZ!dedefddZ"ddZ#dd Z$d!d"Z%d#d$Z&dS)'zCBits and pieces used by the driver that don't really fit elsewhere.N)abc)AnyListNoReturn)SON) ASCENDING) CursorNotFoundDuplicateKeyErrorExecutionTimeoutNotPrimaryErrorOperationFailureWriteConcernError WriteError WTimeoutError_wtimeout_error) HelloCompatiP-[)iJ'i{'i{4iR-i|4)Yi)#icCsddd|DS)z9Generate an index name from the set of fields it is over._cSsg|]}d|qS)z%s_%s).0itemrr6/tmp/pip-target-onvjaxws/lib/python/pymongo/helpers.py Jsz#_gen_index_name..)join)keysrrr_gen_index_nameHsrcCsT|dur ||fgSt|tr|tfgSt|tjrt|St|ttfs(td|S)zHelper to generate a list of (key, direction) pairs. Takes such a list, or a single key, or a single key and direction. NzEif no direction is specified, key_or_list must be an instance of list) isinstancestrrr ItemsViewlisttuple TypeError)Z key_or_list directionrrr _index_listMs    r'cCst|tjrtdt|t|ttfstdt|t|s't dt }|D]\}}t|t s9tdt|t t tjfsFtd|||<q,|S)zbHelper to generate an index specifying document. Takes a list of (key, direction) pairs. zhpassing a dict to sort/create_index/hint is not allowed - use a list of tuples instead. did you mean %r?z0must use a list of (key, direction) pairs, not: z&key_or_list must not be the empty listz6first item in each key pair must be an instance of strz[second item in each key pair must be 1, -1, '2d', or another valid MongoDB index specifier.) r rMappingr%r#itemsr$reprlen ValueErrorrr!int)Z index_listindexkeyvaluerrr_index_document^s(     r1Fc Cs`d|vrt|d|d|||r-d|vr-|d}|d}|r)|d|it||dr3dS|}d|vrP|dD]}|drO|dsO|}nq?|d}|d} |rl| durf| |vredSn||vrldS| durz| tvryt||ntj|vsd |vrt||| d vrt || ||| d krt || ||| d krt || ||t|| ||) z+Check the response to a command for errors.okz$errcodewriteConcernError errorLabelsNrawerrmsgznode is recovering)*i*i&12+) r getupdate_raise_write_concern_errorvalues_NOT_PRIMARY_CODESr rZ LEGACY_ERRORr r r) responseZmax_wire_versionZallowable_errorsZparse_write_concern_error_errorZ_labelsdetailsZshardr7r3rrr_check_command_response{sR     rC write_errorsreturncCs@|d}|ddkrt|dd|t|d|d|)Nr3r8r7)r;r r)rDerrorrrr_raise_last_write_errorsrHrGcCs8t|rt|d|d|t|d|d|)Nr7r3)rrr;r )rGrrrr=sr=cCs(|d}|r|d}|r||d<|S)z%Return the writeConcernError or None.r4r5)r;)resultwceZ error_labelsrrr _get_wce_docs  rKcCs2|d}|r t|t|}|rt|dSdS)z?Backward compatibility helper for write command error handling.Z writeErrorsN)r;rHrKr=)rIrDrJrrr_check_write_command_responses  rLcCs\t|tjr|St|tjtjfr'tdd|Ds!td|ft|dStd|f)zTakes a sequence of field names and returns a matching dictionary. ["a", "b"] becomes {"a": 1, "b": 1} and ["a.b.c", "d", "a.c"] becomes {"a.b.c": 1, "d": 1, "a.c": 1} css|]}t|tVqdSN)r r!)rfieldrrr sz'_fields_list_to_dict..z7%s must be a list of key names, each an instance of strz)%s must be a mapping or list of key names) r rr(SequenceSetallr%dictfromkeys)fieldsZ option_namerrr_fields_list_to_dicts  rWcCs\tjr,t}z"zt|d|d|ddtjWW~dSty)YW~dSw~wdS)z1Print exceptions raised by subscribers to stderr.rrPN)sysstderrexc_info tracebackprint_exceptionIOError)Zeinforrr_handle_exceptions$ r_rM)NF)'__doc__rYr\ collectionsrtypingrrrZbson.sonrZpymongorZpymongo.errorsrr r r r r rrrZ pymongo.hellor frozensetZ_SHUTDOWN_CODESr?Z_RETRYABLE_ERROR_CODESrr'r1rCrHr=rKrLrWr_rrrrsD   ,     =