o Sb@stdZddlmZGdddeZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdS)zFCriteria to select some ServerDescriptions from a TopologyDescription.) SERVER_TYPEc@sheZdZdZeddZddZddZdd Ze d d Z e d d Z e ddZ ddZ ddZdS) Selectionz.Input or output of a server selector function.cCs:|j}d}|D] }|jtjkr|}nqt||j|j|SN) known_servers server_typerZ RSPrimaryrcommon_wire_version)clstopology_descriptionrprimarysdr ?/tmp/pip-target-onvjaxws/lib/python/pymongo/server_selectors.pyfrom_topology_descriptions z#Selection.from_topology_descriptioncCs||_||_||_||_dSr)r server_descriptionsr r)selfr rrr r r r __init__'s zSelection.__init__cCst|j||j|jSr)rr rr )rrr r r with_server_descriptions-sz"Selection.with_server_descriptionscCs$t|}|jrt|jdddSdS)NcSs|jSr)Zlast_write_date)r r r r 5sz>Selection.secondary_with_max_last_write_date..)key)secondary_server_selectorrmax)rZ secondariesr r r "secondary_with_max_last_write_date2sz,Selection.secondary_with_max_last_write_datecCs|jr|jgng}||Sr)r r)rZ primariesr r r primary_selection7s zSelection.primary_selectioncC|jjSr)r heartbeat_frequencyrr r r r<zSelection.heartbeat_frequencycCrr)r topology_typerr r r r@rzSelection.topology_typecCs t|jSr)boolrrr r r __bool__D zSelection.__bool__cCs |j|Sr)r)ritemr r r __getitem__Gr zSelection.__getitem__N)__name__ __module__ __qualname____doc__ classmethodrrrrpropertyrrrrr"r r r r rs     rcCs|Srr  selectionr r r any_server_selectorKsr+cC|dd|jDS)NcSg|]}|jr|qSr )Z is_readable.0sr r r Qz,readable_server_selector..rrr)r r r readable_server_selectorOr4cCr,)NcSr-r ) is_writabler.r r r r1Wr2z,writable_server_selector..r3r)r r r writable_server_selectorUr5r7cCr,)NcSg|] }|jtjkr|qSr )rrZ RSSecondaryr.r r r r1]z-secondary_server_selector..r3r)r r r r[r5rcCr,)NcSr8r )rrZ RSArbiterr.r r r r1cr9z+arbiter_server_selector..r3r)r r r arbiter_server_selectorar5r:cCst|pt|S)z6Like PrimaryPreferred but doesn't use tags or latency.)r7rr)r r r "writable_preferred_server_selectorgsr;cs&fdd|fdd|jDS)zAll servers matching one tag set. A tag set is a dict. A server matches if its tags are a superset: A server tagged {'a': '1', 'b': '2'} matches the tag set {'a': '1'}. The empty tag set {} matches any server. cs0D]\}}||vs|||krdSqdS)NFT)items)Z server_tagsrvalue)tag_setr r tags_matchus z(apply_single_tag_set..tags_matchcsg|] }|jr|qSr )tagsr.)r?r r r1}sz(apply_single_tag_set..r3)r>r*r )r>r?r apply_single_tag_setls rAcCs*|D] }t||}|r|Sq|gS)aNAll servers match a list of tag sets. tag_sets is a list of dicts. The empty tag set {} matches any server, and may be provided at the end of the list as a fallback. So [{'a': 'value'}, {}] expresses a preference for servers tagged {'a': 'value'}, but accepts any server if none matches the first preference. )rAr)tag_setsr*r>Z with_tag_setr r r apply_tag_setss   rCcCt|t|S)z2All near-enough secondaries matching the tag sets.)rCrrBr*r r r #secondary_with_tags_server_selectorrFcCrD)z.All near-enough members matching the tag sets.)rCr4rEr r r member_with_tags_server_selectorrGrHN)r&Zpymongo.server_typerobjectrr+r4r7rr:r;rArCrFrHr r r r s 7