forked from ogdf/ogdf-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.py
More file actions
115 lines (95 loc) · 3.56 KB
/
render.py
File metadata and controls
115 lines (95 loc) · 3.56 KB
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
import functools
import tempfile
import cppyy
import itertools
import sys
SVGConf = None
def wrap_GraphIO(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logger = cppyy.gbl.ogdf.GraphIO.logger
levels = {
l: getattr(logger, l)()
for l in [
"globalLogLevel",
"globalInternalLibraryLogLevel",
"globalMinimumLogLevel",
"localLogLevel",
]
}
logMode = logger.localLogMode()
statMode = logger.globalStatisticMode()
ret = stdout = stderr = None
try:
logger.localLogMode(type(logger).LogMode.Global)
logger.globalStatisticMode(False)
for l in levels:
getattr(logger, l)(type(logger).Level.Minor)
cppyy.gbl.ogdf_pythonization.BeginCaptureStdout()
cppyy.gbl.ogdf_pythonization.BeginCaptureStderr()
try:
ret = func(*args, **kwargs)
finally:
stdout = cppyy.gbl.ogdf_pythonization.EndCaptureStdout().decode("utf8", "replace").strip()
stderr = cppyy.gbl.ogdf_pythonization.EndCaptureStderr().decode("utf8", "replace").strip()
finally:
logger.localLogMode(logMode)
logger.globalStatisticMode(statMode)
for l, v in levels.items():
getattr(logger, l)(v)
if stdout and logger.is_lout(logger.Level.Medium):
print(stdout)
if stderr:
print(stderr, file=sys.stderr)
if not ret:
args = ', '.join(itertools.chain(map(repr, args), (f"{k}={v!r}" for k, v in kwargs.items())))
msg = f"GraphIO.{func.__name__}({args}) failed"
if stdout or stderr:
msg += ":"
if stdout:
msg += "\n" + stdout
if stderr:
msg += "\n" + stderr
else:
msg += " for unknown reason. Does the file exist?"
raise IOError(msg)
else:
return ret
wrapper.__overload__ = func.__overload__
return wrapper
def renderGraph(G):
cppyy.include("ogdf/planarity/PlanarizationGridLayout.h")
ogdf = cppyy.gbl.ogdf
GA = ogdf.GraphAttributes(G, ogdf.GraphAttributes.all)
ogdf.PlanarizationGridLayout().call(GA)
for n in G.nodes:
GA.label[n] = str(n.index())
return GA
def renderClusterGraph(CG):
cppyy.include("ogdf/cluster/ClusterPlanarizationLayout.h")
ogdf = cppyy.gbl.ogdf
CGA = ogdf.ClusterGraphAttributes(CG, ogdf.ClusterGraphAttributes.all)
cppyy.gbl.ogdf_pythonization.BeginCaptureStdout()
try:
ogdf.ClusterPlanarizationLayout().call(CG.getGraph(), CGA, CG)
finally:
stdout = cppyy.gbl.ogdf_pythonization.EndCaptureStdout().decode("utf8", "replace").strip()
for n in CG.getGraph().nodes:
CGA.label[n] = str(n.index())
return CGA
def GraphAttributes_to_svg(self):
global SVGConf
if SVGConf is None:
SVGConf = cppyy.gbl.ogdf.GraphIO.SVGSettings()
SVGConf.margin(50)
SVGConf.bezierInterpolation(False)
# SVGConf.curviness(0.1)
with tempfile.NamedTemporaryFile("w+t", suffix=".svg", prefix="ogdf-python-") as f:
os = cppyy.gbl.std.ofstream(f.name)
cppyy.gbl.ogdf.GraphIO.drawSVG(self, os, SVGConf)
os.close()
return f.read()
def Graph_to_svg(self):
return GraphAttributes_to_svg(renderGraph(self))
def ClusterGraph_to_svg(self):
return GraphAttributes_to_svg(renderClusterGraph(self))