ó i'dWc@sMdZddlZddlmZmZmZddlZddlm Z m Z m Z m Z m Z mZmZddlmZmZddlmZmZddlmZmZmZddlmZdd lmZd efd „ƒYZd „Z d „Z!defd„ƒYZ"d„Z#defd„ƒYZ$defd„ƒYZ%dS(sz state.py: provide unique state implementations for the different possible nvlistp (as determined by its flags) iÿÿÿÿN(tENOENTtEINVALtENOMEM(t DATA_TYPE_MAPtNV_UNIQUE_NAMEtNV_UNIQUE_NAME_TYPEtDATA_TYPE_BOOLEANtDATA_TYPE_UNKNOWNtDATA_TYPE_NVLISTtDATA_TYPE_NVLIST_ARRAY(tnvlistptnvpairp(tcfunctutil(tNVPairtNVPairIteratortState(tADD(tLOOKUPt _NVPairFalsecBsVeZdZd„Zed„ƒZed„ƒZed„ƒZd„Zd„Z RS(sœ A fake NVPair (real ones require an nvpairp and a State. Only need this for DATA_TYPE_BOOLEAN where non-existence is interpreted as False. cCs ||_dS(N(t_name(tselftname((s ../state.pyt__init__3scCs|jS(N(R(R((s ../state.pyR6scCstS(N(R(R((s ../state.pytdatatype:scCstS(N(tFalse(R((s ../state.pytvalue>scCsdS(N((Rtnvl((s ../state.pytremoveBscCsd|jfS(sx.__repr__() <==> repr(x)sNVPair: %s = False(R(R((s ../state.pyt__repr__Es( t__name__t __module__t__doc__RtpropertyRRRRR(((s ../state.pyR-s  cCsˆd„}|j|j}}|jtkr:||ƒ}n7|jtkrqtg|D]}||ƒ^qSƒ}n|j|ƒ||fS(NcSsŒtƒ}tj|tj|ƒdƒ}|dkrˆ|tkrNtdƒ‚qˆ|tkritdƒ‚qˆt |dt j |ƒƒ‚n|S(Nisnvlist_dup(): invalid arguments!nvlist_dup(): insufficient memorysnvlist_dup(): %s( R R t nvlist_duptCtbyrefRt TypeErrorRt MemoryErrortOSErrortoststrerror(torigtdupterr((s ../state.pyR"Ns    (RRRR ttupleR(tnvpairtnvlistR"RRtelem((s ../state.pyt_extract_and_deleteJs ( csåt|tƒr?|d‡fd†‰‰}dˆf}}n>tj|ƒ\‰‰‡‡fd†}dˆtˆf}}gt|ƒD]}||ƒrŠ|^qŠ}t|ƒr¸|SˆtkrÑt ˆƒgSt ||ƒ‚dS(sœ _iter_search(State, str) -> (NVPair, ...) _iter_search(State, (str, int)) -> (NVPair, ...) _iter_search(State, (str, str)) -> (NVPair, ...) Given any State, and a valid key, search for a matching NVPair. If the key is just a str, then datatype will not be checked. If the key is a tuple, then both name and datatype must match. If no matches are found KeyError(key) is raised. cs |jˆkS(N(R(tp(R(s ../state.pytsss%scs|jˆko|jˆkS(N(RR(R2(RR(s ../state.pyR3wss(%s, %s)N( t isinstancetstrtNoneR tnormalize_tuple_keyRRtlenRRtKeyError(tstatetkeytcomparetfmttargstpairtacc((RRs ../state.pyt _iter_searchfs +   t NVLIteratorcBsYeZdZdd„Zed„ƒZd„Zd„Zd„Z e ƒZ e d„Z RS(s NVLIterator is the least powerful of the NVList states. While you can use the dictionary look-up, that will simply retrieve the first matching key it finds. There may be more. cCs/tt|ƒj|ƒ|jdks+t‚dS(Ni(tsuperRBRtflagstAssertionError(Rtnvlp((s ../state.pyRŒscCsdS(Ni((R((s ../state.pyRDscCs-tj|ƒ\}}t||||ƒdS(N(R R7R(RR;RRR((s ../state.pyt __setitem__”scCs+x$t||ƒD]}|j|ƒqWdS(N(RAR(RR;R?((s ../state.pyt __delitem__šscCs2t||ƒ}g|D]}|j|jf^qS(N(RARR(RR;tpairsR?((s ../state.pyt__getitem_helper_state__ŸscCskyt||ƒ}Wn5tk rJ}||jkr:‚qKt|fgSnXg|D]}t||ƒ^qRS(N(RAR9t_NVLIterator__markerRR1(RR;tdefaultRIR,R?((s ../state.pyt__pop_helper__¦sN( RRR R6RR!RDRGRHRJtobjectRKRM(((s ../state.pyRB„s     c CsKt|tƒstdƒ‚nt|tƒrM|d}}d|f}}n,tj|ƒ\}}d|t|f}}tƒ}t j ||t j |ƒƒ}|t tfkrÛ|tkrÈt|ƒSt||ƒ‚n+|dkrt|dtj|ƒƒ‚nt||ƒ}|rG||jkrGtd|t|fƒ‚n|S(s” _dict_search(State, str) -> NVPair _dict_search(State, (str, int)) -> NVPair _dict_search(State, (str, str)) -> NVPair Given a NVLDictionary State, and a valid key, search for a matching NVPair. If the key is just a str, then datatype will not be checked. If the key is a tuple, then both name and datatype must match. If no matches are found KeyError(key) is raised. s*can only search by name with NVLDictionarys%ss(%s, %s)isnvlist_lookup_nvpair(): %sN(R4t NVLDictionaryR%R5R6R R7RR R tnvlist_lookup_nvpairR#R$RRRRR9R'R(R)RR( R:R;RRR=R>tnvpR,R?((s ../state.pyt _dict_search´s(       ROcBsYeZdZdd„Zed„ƒZd„Zd„Zd„Z e ƒZ e d„Z RS(s NVLDictionary is the most efficient of the NVList States. It is the only one we don't have to loop to find NVpairs. cCstt|ƒj|ƒdS(N(RCROR(RRF((s ../state.pyRïscCstS(N(R(R((s ../state.pyRDòscCs-tj|ƒ\}}t||||ƒdS(N(R R7R(RR;RRR((s ../state.pyRGöscCs t||ƒ}|j|ƒdS(N(RRR(RR;R?((s ../state.pyRHþscCs"t||ƒ}|j|jfgS(N(RRRR(RR;R?((s ../state.pyRJscCs[yt||ƒ}Wn5tk rJ}||jkr:‚qKt|fgSnXt||ƒgS(N(RRR9t_NVLDictionary__markerRR1(RR;RLR?R,((s ../state.pyRM sN( RRR R6RR!RDRGRHRJRNRSRM(((s ../state.pyROés     tNVLTupleDictionarycBsYeZdZdd„Zed„ƒZd„Zd„Zd„Z e ƒZ e d„Z RS(së This state behaves like a python dictionary which must have keys (str, DATA_TYPE). Unfortunately, without an API like nvlist_lookup_nvpair that takes a datatype, we are slowed down when deleting or popping items. cCstt|ƒj|ƒdS(N(RCRTR(RRF((s ../state.pyR"scCstS(N(R(R((s ../state.pyRD%scCs-tj|ƒ\}}t||||ƒdS(N(R R7R(RR;RRR((s ../state.pyRG)scCs$t||ƒd}|j|ƒdS(Ni(RAR(RR;R?((s ../state.pyRH1scCsvt|tƒrAt||ƒ}g|D]}|j|jf^q%Stj|ƒ\}}t|||ƒ}||fgS(N(R4R5RARRR R7R(RR;RIR?RRtresult((s ../state.pyRJ7s #cCskyt||ƒ}Wn5tk rJ}||jkr:‚qKt|fgSnXg|D]}t||ƒ^qRS(N(RAR9t_NVLTupleDictionary__markerRR1(RR;RLRIR,R?((s ../state.pyRMDsN( RRR R6RR!RDRGRHRJRNRVRM(((s ../state.pyRTs    (&R tctypesR#terrnoRRRR(tlibnvpair.constRRRRRRR tlibnvpair.cstructR R t libnvpairR R tlibnvpair.privateRRRt libnvpair.addRtlibnvpair.lookupRRNRR1RARBRRRORT(((s ../state.pyts   4  0 50