/usr/share/pyshared/pygraphviz/agraph.py is in python-pygraphviz 1.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 | # -*- coding: utf-8 -*-
"""
A Python interface to Graphviz.
"""
# Copyright (C) 2006-2011 by
# Aric Hagberg <hagberg@lanl.gov>
# Dan Schult <dschult@colgate.edu>
# Manos Renieris, http://www.cs.brown.edu/~er/
# Distributed with BSD license.
# All rights reserved, see LICENSE for details.
import re
import shlex
import subprocess
import sys
import threading
import warnings
import UserDict
import graphviz as gv
_DEFAULT_ENCODING = 'UTF-8'
def is_string_like(obj): # from John Hunter, types-free version
try:
obj + ''
except (TypeError, ValueError):
return False
return True
class PipeReader(threading.Thread):
"""Read and write pipes using threads.
"""
def __init__(self, result, pipe):
threading.Thread.__init__(self)
self.result = result
self.pipe = pipe
def run(self):
try:
while True:
chunk = self.pipe.read()
if not chunk:
break
self.result.append( chunk )
finally:
self.pipe.close()
class _Action:
find, create = 0,1
class AGraph(object):
"""Class for Graphviz agraph type.
Example use
>>> from pygraphviz import *
>>> G=AGraph()
>>> G=AGraph(directed=True)
>>> G=AGraph("file.dot") # doctest: +SKIP
Graphviz graph keyword parameters are processed so you may add
them like
>>> G=AGraph(landscape='true',ranksep='0.1')
or alternatively
>>> G=AGraph()
>>> G.graph_attr.update(landscape='true',ranksep='0.1')
and
>>> G.node_attr.update(color='red')
>>> G.edge_attr.update(len='2.0',color='blue')
See http://www.graphviz.org/doc/info/attrs.html
for a list of attributes.
Keyword parameters:
thing is a generic input type (filename, string, handle to pointer,
dictionary of dictionaries). An attempt is made to automaticaly
detect the type so you may write for example:
>>> d={'1': {'2': None}, '2': {'1': None, '3': None}, '3': {'2': None}}
>>> A=AGraph(d)
>>> s=A.to_string()
>>> B=AGraph(s)
>>> h=B.handle
>>> C=AGraph(h)
Parameters::
name: Name for the graph
strict: True|False (True for simple graphs)
directed: True|False
data: Dictionary of dictionaries or dictionary of lists
representing nodes or edges to load into intial graph
string: String containing a dot format graph
handle: Swig pointer to an agraph_t data structure
"""
def __init__(self, thing=None,
filename=None, data=None, string=None, handle=None,
name='', strict=True, directed=False, **attr):
self.handle=None # assign first in case the __init__ bombs
# initialization can take no arguments (gives empty graph) or
# a file name
# a string of graphviz dot language
# a swig pointer (handle) to a graph
# a dict of dicts (or dict of lists) data structure
# backward compability
filename=attr.get('file',filename)
# guess input type if specified as first (nonkeyword) argument
if thing is not None:
# can't specify first argument and also file,data,string,handle
filename=None
data=None
string=None
handle=None
if isinstance(thing,dict):
data=thing # a dictionary of dictionaries (or lists)
elif hasattr(thing,'own'): # a Swig pointer - graph handle
handle=thing
elif self._is_string_like(thing):
pattern=re.compile('(strict)?\s*(graph|digraph).*{.*}\s*',
re.DOTALL)
if pattern.match(thing):
string=thing # this is a dot format graph in a string
else:
filename=thing # assume this is a file name
else:
raise TypeError('Unrecognized input %s'%thing)
if handle is not None:
# if handle was specified, reference it
self.handle = handle
elif filename is not None:
# load new graph from file (creates self.handle)
self.read(filename)
elif string is not None:
# load new graph from string (creates self.handle)
# get the charset from the string to properly encode it for
# writing to the temporary file in from_string()
match = re.search(r'charset\s*=\s*"([^"]+)"', string)
if match is not None:
self.encoding = match.group(1)
else:
self.encoding = _DEFAULT_ENCODING
self.from_string(string)
else:
# no handle, need to
self.handle = None
if self.handle is not None:
# the handle was specified or created
# get the encoding from the "charset" graph attribute
item=gv.agget(self.handle,'charset')
if item is not None:
self.encoding = item
else:
self.encoding = _DEFAULT_ENCODING
else:
# no handle was specified or created
# get encoding from the "charset" kwarg
self.encoding = attr.get('charset', _DEFAULT_ENCODING)
try:
if name is None:
name = ''
# instantiate a new, empty graph
self.handle=gv.agraphnew(name.encode(self.encoding),
strict,directed)
except TypeError:
raise TypeError("Graph name must be a string: %s"%name)
# encoding is already set but if it was specified explicitly
# as an attr, then set it explicitly for the graph
if 'charset' in attr:
gv.agattr_label(self.handle,0, 'charset', self.encoding)
# if data is specified, populate the newly created graph
if data is not None:
# load from dict of dicts or dict of lists
for node in data:
for nbr in data[node]:
self.add_edge(node,nbr)
self.add_nodes_from(data.keys())
# throw away the charset attribute, if one exists,
# since we've already set it, and now it should not be changed
if 'charset' in attr:
del attr['charset']
# assign any attributes specified through keywords
self.graph_attr=Attribute(self.handle,0) # default graph attributes
self.graph_attr.update(attr) # apply attributes passed to init
self.node_attr=Attribute(self.handle,1) # default node attributes
self.edge_attr=Attribute(self.handle,2) # default edge attribtes
def __str__(self):
return unicode(self).encode(self.encoding,'replace')
def __unicode__(self):
return self.string()
def __repr__(self):
name=gv.agnameof(self.handle)
if name is None:
return '<AGraph %s>' %(self.handle)
return '<AGraph %s %s>' %(name,self.handle)
def __eq__(self,other):
# two graphs are equal if they have exact same string representation
# this is not graph isomorphism
return self.string()==other.string()
def __hash__(self):
# hash the string representation for id
return hash(self.string())
def __iter__(self):
# provide "for n in G"
return self.nodes_iter()
def __contains__(self,n):
# provide "n in G"
return self.has_node(n)
def __len__(self):
return self.number_of_nodes()
def __getitem__(self,n):
# "G[n]" returns nodes attached to n
return self.neighbors(n)
# not implemented, but could be...
# def __setitem__(self,u,v):
# self.add_edge(u,v)
def get_name(self):
name=gv.agnameof(self.handle)
if name is not None:
name=name.decode(self.encoding)
return name
name=property(get_name)
def add_node(self, n, **attr):
"""Add a single node n.
If n is not a string, conversion to a string will be attempted.
String conversion will work if n has valid string representation
(try str(n) if you are unsure).
>>> G=AGraph()
>>> G.add_node('a')
>>> G.nodes()
[u'a']
>>> G.add_node(1) # will be converted to a string
>>> G.nodes()
[u'a', u'1']
Attributes can be added to nodes on creation
(attribute values must be strings)
>>> G.add_node(2,color='red')
See http://www.graphviz.org/doc/info/attrs.html
for a list of attributes.
Anonymous Graphviz nodes are currently not implemented.
"""
if not self._is_string_like(n):
n=str(n)
n=n.encode(self.encoding)
try:
nh=gv.agnode(self.handle,n,_Action.find)
except KeyError:
nh=gv.agnode(self.handle,n,_Action.create)
node=Node(self,nh=nh)
node.attr.update(**attr)
def add_nodes_from(self, nbunch, **attr):
"""Add nodes from a container nbunch.
nbunch can be any iterable container such as a list or dictionary
>>> G=AGraph()
>>> nlist=['a','b',1,'spam']
>>> G.add_nodes_from(nlist)
>>> sorted(G.nodes())
[u'1', u'a', u'b', u'spam']
Attributes can be added to nodes on creation
>>> G.add_nodes_from(nlist, color='red') # set all nodes in nlist red
"""
for n in nbunch:
self.add_node(n,**attr)
def remove_node(self,n):
"""Remove the single node n.
Attempting to remove a node that isn't in the graph will produce
an error.
>>> G=AGraph()
>>> G.add_node('a')
>>> G.remove_node('a')
"""
if not self._is_string_like(n):
n=str(n)
n=n.encode(self.encoding)
try:
nh=gv.agnode(self.handle,n.encode(self.encoding),_Action.find)
gv.agdelnode(self.handle,nh)
except KeyError:
raise KeyError("Node %s not in graph."%n.decode(self.encoding))
delete_node=remove_node
def remove_nodes_from(self,nbunch):
"""Remove nodes from a container nbunch.
nbunch can be any iterable container such as a list or dictionary
>>> G=AGraph()
>>> nlist=['a','b',1,'spam']
>>> G.add_nodes_from(nlist)
>>> G.remove_nodes_from(nlist)
"""
for n in nbunch:
self.remove_node(n)
delete_nodes_from=remove_nodes_from
def nodes_iter(self):
"""Return an iterator over all the nodes in the graph.
Note: modifying the graph structure while iterating over
the nodes may produce unpredictable results. Use nodes()
as an alternative.
"""
nh=gv.agfstnode(self.handle)
while nh is not None:
yield Node(self,nh=nh)
nh=gv.agnxtnode(self.handle,nh)
raise StopIteration
iternodes=nodes_iter
def nodes(self):
"""Return a list of all nodes in the graph."""
return list(self.nodes_iter())
def number_of_nodes(self):
"""Return the number of nodes in the graph."""
return gv.agnnodes(self.handle)
def order(self):
"""Return the number of nodes in the graph."""
return self.number_of_nodes()
def has_node(self,n):
"""Return True if n is in the graph or False if not.
>>> G=AGraph()
>>> G.add_node('a')
>>> G.has_node('a')
True
>>> 'a' in G # same as G.has_node('a')
True
"""
try:
node=Node(self,n)
return True
except KeyError:
return False
def get_node(self,n):
"""Return a node object (Node) corresponding to node n.
>>> G=AGraph()
>>> G.add_node('a')
>>> node=G.get_node('a')
>>> print node
a
"""
return Node(self,n)
def add_edge(self,u,v=None,key=None,**attr):
"""Add a single edge between nodes u and v.
If the nodes u and v are not in the graph they will added.
If u and v are not strings, conversion to a string will be attempted.
String conversion will work if u and v have valid string representation
(try str(u) if you are unsure).
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.edges()
[(u'a', u'b')]
The optional key argument allows assignment of a key to the
edge. This is especially useful to distinguish between
parallel edges in multi-edge graphs (strict=False).
>>> G=AGraph(strict=False)
>>> G.add_edge('a','b','first')
>>> G.add_edge('a','b','second')
>>> sorted(G.edges(keys=True))
[(u'a', u'b', u'first'), (u'a', u'b', u'second')]
Attributes can be added when edges are created
>>> G.add_edge(u'a',u'b',color='green')
Attributes must be valid strings.
See http://www.graphviz.org/doc/info/attrs.html
for a list of attributes.
"""
if v is None: (u,v)=u # no v given, assume u is an edge tuple
try:
uh=Node(self,u).handle
except:
self.add_node(u)
uh=Node(self,u).handle
try:
vh=Node(self,v).handle
except:
self.add_node(v)
vh=Node(self,v).handle
try:
if key is not None:
if not self._is_string_like(key):
key=str(key)
key=key.encode(self.encoding)
eh=gv.agedge(self.handle,uh,vh,key,_Action.create)
e=Edge(self,eh=eh)
e.attr.update(**attr)
except KeyError:
return None # silent failure for strict graph, already added
def add_edges_from(self, ebunch, **attr):
"""Add nodes to graph from a container ebunch.
ebunch is a container of edges such as a list or dictionary.
>>> G=AGraph()
>>> elist=[('a','b'),('b','c')]
>>> G.add_edges_from(elist)
Attributes can be added when edges are created
>>> G.add_edges_from(elist, color='green')
"""
for e in ebunch:
self.add_edge(e,**attr)
def get_edge(self, u, v, key=None):
"""Return an edge object (Edge) corresponding to edge (u,v).
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> edge=G.get_edge('a','b')
>>> print edge
(u'a', u'b')
With optional key argument will only get edge matching (u,v,key).
"""
return Edge(self,u,v,key)
def remove_edge(self, u, v=None, key=None):
"""Remove edge between nodes u and v from the graph.
With optional key argument will only remove an edge
matching (u,v,key).
"""
if v is None:
(u,v)=u # no v given, assume u is an edge tuple
e=Edge(self,u,v,key)
try:
gv.agdeledge(self.handle,e.handle)
except KeyError:
raise KeyError("Edge %s-%s not in graph."%(u,v))
delete_edge=remove_edge
def remove_edges_from(self, ebunch):
"""Remove edges from ebunch (a container of edges)."""
for e in ebunch:
self.remove_edge(e)
delete_edges_from=remove_edges_from
def has_edge(self, u, v=None, key=None):
"""Return True an edge u-v is in the graph or False if not.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.has_edge('a','b')
True
Optional key argument will restrict match to edges (u,v,key).
"""
if v is None:
(u,v)=u # no v given, assume u is an edge tuple
try:
Edge(self,u,v,key)
return True
except KeyError:
return False
def edges(self, nbunch=None, keys=False):
"""Return list of edges in the graph.
If the optional nbunch (container of nodes) only edges
adjacent to nodes in nbunch will be returned.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.add_edge('c','d')
>>> print sorted(G.edges())
[(u'a', u'b'), (u'c', u'd')]
>>> print G.edges('a')
[(u'a', u'b')]
"""
return list(self.edges_iter(nbunch=nbunch,keys=keys))
def has_neighbor(self, u, v, key=None):
"""Return True if u has an edge to v or False if not.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> G.has_neighbor('a','b')
True
Optional key argument will only find edges (u,v,key).
"""
return self.has_edge(u,v)
def neighbors_iter(self,n):
"""Return iterator over the nodes attached to n.
Note: modifying the graph structure while iterating over
node neighbors may produce unpredictable results. Use neighbors()
as an alternative.
"""
n=Node(self,n)
nh=n.handle
eh=gv.agfstedge(self.handle,nh)
while eh is not None:
(s,t)=Edge(self,eh=eh)
if s==n:
yield Node(self,t)
else:
yield Node(self,s)
eh=gv.agnxtedge(self.handle,eh,nh)
raise StopIteration
def neighbors(self, n):
"""Return a list of the nodes attached to n."""
return list(self.neighbors_iter(n))
iterneighbors=neighbors_iter
def out_edges_iter(self, nbunch=None, keys=False):
"""Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges
adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over
edges may produce unpredictable results. Use out_edges()
as an alternative.
"""
if nbunch is None: # all nodes
nh=gv.agfstnode(self.handle)
while nh is not None:
eh=gv.agfstout(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtout(self.handle,eh)
nh=gv.agnxtnode(self.handle,nh)
elif nbunch in self: # if nbunch is a single node
n=Node(self,nbunch)
nh=n.handle
eh=gv.agfstout(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtout(self.handle,eh)
else: # if nbunch is a sequence of nodes
try: bunch=[n for n in nbunch if n in self]
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
for n in nbunch:
try:
nh=Node(self,n).handle
except KeyError:
continue
eh=gv.agfstout(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtout(self.handle,eh)
raise StopIteration
iteroutedges=out_edges_iter
iteredges=out_edges_iter
def in_edges_iter(self, nbunch=None, keys=False):
"""Return iterator over out edges in the graph.
If the optional nbunch (container of nodes) only out edges
adjacent to nodes in nbunch will be returned.
Note: modifying the graph structure while iterating over
edges may produce unpredictable results. Use in_edges()
as an alternative.
"""
if nbunch is None: # all nodes
nh=gv.agfstnode(self.handle)
while nh is not None:
eh=gv.agfstin(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtin(self.handle,eh)
nh=gv.agnxtnode(self.handle,nh)
elif nbunch in self: # if nbunch is a single node
n=Node(self,nbunch)
nh=n.handle
eh=gv.agfstin(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtin(self.handle,eh)
else: # if nbunch is a sequence of nodes
try: bunch=[n for n in nbunch if n in self]
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
for n in nbunch:
try:
nh=Node(self,n).handle
except KeyError:
continue
eh=gv.agfstin(self.handle,nh)
while eh is not None:
e=Edge(self,eh=eh)
if keys:
yield (e[0],e[1],e.name)
else:
yield e
eh=gv.agnxtin(self.handle,eh)
raise StopIteration
iterinedges=in_edges_iter
# define edges to be out_edges implicitly since edges uses edges_iter
edges_iter=out_edges_iter
def out_edges(self, nbunch=None, keys=False):
"""Return list of out edges in the graph.
If the optional nbunch (container of nodes) only out edges
adjacent to nodes in nbunch will be returned.
"""
return list(self.out_edges_iter(nbunch=nbunch,keys=keys))
def in_edges(self, nbunch=None, keys=False):
"""Return list of in edges in the graph.
If the optional nbunch (container of nodes) only in edges
adjacent to nodes in nbunch will be returned.
"""
return list(self.in_edges_iter(nbunch=nbunch,keys=keys))
def predecessors_iter(self,n):
"""Return iterator over predecessor nodes of n.
Note: modifying the graph structure while iterating over
node predecessors may produce unpredictable results. Use
predecessors() as an alternative.
"""
n=Node(self,n)
nh=n.handle
eh=gv.agfstin(self.handle,nh)
while eh is not None:
(s,t)=Edge(self,eh=eh)
if s==n:
yield Node(self,t)
else:
yield Node(self,s)
eh=gv.agnxtin(self.handle,eh)
raise StopIteration
iterpred=predecessors_iter
def successors_iter(self,n):
"""Return iterator over successor nodes of n.
Note: modifying the graph structure while iterating over
node successors may produce unpredictable results. Use
successors() as an alternative.
"""
n=Node(self,n)
nh=n.handle
eh=gv.agfstout(self.handle,nh)
while eh is not None:
(s,t)=Edge(self,eh=eh)
if s==n:
yield Node(self,t)
else:
yield Node(self,s)
eh=gv.agnxtout(self.handle,eh)
raise StopIteration
itersucc=successors_iter
def successors(self, n):
"""Return list of successor nodes of n."""
return list(self.successors_iter(n))
def predecessors(self, n):
"""Return list of predecessor nodes of n."""
return list(self.predecessors_iter(n))
# digraph definitions
out_neighbors=successors
in_neighbors=predecessors
def degree_iter(self,nbunch=None,indeg=True,outdeg=True):
"""Return an iterator over the degree of the nodes given in
nbunch container.
Returns paris of (node,degree).
"""
# prepare nbunch
if nbunch is None: # include all nodes via iterator
bunch=[n for n in self.nodes_iter()]
elif nbunch in self: # if nbunch is a single node
bunch=[Node(self,nbunch)]
else: # if nbunch is a sequence of nodes
try: bunch=[Node(self,n) for n in nbunch if n in self]
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
for n in bunch:
yield (Node(self,n),gv.agdegree(self.handle,
n.handle,indeg,outdeg))
def in_degree_iter(self,nbunch=None):
"""Return an iterator over the in-degree of the nodes given in
nbunch container.
Returns paris of (node,degree).
"""
return self.degree_iter(nbunch,indeg=True,outdeg=False)
def out_degree_iter(self,nbunch=None):
"""Return an iterator over the out-degree of the nodes given in
nbunch container.
Returns paris of (node,degree).
"""
return self.degree_iter(nbunch,indeg=False,outdeg=True)
iteroutdegree=out_degree_iter
iterindegree=in_degree_iter
def out_degree(self,nbunch=None, with_labels=False):
"""Return the out-degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary
keyed by node with value set to the degree.
"""
if with_labels:
return dict(self.out_degree_iter(nbunch))
else:
dlist=list(d for n,d in self.out_degree_iter(nbunch))
if nbunch in self:
return dlist[0]
else:
return dlist
def in_degree(self,nbunch=None, with_labels=False):
"""Return the in-degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary
keyed by node with value set to the degree.
"""
if with_labels:
return dict(self.in_degree_iter(nbunch,with_labels))
else:
dlist=list(d for n,d in self.in_degree_iter(nbunch))
if nbunch in self:
return dlist[0]
else:
return dlist
def reverse(self):
"""Return copy of directed graph with edge directions reversed."""
if self.directed:
# new empty DiGraph
H=self.__class__(strict=self.strict,directed=True,name=self.name)
H.graph_attr.update(self.graph_attr)
H.node_attr.update(self.node_attr)
H.edge_attr.update(self.edge_attr)
for n in self.nodes():
H.add_node(n)
new_n=Node(H,n)
new_n.attr.update(n.attr)
for e in self.edges():
(u,v)=e
H.add_edge(v,u)
uv=H.get_edge(v,u)
uv.attr.update(e.attr)
return H
else:
return self
def degree(self,nbunch=None,with_labels=False):
"""Return the degree of nodes given in nbunch container.
Using optional with_labels=True returns a dictionary
keyed by node with value set to the degree.
"""
if with_labels:
return dict(self.degree_iter(nbunch))
else:
dlist=list(d for n,d in self.degree_iter(nbunch))
if nbunch in self:
return dlist[0]
else:
return dlist
iterdegree=degree_iter
def number_of_edges(self):
"""Return the number of edges in the graph."""
return gv.agnedges(self.handle)
def size(self):
"""Return the number of edges in the graph."""
return self.number_of_edges()
def clear(self):
"""Remove all nodes, edges, and attributes from the graph."""
self.edge_attr.clear()
self.node_attr.clear()
self.graph_attr.clear()
# now "close" existing graph and create a new graph
name=gv.agnameof(self.handle)
strict=self.strict
directed=self.directed
gv.agclose(self.handle)
self.handle=gv.agraphnew(name,strict,directed)
def close(self):
# may be useful to clean up graphviz data
# this should completely remove all of the existing graphviz data
gv.agclose(self.handle)
def copy(self):
"""Return a copy of the graph."""
from tempfile import TemporaryFile
fh = TemporaryFile()
# Cover TemporaryFile wart: on 'nt' we need the file member
if hasattr( fh, 'file' ):
fhandle = fh.file
else:
fhandle = fh
self.write( fhandle )
fh.seek( 0 )
return self.__class__( filename=fhandle )
def add_path(self, nlist):
"""Add the path of nodes given in nlist."""
fromv = nlist.pop(0)
while len(nlist) > 0:
tov=nlist.pop(0)
self.add_edge(fromv,tov)
fromv=tov
def add_cycle(self, nlist):
"""Add the cycle of nodes given in nlist."""
self.add_path(nlist+[nlist[0]])
def prepare_nbunch(self,nbunch=None):
# private function to build bunch from nbunch
if nbunch is None: # include all nodes via iterator
bunch=self.nodes_iter()
elif nbunch in self: # if nbunch is a single node
bunch=[Node(self,nbunch)]
else: # if nbunch is a sequence of nodes
try: # capture error for nonsequence/iterator entries.
bunch=[Node(self,n) for n in nbunch if n in self]
# bunch=(n for n in nbunch if n in self) # need python 2.4
except TypeError:
raise TypeError("nbunch is not a node or a sequence of nodes.")
return bunch
def add_subgraph(self, nbunch=None, name=None, **attr):
"""Return subgraph induced by nodes in nbunch.
"""
if name is not None:
name=name.encode(self.encoding)
try:
handle=gv.agsubg(self.handle,name, _Action.create)
except TypeError:
raise TypeError("Subgraph name must be a string: %s"%name.decode(self.encoding))
H=self.__class__(strict=self.strict,
directed=self.directed,
handle=handle,name=name,
**attr)
if nbunch is None: return H
# add induced subgraph on nodes in nbunch
bunch=self.prepare_nbunch(nbunch)
H.add_nodes_from(bunch)
for (u,v) in self.edges():
if u in H and v in H:
H.add_edge(u,v)
return H
def remove_subgraph(self, name):
"""Remove subgraph with given name."""
try:
handle=gv.agsubg(self.handle,name.encode(self.encoding),
_Action.find)
except TypeError:
raise TypeError("Subgraph name must be a string: %s"%name)
if handle is None:
raise KeyError("Subgraph %s not in graph."%name)
gv.agdelsubg(self.handle,handle)
delete_subgraph=remove_subgraph
subgraph=add_subgraph
def subgraph_parent(self, nbunch=None, name=None):
"""Return parent graph of subgraph or None if graph is root graph.
"""
handle=gv.agparent(self.handle)
if handle is None:
return None
H=self.__class__(strict=self.strict,
directed=self.directed,
handle=handle,
name=name)
return H
def subgraph_root(self, nbunch=None, name=None):
"""Return root graph of subgraph or None if graph is root graph.
"""
handle=gv.agroot(self.handle)
if handle is None:
return None
H=self.__class__(strict=self.strict,
directed=self.directed,
handle=handle,name=name)
return H
def get_subgraph(self,name):
"""Return existing subgraph with specified name or None if it
doesn't exist.
"""
try:
handle=gv.agsubg(self.handle,name.encode(self.encoding)
,_Action.find)
except TypeError:
raise TypeError("Subgraph name must be a string: %s"%name)
if handle is None:
return None
H=self.__class__(strict=self.strict,
directed=self.directed,
handle=handle)
return H
def subgraphs_iter(self):
"""Iterator over subgraphs."""
handle=gv.agfstsubg(self.handle)
while handle is not None:
yield self.__class__(strict=self.strict,
directed=self.directed,
handle=handle)
handle=gv.agnxtsubg(handle)
raise StopIteration
def subgraphs(self):
"""Return a list of all subgraphs in the graph."""
return list(self.subgraphs_iter())
# directed, undirected tests and conversions
def is_strict(self):
"""Return True if graph is strict or False if not.
Strict graphs do not allow parallel edges or self loops.
"""
if gv.agisstrict(self.handle)==1:
return True
else:
return False
strict=property(is_strict)
def is_directed(self):
"""Return True if graph is directed or False if not."""
if gv.agisdirected(self.handle)==1:
return True
else:
return False
directed=property(is_directed)
def is_undirected(self):
"""Return True if graph is undirected or False if not."""
if gv.agisundirected(self.handle)==1:
return True
else:
return False
def to_undirected(self):
"""Return undirected copy of graph."""
if not self.directed:
return self.copy()
else:
U=AGraph(strict=self.strict)
U.graph_attr.update(self.graph_attr)
U.node_attr.update(self.node_attr)
U.edge_attr.update(self.edge_attr)
for n in self.nodes():
U.add_node(n)
new_n=Node(U,n)
new_n.attr.update(n.attr)
for e in self.edges():
(u,v)=e
U.add_edge(u,v)
uv=U.get_edge(u,v)
uv.attr.update(e.attr)
return U
def to_directed(self,**kwds):
"""Return directed copy of graph.
Each undirected edge u-v is represented as two directed
edges u->v and v->u.
"""
if not self.directed:
D=AGraph(strict=self.strict,directed=True)
D.graph_attr.update(self.graph_attr)
D.node_attr.update(self.node_attr)
D.edge_attr.update(self.edge_attr)
for n in self.nodes():
D.add_node(n)
new_n=Node(D,n)
new_n.attr.update(n.attr)
for e in self.edges():
(u,v)=e
D.add_edge(u,v)
D.add_edge(v,u)
uv=D.get_edge(u,v)
vu=D.get_edge(v,u)
uv.attr.update(e.attr)
uv.attr.update(e.attr)
vu.attr.update(e.attr)
return D
else:
return self.copy()
# io
def read(self, path):
"""Read graph from dot format file on path.
path can be a file name or file handle
use::
G.read('file.dot')
"""
fh=self._get_fh(path)
try:
self.handle = gv.agread(fh,None)
except IOError:
print "IO error reading file"
def write(self, path=None):
"""Write graph in dot format to file on path.
path can be a file name or file handle
use::
G.write('file.dot')
"""
if path is None:
path=sys.stdout
fh=self._get_fh(path,'w')
try:
gv.agwrite(self.handle,fh)
except IOError:
print "IO error writing file"
def string_nop(self):
"""Return a string (unicode) representation of graph in dot format."""
# this will fail for graphviz-2.8 because of a broken nop
# so use tempfile version below
return self.draw(format='dot',prog='nop').decode(self.encoding)
def to_string(self):
"""Return a string (unicode) representation of graph in dot format."""
from tempfile import TemporaryFile
fh = TemporaryFile()
# Cover TemporaryFile wart: on 'nt' we need the file member
if hasattr( fh, 'file' ):
self.write( fh.file )
else:
self.write( fh )
fh.seek( 0 )
data = fh.read()
fh.close()
return data.decode(self.encoding)
def string(self):
"""Return a string (unicode) represetnation of graph in dot format."""
# return self.to_string()
return self.string_nop()
def from_string(self,string):
"""Load a graph from a string in dot format.
Overwrites any existing graph.
To make a new graph from a string use
>>> s='digraph {1 -> 2}'
>>> A=AGraph()
>>> t=A.from_string(s)
>>> A=AGraph(string=s) # specify s is a string
>>> A=AGraph(s) # s assumed to be a string during initialization
"""
# allow either unicode or encoded string
try:
string = string.decode(self.encoding)
except UnicodeEncodeError:
pass
from tempfile import TemporaryFile
fh = TemporaryFile()
fh.write(string.encode(self.encoding))
fh.seek(0)
# Cover TemporaryFile wart: on 'nt' we need the file member
if hasattr(fh, 'file'):
self.read(fh.file)
else:
self.read(fh)
fh.close()
return self
def _get_prog(self,prog):
# private: get path of graphviz program
try:
gvprogs=dict.fromkeys(\
['neato','dot','twopi','circo','fdp','nop',
'wc','acyclic','gvpr','gvcolor','ccomps','sccmap','tred',
'sfdp'])
p=gvprogs[prog]
except KeyError:
raise ValueError("Program %s is not one of: %s."%\
(prog,', '.join(gvprogs.keys())))
try: # user must pick one of the graphviz programs...
runprog = self._which(prog)
except:
raise ValueError("Program %s not found in path."%prog)
return runprog
def _run_prog(self,prog='nop',args=''):
"""Apply graphviz program to graph and return the result as a string.
>>> A=AGraph()
>>> s=A._run_prog() # doctest: +SKIP
>>> s=A._run_prog(prog='acyclic') # doctest: +SKIP
Use keyword args to add additional arguments to graphviz programs.
"""
runprog=r'"%s"'%self._get_prog(prog)
cmd=' '.join([runprog,args])
dotargs = shlex.split(cmd)
p = subprocess.Popen(dotargs,
shell=False,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=False)
(child_stdin,
child_stdout,
child_stderr) = (p.stdin, p.stdout, p.stderr)
# Use threading to avoid blocking
data = []
errors = []
threads = [PipeReader(data, child_stdout),
PipeReader(errors, child_stderr)]
for t in threads:
t.start()
self.write(child_stdin)
child_stdin.close()
for t in threads:
t.join()
if data==[]:
raise IOError("".join(errors))
if len(errors)>0:
warnings.warn("".join(errors),RuntimeWarning)
return "".join(data)
def layout(self,prog='neato',args=''):
"""Assign positions to nodes in graph.
Optional prog=['neato'|'dot'|'twopi'|'circo'|'fdp'|'nop']
will use specified graphviz layout method.
>>> A=AGraph()
>>> A.layout() # uses neato
>>> A.layout(prog='dot')
Use keyword args to add additional arguments to graphviz programs.
The layout might take a long time on large graphs.
"""
import os
from tempfile import TemporaryFile
fmt='dot'
data=self._run_prog(prog,' '.join([args,"-T",fmt]))
self.from_string(data)
self.has_layout=True
return
def tred(self,args='',copy=False):
"""Transitive reduction of graph. Modifies existing graph.
To create a new graph use
>>> A=AGraph()
>>> B=A.tred(copy=True) # doctest: +SKIP
See the graphviz "tred" program for details of the algorithm.
"""
data=self._run_prog('tred',args)
if copy:
return self.__class__(string=data)
else:
return self.from_string(data)
def acyclic(self,args='',copy=False):
"""Reverse sufficient edges in digraph to make graph acyclic.
Modifies existing graph.
To create a new graph use
>>> A=AGraph()
>>> B=A.acyclic(copy=True) # doctest: +SKIP
See the graphviz "acyclic" program for details of the algorithm.
"""
data=self._run_prog('acyclic',args)
if copy:
return self.__class__(string=data)
else:
return self.from_string(data)
def draw(self,path=None,format=None,prog=None,args=''):
"""Output graph to path in specified format.
An attempt will be made to guess the output format based on the file
extension of `path`. If that fails, then the `format` parameter will
be used.
Note, if `path` is a file object returned by a call to os.fdopen(),
then the method for discovering the format will not work. In such
cases, one should explicitly set the `format` parameter; otherwise, it
will default to 'dot'.
Formats (not all may be available on every system depending on
how Graphviz was built)
'canon', 'cmap', 'cmapx', 'cmapx_np', 'dia', 'dot',
'fig', 'gd', 'gd2', 'gif', 'hpgl', 'imap', 'imap_np',
'ismap', 'jpe', 'jpeg', 'jpg', 'mif', 'mp', 'pcl', 'pdf',
'pic', 'plain', 'plain-ext', 'png', 'ps', 'ps2', 'svg',
'svgz', 'vml', 'vmlz', 'vrml', 'vtx', 'wbmp', 'xdot', 'xlib'
If prog is not specified and the graph has positions
(see layout()) then no additional graph positioning will
be performed.
Optional prog=['neato'|'dot'|'twopi'|'circo'|'fdp'|'nop']
will use specified graphviz layout method.
>>> G=AGraph()
>>> G.layout()
# use current node positions, output ps in 'file.ps'
>>> G.draw('file.ps')
# use dot to position, output png in 'file'
>>> G.draw('file', format='png',prog='dot')
# use keyword 'args' to pass additional arguments to graphviz
>>> G.draw('test.ps',prog='twopi',args='-Gepsilon=1')
The layout might take a long time on large graphs.
"""
import os
# try to guess format from extension
if format is None and path is not None:
fh=self._get_fh(path,'w+b')
format=os.path.splitext(fh.name)[-1].lower()[1:]
if format is None or format=='':
format = 'dot'
if prog is None:
try:
self.has_layout==True
prog='neato'
args+="-n2"
except:
raise AttributeError(\
"""Graph has no layout information, see layout()
or specify prog=%s."""%\
("|".join(['neato','dot','twopi','circo','fdp','nop'])))
else:
if self.number_of_nodes()>1000:
sys.stderr.write(\
"Warning: graph has %s nodes...layout may take a long time.\n"%\
self.number_of_nodes())
if prog=='nop': # nop takes no switches
args=''
else:
args=' '.join([args,"-T"+format])
data=self._run_prog(prog,args)
if path is not None:
fh=self._get_fh(path,'w+b')
fh.write("".join(data))
if self._is_string_like(path):
fh.close()
d=None
else:
d="".join( data )
return d
# some private helper functions
def _is_string_like(self,obj): # from John Hunter, types-free version
try:
obj + ''
except (TypeError, ValueError):
return False
return True
def _get_fh(self, path, mode='r'):
""" Return a file handle for given path.
Path can be a string or a file handle.
Attempt to uncompress/compress files ending in '.gz' and '.bz2'.
"""
import os
if self._is_string_like(path):
if path.endswith('.gz'):
# import gzip
# fh = gzip.open(path,mode=mode) # doesn't return real fh
fh=os.popen("gzcat "+path) # probably not portable
elif path.endswith('.bz2'):
# import bz2
# fh = bz2.BZ2File(path,mode=mode) # doesn't return real fh
fh=os.popen("bzcat "+path) # probably not portable
else:
fh = file(path,mode=mode)
elif hasattr(path, 'write'):
# Note, mode of file handle is unchanged.
fh = path
else:
raise TypeError('path must be a string or file handle.')
return fh
def _which(self,name):
"""Searches for name in exec path and returns full path"""
import os
import glob
paths = os.environ["PATH"]
if os.name == "nt":
exe = ".exe"
else:
exe = ""
for path in paths.split(os.pathsep):
match=glob.glob(os.path.join(path, name+exe))
if match:
return match[0]
raise ValueError("No prog %s in path."%name)
class Node(unicode):
"""Node object based on unicode.
If G is a graph
>>> G=AGraph()
then
>>> G.add_node(1)
will create a node object labeled by the string "1".
To get the object use
>>> node=Node(G,1)
or
>>> node=G.get_node(1)
The node object is derived from a string and can be manipulated as such.
Each node has attributes that can be directly accessed through
the attr dictionary:
>>> node.attr['color']='red'
"""
def __new__(self,graph,name=None,nh=None):
if nh is not None:
n=unicode.__new__(self,gv.agnameof(nh),graph.encoding)
else:
n=unicode.__new__(self,name)
try:
nh=gv.agnode(graph.handle,n.encode(graph.encoding),_Action.find)
except KeyError:
raise KeyError("Node %s not in graph."%n)
n.ghandle=graph.handle
n.attr=ItemAttribute(nh,1)
n.handle=nh
n.encoding=graph.encoding
return n
def get_handle(self):
"""Return pointer to graphviz node object."""
return gv.agnode(self.ghandle,self.encode(self.encoding),_Action.find)
# handle=property(get_handle)
def get_name(self):
name=gv.agnameof(self.handle)
if name is not None:
name=name.decode(self.encoding)
return name
name=property(get_name)
class Edge(tuple):
"""Edge object based on tuple.
If G is a graph
>>> G=AGraph()
then
>>> G.add_edge(1,2)
will add the edge 1-2 to the graph.
>>> edge=Edge(G,1,2)
or
>>> edge=G.get_edge(1,2)
will get the edge object.
An optional key can be used
>>> G.add_edge(2,3,'spam')
>>> edge=Edge(G,2,3,'spam')
The edge is represented as a tuple (u,v) or (u,v,key)
and can be manipulated as such.
Each edge has attributes that can be directly accessed through
the attr dictionary:
>>> edge.attr['color']='red'
"""
def __new__(self,graph,source=None,target=None,key=None,eh=None):
# edge handle given, reconstruct node object
if eh is not None:
(source,target)=(gv.agtail(eh),gv.aghead(eh))
s=Node(graph,nh=source)
t=Node(graph,nh=target)
# no edge handle, search for edge and construct object
else:
s=Node(graph,source)
t=Node(graph,target)
if key is not None:
if not is_string_like(key):
key=str(key)
key=key.encode(graph.encoding)
try:
eh=gv.agedge(graph.handle,
s.handle,
t.handle,
key,
_Action.find)
except KeyError:
raise KeyError("Edge %s-%s not in graph."%(source,target))
tp=tuple.__new__(self,(s,t))
tp.ghandle=graph.handle
tp.handle=eh
tp.attr=ItemAttribute(eh,3)
tp.encoding=graph.encoding
return tp
def get_name(self):
name=gv.agnameof(self.handle)
if name is not None:
name=name.decode(self.encoding)
return name
name=property(get_name)
key=property(get_name)
class Attribute(UserDict.DictMixin):
"""Default attributes for graphs.
Assigned on initialization of AGraph class.
and manipulated through the class data.
>>> G=AGraph() # initialize, G.graph_attr, G.node_attr, G.edge_attr
>>> G.graph_attr['splines']='true'
>>> G.node_attr['shape']='circle'
>>> G.edge_attr['color']='red'
See
http://graphviz.org/doc/info/attrs.html
for a list of all attributes.
"""
# use for graph, node, and edge default attributes
# atype:graph=0, node=1,edge=3
def __init__(self,handle,atype):
self.handle=handle
self.type=atype
# get the encoding
ghandle=gv.agraphof(handle)
root_handle=gv.agroot(ghandle) # get root graph
try:
ah=gv.agattr(root_handle,0,'charset',None)
self.encoding=gv.agattrdefval(ah)
except KeyError:
self.encoding=_DEFAULT_ENCODING
def __setitem__(self, name, value):
if name == 'charset' and self.type == 0:
raise ValueError('Graph charset is immutable!')
if not is_string_like(value):
value=str(value)
ghandle=gv.agroot(self.handle) # get root graph
if ghandle==self.handle:
gv.agattr_label(self.handle,self.type,
name.encode(self.encoding),
value.encode(self.encoding))
else:
gv.agsafeset_label(ghandle,self.handle,
name.encode(self.encoding),
value.encode(self.encoding),'')
def __getitem__(self, name):
item=gv.agget(self.handle,name.encode(self.encoding))
if item is None:
ah=gv.agattr(self.handle,self.type,
name.encode(self.encoding),
None)
item=gv.agattrdefval(ah)
return item.decode(self.encoding)
def __delitem__(self, name):
gv.agattr(self.handle,self.type,name.encode(self.encoding),'')
def __contains__(self, name):
try:
self.__getitem__(name)
return True
except:
return False
def has_key(self, name):
return self.__contains__(name)
def keys(self):
return(list(self.__iter__()))
def __iter__(self):
for (k,v) in self.iteritems():
yield k
def iteritems(self):
ah=None
while True:
try:
ah=gv.agnxtattr(self.handle,self.type,ah)
yield (gv.agattrname(ah).decode(self.encoding),
gv.agattrdefval(ah).decode(self.encoding))
except KeyError: # gv.agattrdefval returned KeyError, skip
continue
class ItemAttribute(Attribute):
"""Attributes for individual nodes and edges.
Assigned on initialization of Node or Edge classes
and manipulated through the class data.
>>> G=AGraph()
>>> G.add_edge('a','b')
>>> n=Node(G,'a')
>>> n.attr['shape']='circle'
>>> e=Edge(G,'a','b')
>>> e.attr['color']='red'
See
http://graphviz.org/doc/info/attrs.html
for a list of all attributes.
"""
# use for individual item attributes - either a node or an edge
# graphs and default node and edge attributes use Attribute
def __init__(self,handle,atype):
self.handle=handle
self.type=atype
self.ghandle=gv.agraphof(handle)
# get the encoding
root_handle=gv.agroot(self.ghandle) # get root graph
try:
ah=gv.agattr(root_handle,0,'charset',None)
self.encoding=gv.agattrdefval(ah)
except KeyError:
self.encoding=_DEFAULT_ENCODING
def __setitem__(self, name, value):
if not is_string_like(value):
value=str(value)
if self.type==1 and name=='label':
default='\N'
else:
default=''
gv.agsafeset_label(self.ghandle,self.handle,
name.encode(self.encoding),
value.encode(self.encoding),
default.encode(self.encoding))
def __getitem__(self, name):
val = gv.agget(self.handle,name.encode(self.encoding))
if val is not None:
val = val.decode(self.encoding)
return val
def __delitem__(self, name):
gv.agset(self.handle,name.encode(self.encoding),'')
def iteritems(self):
ah=None
while 1:
try:
ah=gv.agnxtattr(self.ghandle,self.type,ah)
value=gv.agxget(self.handle,ah)
try:
defval=gv.agattrdefval(ah) # default value
if defval==value:
continue # don't report default
except: # no default, gv.getattrdefval raised error
pass
# unique value for this edge
yield (gv.agattrname(ah).decode(self.encoding),
value.decode(self.encoding))
except KeyError: # gv.agxget returned KeyError, skip
continue
def _test_suite():
import doctest
suite = doctest.DocFileSuite('tests/graph.txt',
'tests/attributes.txt',
'tests/layout_draw.txt',
'tests/subgraph.txt',
package='pygraphviz')
doctest.testmod() # test docstrings in module
return suite
if __name__ == "__main__":
import os
import sys
import unittest
if sys.version_info[:2] < (2, 4):
print "Python version 2.4 or later required for tests (%d.%d detected)." % sys.version_info[:2]
sys.exit(-1)
# directory of package (relative to this)
nxbase=sys.path[0]+os.sep+os.pardir
sys.path.insert(0,nxbase) # prepend to search path
unittest.TextTestRunner().run(_test_suite())
|