Ñò Š„[Jc@s¤dZddklZddklZdddgZd„Zd„Zed „Z d e fd „ƒYZ d e fd „ƒYZ eed„Z d„Zd„ZdS(sMTopological sorting algorithms. The topological sort is an algorithm that receives this list of dependencies as a *partial ordering*, that is a list of pairs which might say, *X is dependent on Y*, *Q is dependent on Z*, but does not necessarily tell you anything about Q being dependent on X. Therefore, its not a straight sort where every element can be compared to another... only some of the elements have any sorting preference, and then only towards just some of the other elements. For a particular partial ordering, there can be many possible sorts that satisfy the conditions. iÿÿÿÿ(tCircularDependencyError(tutiltsorttsort_with_cyclest sort_as_treecCs7g}t||dtdtƒD]}||iq ~S(swsort the given list of items by dependency. 'tuples' is a list of tuples representing a partial ordering. t allow_cyclestignore_self_cycles(t_sorttFalsetTruetitem(ttuplestallitemst_[1]tn((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyRscCs_g}t||dtƒD]>}||ig}|ipgD]}||iq>~fq~S(s!sort the given list of items by dependency, cutting out cycles. returns results as an iterable of 2-tuples, containing the item, and a list containing items involved in a cycle with this item, if any. 'tuples' is a list of tuples representing a partial ordering. R(RR R tcycles(R R R Rt_[2]((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR"s cCstt||d|ƒƒS(ssort the given list of items by dependency, and return results as a hierarchical tree structure. returns results as an iterable of 3-tuples, containing the item, a list containing items involved in a cycle with this item, if any, and a list of child tuples. if with_cycles is False, the returned structure is of the same form but the second element of each tuple, i.e. the 'cycles', is an empty list. 'tuples' is a list of tuples representing a partial ordering. R(t_organize_as_treeR(R R t with_cycles((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR-st_NodecBs>eZdZd„Zd„Zdd„Zd„Zd„ZRS(s Represent each item in the sort.cCs+||_tƒ|_g|_d|_dS(N(R tsett dependenciestchildrentNoneR(tselfR ((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt__init__As   cCs |iƒS(N(tsafestr(R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt__str__GsicCs~d|dt|iƒ|idj o3dtg}|iD] }||q<~ƒdpdddid„|iDƒƒS(Nt is (cycles: t)ts cssx|]}t|ƒVqWdS(N(tstr(t.0R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pys Os (RR RRtreprtjoinR(RtindentR tx((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyRJsccCsdt|iƒS(Ns%s(RR (R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt__repr__QscCsKt|iƒ}|idj o(x%|iD]}|i|iƒq)Wn|S(s>Return a set of dependencies for this node and all its cycles.N(RRRRtupdate(Rtdepstc((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pytall_depsTs  (t__name__t __module__t__doc__RRRR%R)(((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR>s     t_EdgeCollectioncBsheZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z d „Z d „Z RS( sA collection of directed edges.cCs(titƒ|_titƒ|_dS(N(Rt defaultdictRtparent_to_childrentchild_to_parents(R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR`scCsH|\}}|i|i|ƒ|i|i|ƒ|ii|ƒdS(sAdd an edge to this collection.N(R/taddR0R(Rtedget parentnodet childnode((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR1ds cCsO|\}}|i|i|ƒ|i|i|ƒ|i|p|SdSdS(sgRemove an edge from this collection. Return the childnode if it has no other parents. N(R/tremoveR0R(RR2R3R4((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR5ls  cCs!||ijot|i|ƒS(N(R0tbool(Rtnode((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt has_parentszscCsE||ijo-g}|i|D]}|||fq"~SgSdS(N(R/(RR7R tchild((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pytedges_by_parent}s-cCs |iiƒS(N(R/tkeys(R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt get_parentsƒsccsf|ii|dƒ}|dj o@x=|D]1}|i|i|ƒ|i|p |Vq)q)WndS(s³Remove all edges where the given node is a parent. Return the collection of all nodes which were children of the given node, and have no further parents. N(R/tpopRR0R5(RR7RR9((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pytpop_node†s cCstd„|iiƒDƒƒS(Ncssx|]}t|ƒVqWdS(N(tlen(R R$((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pys •s (tsumR/tvalues(R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt__len__”sccs@x9|iiƒD](\}}x|D]}||fVq#WqWdS(N(R/t iteritems(RtparentRR9((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyt__iter__—s  cCstt|ƒƒS(N(R!tlist(R((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR%œs( R*R+R,RR1R5R8R:R<R>RBRER%(((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyR-]s         cCs@h}tƒ}x‡t|ƒg}|D]}||dq&~g}|D]}||dqI~D]3} t| ƒ|jot| ƒ} | || (R R RRtnodestedgesR ttRR R7RR4R3tqueuetoutputtcycletleadR2tk((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyRŸst S         #$  c Cs |pdSg}xRt|ƒD]D}t|gƒ}|idj ot|iƒ}n tƒ}|iƒ}|oÚx×tt|ƒdddƒD]µ}||\}}} |i|ƒp| i|iƒoy|i g} |ipgD]} | | i qç~ |i fg|i dd+|i |ƒ|i | ƒg|||d+qqWn|i |||fƒqW|i ƒd} g} |D]P}| |di g}|dipgD]} || i q®~|di fq‚~ | i dd+| i g}| ipgD]} || i q~| i fS(sîGiven a list of nodes from a topological sort, organize the nodes into a tree structure, with as many non-dependent nodes set as siblings to each other as possible. returns nodes as 3-tuples (item, cycles, children). iiÿÿÿÿiN(RtreversedRRR)txrangeR?t intersectionRR RR&RIR=(RLt independentsR7tsubtreeRt nodealldepstindexR9t childsubtreet childcyclesR RtheadRtit_[3]t_[4]((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyRÚs.   #J  nc#sÑtƒ‰h‰dd‡‡‡‡fd†‰xˆiƒD]}ˆ|ƒq:Wtd„ˆiƒDƒƒiƒ}xZ|D]R}g}ˆD]3}|d|jo|d|jo ||qˆqˆ~}|VqwWdS(Nc s-|djo|}g}n||jotSx÷ˆi|ƒD]æ\}}||joq?n|i|ƒˆ|||ƒoœt|ƒ}x|D]}ˆi|ƒ|ˆjoWˆ|}g}|D]} ||i| ƒqÇ~x|D]} |ˆ| )s ii(RRR<tdictRA(RMRDt unique_cyclesRQR R2tedgecollection((RgRMRfRs:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyRJ s  "6N(R,tsqlalchemy.excRt sqlalchemyRt__all__RRRRtobjectRR-RRRJ(((s:/usr/lib/python2.6/site-packages/sqlalchemy/topological.pyts  B; /