| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <[email protected]> |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: BSD-2-Clause |
| 6 |
|
| 7 |
""" |
| 8 |
A set of classes and functions to make it easier to work with Graphviz graphs |
| 9 |
""" |
| 10 |
|
| 11 |
import gv |
| 12 |
|
| 13 |
|
| 14 |
class Node(object): |
| 15 |
def __init__(self, node_handle): |
| 16 |
self.handle = node_handle |
| 17 |
|
| 18 |
@property |
| 19 |
def name(self): |
| 20 |
return gv.nameof(self.handle) |
| 21 |
|
| 22 |
@property |
| 23 |
def label(self): |
| 24 |
return gv.getv(self.handle, "label") |
| 25 |
|
| 26 |
@property |
| 27 |
def shape(self): |
| 28 |
return gv.getv(self.handle, "shape") |
| 29 |
|
| 30 |
|
| 31 |
class Edge(object): |
| 32 |
def __init__(self, edge_handle): |
| 33 |
self.handle = edge_handle |
| 34 |
|
| 35 |
@property |
| 36 |
def head(self): |
| 37 |
handle = gv.headof(self.handle) |
| 38 |
if handle is None: |
| 39 |
return None |
| 40 |
else: |
| 41 |
return Node(handle) |
| 42 |
|
| 43 |
@property |
| 44 |
def tail(self): |
| 45 |
handle = gv.tailof(self.handle) |
| 46 |
if handle is None: |
| 47 |
return None |
| 48 |
else: |
| 49 |
return Node(handle) |
| 50 |
|
| 51 |
|
| 52 |
def get_node_list(graph_h): |
| 53 |
"""Generator to iterate over all nodes of a graph""" |
| 54 |
handle = gv.firstnode(graph_h) |
| 55 |
while gv.ok(handle): |
| 56 |
yield handle |
| 57 |
handle = gv.nextnode(graph_h, handle) |
| 58 |
|
| 59 |
|
| 60 |
def get_edge_list(graph_h): |
| 61 |
"""Generator to iterate over all edges of a graph""" |
| 62 |
handle = gv.firstedge(graph_h) |
| 63 |
while gv.ok(handle): |
| 64 |
yield handle |
| 65 |
handle = gv.nextedge(graph_h, handle) |