1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
U
K±dZã@s<dZddlmZddlmZddlmZGdd„deƒZdS)zá
altgraph.ObjectGraph - Graph of objects with an identifier
==========================================================
 
A graph of objects that have a "graphident" attribute.
graphident is the key for the object in the graph
é)Ú
GraphError)ÚGraph)Ú filter_stackc@s¶eZdZdZd,dd„Zdd„Zd-dd    „Zd
d „Zd d „Zdd„Z    dd„Z
dd„Z dd„Z dd„Z dd„Zdd„Zdd„Zdd„Zd d!„Zd.d"d#„Zd$d%„Zd&d'„Zd(d)„Zd*d+„ZdS)/Ú ObjectGraphzv
    A graph of objects that have a "graphident" attribute.
    graphident is the key for the object in the graph
    NrcCs6|dkrtƒ}||_||_||_d|_| |d¡dS)Nr)rÚ
graphidentÚgraphÚdebugÚindentÚadd_node)Úselfrr©r úKd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\altgraph/ObjectGraph.pyÚ__init__szObjectGraph.__init__cCsdt|ƒjfS)Nz<%s>)ÚtypeÚ__name__©r r r r Ú__repr__szObjectGraph.__repr__cCs&|dkr |}| |¡}|jj||dS)z–
        Iterate over the subgraph that is entirely reachable by condition
        starting from the given start node or the ObjectGraph root
        N)ÚstartÚ    condition)Ú getRawIdentrZiterdata)r rrr r r Úflatten s
zObjectGraph.flattenccs2|jD]&}|j |¡}|dk    r|j |¡VqdS©N)rÚ    node_data)r ÚidentÚnoder r r Únodes*s
 zObjectGraph.nodescsJ|dkr ˆ}ˆ |¡}ˆj |¡\}}}}‡fdd„}||dƒ||dƒfS)Nc3sHtƒ}‡fdd„|DƒD]*}||}||krˆ |¡V| |¡qdS)Nc3s|]}ˆj |¡VqdSr)rZ describe_edge)Ú.0Úerr r Ú    <genexpr>8sz<ObjectGraph.get_edges.<locals>.iter_edges.<locals>.<genexpr>)ÚsetÚfindNodeÚadd)ÚlstÚnÚseenZtplrrr r Ú
iter_edges6s  z)ObjectGraph.get_edges.<locals>.iter_edgeséé)rrZ describe_node)r rrÚ_ZoutrawZincrawr%r rr Ú    get_edges0s 
 zObjectGraph.get_edgescCs:|dkr |}| |¡}| |¡}|j ||¡}|j |¡Sr)rrÚ edge_by_nodeÚ    edge_data)r ÚfromNodeÚtoNoderÚstopÚedger r r ÚedgeData@s 
 
zObjectGraph.edgeDatacCs@|dkr |}| |¡}| |¡}|j ||¡}|j ||¡dSr)rrr*Zupdate_edge_data)r r,r-r0rr.r/r r r ÚupdateEdgeDataHs 
 
zObjectGraph.updateEdgeDatacCsdt|j||ƒ\}}}|D]\}}|jj||ddq|D]}|j |¡q8t|ƒdt|ƒt|ƒfS)zþ
        Filter the ObjectGraph in-place by removing all edges to nodes that
        do not match every filter in the given filter list
 
        Returns a tuple containing the number of:
            (nodes_visited, nodes_removed, nodes_orphaned)
        Zorphan©r+é)rrÚadd_edgeÚ    hide_nodeÚlen)r ÚfiltersÚvisitedZremovesZorphansZ    last_goodÚtailrr r r Ú filterStackPs  zObjectGraph.filterStackcCs"| |¡}|dk    r|j |¡dS)zC
        Remove the given node from the graph if it exists
        N)ÚgetIdentrr5©r rrr r r Ú
removeNodebs
zObjectGraph.removeNodecCsZ|dkr |}| |¡}| |¡}|dk    rV|dk    rV|j ||¡}|dkrHqV|j |¡q0dS)z:
        Remove all edges from fromnode to tonode
        N)r;rr*Z    hide_edge)r ÚfromnodeÚtonodeÚ    fromidentÚtoidentr/r r r ÚremoveReferencejs
 
zObjectGraph.removeReferencecCs2| |¡}|dk    r|S| |¡}|dkr,dS|jS)z5
        Get the graph identifier for a node
        N)rr rr<r r r r;ys
 
zObjectGraph.getIdentcCs||kr |St|ddƒ}|S)z6
        Get the identifier for a node object
        rN)Úgetattrr<r r r r…s zObjectGraph.getRawIdentcCs| |¡dk    Sr)r ©r rr r r Ú __contains__ŽszObjectGraph.__contains__cCs@| |¡}|dkr|}z|j |¡WStk
r:YdSXdS)z,
        Find the node on the graph
        N)rrrÚKeyErrorr<r r r r ‘s
zObjectGraph.findNodecCsJ| dd|¡z|j |j¡Wn$tk
rD|j |j|¡YnXdS)z@
        Add a node to the graph referenced by the root
        éÚaddNodeN)ÚmsgrZ restore_noderrr
rDr r r rHs
zObjectGraph.addNodecCs^|dkr |}| |¡| |¡}}|dks2|dkr6dS| dd|||¡|jj|||ddS)z<
        Create a reference from fromnode to tonode
        NrGÚcreateReferencer2)r;rIrr4)r r>r?r+r@rAr r r rJ¨szObjectGraph.createReferencecOs0| |¡}|dkr,||f|ž|Ž}| |¡|S)zl
        Add a node of type cls to the graph if it does not already exist
        by the given name
        N)r rH)r ÚclsÚnameÚargsÚkwÚmr r r Ú
createNode´s
 
 
zObjectGraph.createNodec    Gs6|r2||jkr2tdd|j|d tt|ƒ¡fƒdS)z<
        Print a debug message with the given level
        z%s%s %sz  ú N)rÚprintr    ÚjoinÚmapÚrepr©r ÚlevelÚsrMr r r rI¿szObjectGraph.msgcGs,||jkr(|j||f|žŽ|jd|_dS)z2
        Print a debug message and indent
        r3N)rrIr    rVr r r ÚmsginÆs
zObjectGraph.msgincGs,||jkr(|jd|_|j||f|žŽdS)z2
        Dedent and print a debug message
        r3N)rr    rIrVr r r ÚmsgoutÎs
 zObjectGraph.msgout)Nr)NN)N)rÚ
__module__Ú __qualname__Ú__doc__rrrrr)r0r1r:r=rBr;rrEr rHrJrPrIrYrZr r r r rs*
    
 
      
 rN)    r]ZaltgraphrZaltgraph.GraphrZaltgraph.GraphUtilrÚobjectrr r r r Ú<module>s