Merge lp:~rmurri/objgraph/any-output-format into lp:objgraph

Proposed by Riccardo Murri
Status: Merged
Merge reported by: Marius Gedminas
Merged at revision: not available
Proposed branch: lp:~rmurri/objgraph/any-output-format
Merge into: lp:objgraph
Diff against target: 51 lines (+16/-15)
1 file modified
objgraph.py (+16/-15)
To merge this branch: bzr merge lp:~rmurri/objgraph/any-output-format
Reviewer Review Type Date Requested Status
Marius Gedminas Approve
Review via email: mp+93969@code.launchpad.net

Description of the change

Allow the ``filename`` argument in ``show_graph`` to specify an arbitrary output format (by extension).

The ``dot`` program will do the right thing, and we fall back to just providing the ``.dot`` file if conversion fails (which may well be, since many backends are optional in graphviz).

I have only used/tested this with ``.svg`` output, but there's no reason it should not work with any other.

To post a comment you must log in.
Revision history for this message
Marius Gedminas (mgedmin) wrote :

Thanks, merged!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'objgraph.py'
2--- objgraph.py 2011-12-11 16:44:37 +0000
3+++ objgraph.py 2012-02-21 11:39:25 +0000
4@@ -326,11 +326,14 @@
5 ``objs`` can be a single object, or it can be a list of objects. If
6 unsure, wrap the single object in a new list.
7
8- ``filename`` if specified, can be the name of a .dot or a .png file,
9- indicating the desired output format. If not specified, ``show_backrefs``
10- will try to produce a .dot file and spawn a viewer (xdot). If xdot is
11- not available, ``show_backrefs`` will convert the .dot file to a .png
12- and print its name.
13+ ``filename`` if specified, can be the name of a .dot or a image
14+ file, whose extension indicates the desired output format; note
15+ that output to a specific format is entirely handled by GraphViz:
16+ if the desired format is not supported, you just get the .dot
17+ file. If ``filename`` is not specified, ``show_backrefs`` will
18+ try to produce a .dot file and spawn a viewer (xdot). If xdot is
19+ not available, ``show_backrefs`` will convert the .dot file to a
20+ .png and print its name.
21
22 Use ``max_depth`` and ``too_many`` to limit the depth and breadth of the
23 graph.
24@@ -612,19 +615,17 @@
25 elif program_in_path('dot'):
26 if not filename:
27 print("Graph viewer (xdot) not found, generating a png instead")
28- if filename and filename.endswith('.png'):
29- f = open(filename, 'wb')
30- png_filename = filename
31- else:
32- if filename:
33- print("Unrecognized file type (%s)" % filename)
34- fd, png_filename = tempfile.mkstemp('.png', text=False)
35- f = os.fdopen(fd, "wb")
36- dot = subprocess.Popen(['dot', '-Tpng', dot_filename],
37+ filename = dot_filename[:-4] + '.png'
38+ stem, ext = os.path.splitext(filename)
39+ f = open(filename, 'wb')
40+ dot = subprocess.Popen(['dot', ('-T' + ext[1:]), dot_filename],
41 stdout=f, close_fds=False)
42 dot.wait()
43+ if dot.returncode != 0:
44+ # XXX: shouldn't this go to stderr or a log?
45+ print("dot failed to generate '%s' image: output format not supported?")
46 f.close()
47- print("Image generated as %s" % png_filename)
48+ print("Image generated as %s" % filename)
49 else:
50 if filename:
51 print("Graph viewer (xdot) and image renderer (dot) not found, not doing anything else")

Subscribers

People subscribed via source and target branches