forked from tkf/ipython-hierarchymagic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hierarchymagic.py
314 lines (260 loc) · 10.7 KB
/
hierarchymagic.py
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
"""
`%hierarchy`, `%%dot` and `%dotstr` magics for IPython
===========================================
This extension provides three magics.
First magic is ``%hierarchy``. This magic command draws hierarchy of
given class or the class of given instance. For example, the
following shows class hierarchy of currently running IPython shell.::
%hierarchy get_ipython()
Second magic is ``%%dot``. You can write graphiz dot language in a
cell using this magic. Example::
%%dot -- -Kfdp
digraph G {
a->b; b->c; c->d; d->b; d->a;
}
Third magic is ``%%dotstr``. When you have a string written in the dot
language, use this function. E.g. when using pydot objects. Example::
%dotstr -f svg -o-Kfdp pydot_obj.to_string()
License for ipython-hierarchymagic
----------------------------------
ipython-hierarchymagic is licensed under the term of the Simplified
BSD License (BSD 2-clause license), as follows:
Copyright (c) 2012 Takafumi Arakaki
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License for Sphinx
------------------
`run_dot` function and `HierarchyMagic._class_name` method in this
extension heavily based on Sphinx code `sphinx.ext.graphviz.render_dot`
and `InheritanceGraph.class_name`.
Copyright notice for Sphinx can be found below.
Copyright (c) 2007-2011 by the Sphinx team (see AUTHORS file).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
from IPython.core.magic import Magics, magics_class, line_magic, cell_magic
from IPython.core.magic_arguments import (argument, magic_arguments,
parse_argstring)
from IPython.core.display import display_png, display_svg
from sphinx.ext.inheritance_diagram import InheritanceGraph
def run_dot(code, options=[], format='png'):
# mostly copied from sphinx.ext.graphviz.render_dot
import os
from subprocess import Popen, PIPE
from sphinx.util.osutil import EPIPE, EINVAL
dot_args = ['dot'] + options + ['-T', format]
if os.name == 'nt':
# Avoid opening shell window.
# * https://github.com/tkf/ipython-hierarchymagic/issues/1
# * http://stackoverflow.com/a/2935727/727827
p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE,
creationflags=0x08000000)
else:
p = Popen(dot_args, stdout=PIPE, stdin=PIPE, stderr=PIPE)
wentwrong = False
try:
# Graphviz may close standard input when an error occurs,
# resulting in a broken pipe on communicate()
stdout, stderr = p.communicate(code.encode('utf-8'))
except (OSError, IOError) as err:
if err.errno != EPIPE:
raise
wentwrong = True
except IOError as err:
if err.errno != EINVAL:
raise
wentwrong = True
if wentwrong:
# in this case, read the standard output and standard error streams
# directly, to get the error message(s)
stdout, stderr = p.stdout.read(), p.stderr.read()
p.wait()
if p.returncode != 0:
raise RuntimeError('dot exited with error:\n[stderr]\n{0}'
.format(stderr.decode('utf-8')))
return stdout
@magics_class
class GraphvizMagic(Magics):
@magic_arguments()
@argument(
'-f', '--format', default='png', choices=('png', 'svg'),
help='output format (png/svg)'
)
@argument(
'options', default=[], nargs='*',
help='options passed to the `dot` command'
)
@cell_magic
def dot(self, line, cell):
""" Draw a figure from the cell contents (using Graphviz dot)."""
args = parse_argstring(self.dot, line)
self._dot(args, cell)
@magic_arguments()
@argument(
'-f', '--format', default='png', choices=('png', 'svg'),
help='Output format (png/svg).'
)
@argument(
'-o', '--options', default=[], nargs='*',
help='Options passed to the `dot` command.'
)
@argument(
'strings', nargs='*',
help='Strings in graphviz syntax.'
)
@line_magic
def dotstr(self, line):
""" Draw a figure from a string (using Graphviz dot). """
args = parse_argstring(self.dotstr, line)
for s in args.strings:
self._dot(args, self.shell.ev(s))
def _dot(self, args, string):
""" Run the Graphviz dot command. """
# Parse the arguments
image = run_dot(string, args.options, format=args.format)
if args.format == 'png':
display_png(image, raw=True)
elif args.format == 'svg':
display_svg(image, raw=True)
class FoldedInheritanceGraph(InheritanceGraph):
def __init__(self, *args, **kwds):
self._width = kwds.pop('width', 40)
super(FoldedInheritanceGraph, self).__init__(*args, **kwds)
@staticmethod
def _foldclassname(classname, width):
r"""
Split `classname` in newlines if the width is wider than `width`.
>>> fold = FoldedInheritanceGraph._foldclassname
>>> fold('aaa.bbb.ccc', 7)
'aaa.bbb\\n.ccc'
>>> fold('aaa.bbb.ccc', 3)
'aaa\\n.bbb\\n.ccc'
>>> identity = lambda x, y: ''.join(fold(x, y).split('\\n'))
>>> identity('aaa.bbb.ccc', 7)
'aaa.bbb.ccc'
>>> identity('aaa.bbb.ccc', 3)
'aaa.bbb.ccc'
"""
parts = classname.split('.')
lines = []
chunk = [parts.pop(0)]
for p in parts:
if len('.'.join(chunk + [p])) > width:
lines.append('.'.join(chunk))
chunk = [p]
else:
chunk.append(p)
lines.append('.'.join(chunk))
return '\\n.'.join(lines)
def _class_info(self, *args, **kwds):
class_info = super(FoldedInheritanceGraph, self) \
._class_info(*args, **kwds)
width = self._width
def fold(elem):
(nodename, fullname, baselist, tooltip) = elem
nodename = self._foldclassname(nodename, width)
baselist = [self._foldclassname(b, width) for b in baselist]
return (nodename, fullname, baselist, tooltip)
return map(fold, class_info)
@magics_class
class HierarchyMagic(Magics):
@magic_arguments()
@argument(
'-r', '--rankdir', default='TB',
help='direction of the hierarchy graph (default: %(default)s)'
)
@argument(
'-s', '--size', default='5.0, 12.0',
help='size of the generated figure (default: %(default)s)',
)
@argument(
'-w', '--name-width', default=80, type=int,
help='width of each nodes in character length (default: %(default)s)',
)
@argument(
'object', nargs='+',
help='Class hierarchy of these classes or objects will be drawn',
)
@line_magic
def hierarchy(self, parameter_s=''):
"""Draw hierarchy of a given class."""
args = parse_argstring(self.hierarchy, parameter_s)
objects = map(self.shell.ev, args.object)
clslist = map(self._object_to_class, objects)
namelist = map(self._class_name, clslist)
ig = FoldedInheritanceGraph(
namelist, '',
width=args.name_width)
code = ig.generate_dot('inheritance_graph',
graph_attrs={'rankdir': args.rankdir,
'size': '"{0}"'.format(args.size)})
stdout = run_dot(code, format='png')
display_png(stdout, raw=True)
@staticmethod
def _object_to_class(obj):
if isinstance(obj, type):
return obj
elif hasattr(obj, "__class__"):
return obj.__class__
else:
raise ValueError(
"Given object {0} is not a class or an instance".format(obj))
@staticmethod
def _class_name(cls, parts=0):
"""Given a class object, return a fully-qualified name.
This works for things I've tested in matplotlib so far, but may not be
completely general.
"""
module = cls.__module__
if module == '__builtin__':
fullname = cls.__name__
else:
fullname = '%s.%s' % (module, cls.__name__)
if parts == 0:
return fullname
name_parts = fullname.split('.')
return '.'.join(name_parts[-parts:])
def load_ipython_extension(ip):
"""Load the extension in IPython."""
global _loaded
if not _loaded:
ip.register_magics(HierarchyMagic)
ip.register_magics(GraphvizMagic)
_loaded = True
_loaded = False