Support several output formats in state_graph tool

This commit adds -T aka --format option to state graph tool that
can be used to specify output format. Supported values are 'dot',
'svg', 'svgz' and 'png'.

Change-Id: I27a74b582726da24a1b05d4ffb72bde860fff417
This commit is contained in:
Ivan A. Melnikov 2013-10-16 19:52:29 +04:00 committed by Gerrit Code Review
parent aeb8fb9678
commit 176d0a920e

View File

@ -24,36 +24,44 @@ def mini_exec(cmd, ok_codes=(0,)):
(stdout, stderr) = proc.communicate() (stdout, stderr) = proc.communicate()
rc = proc.returncode rc = proc.returncode
if rc not in ok_codes: if rc not in ok_codes:
raise RuntimeError("Could not run %s [%s]", cmd, rc) raise RuntimeError("Could not run %s [%s]\nStderr: %s"
% (cmd, rc, stderr))
return (stdout, stderr) return (stdout, stderr)
def make_svg(graph, output_filename): def make_svg(graph, output_filename, output_format):
# NOTE(harlowja): requires pydot! # NOTE(harlowja): requires pydot!
gdot = gu.export_graph_to_dot(graph) gdot = gu.export_graph_to_dot(graph)
with tempfile.NamedTemporaryFile(suffix=".dot") as fh: if output_format == 'dot':
fh.write(gdot) output = gdot
fh.flush() elif output_format in ('svg', 'svgz', 'png'):
cmd = ['dot', '-Tsvg', fh.name] with tempfile.NamedTemporaryFile(suffix=".dot") as fh:
stdout, _stderr = mini_exec(cmd) fh.write(gdot)
with open(output_filename, "wb") as fh: fh.flush()
fh.write(stdout) cmd = ['dot', '-T%s' % output_format, fh.name]
# NOTE(harlowja): if u want a png instead u can run the following which output, _stderr = mini_exec(cmd)
# requires ImageMagick and its little helper `convert` program. else:
# raise ValueError('Unknown format: %s' % output_filename)
# $ convert input.svg output.png with open(output_filename, "wb") as fh:
fh.write(output)
def main(): def main():
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-f", "--file", dest="filename", parser.add_option("-f", "--file", dest="filename",
help="write svg to FILE", metavar="FILE", help="write svg to FILE", metavar="FILE")
default="states.svg")
parser.add_option("-t", "--tasks", dest="tasks", parser.add_option("-t", "--tasks", dest="tasks",
action='store_true', action='store_true',
help="use task state transitions", help="use task state transitions",
default=False) default=False)
parser.add_option("-T", "--format", dest="format",
help="output in given format",
default='svg')
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
if options.filename is None:
options.filename = 'states.%s' % options.format
g = nx.DiGraph(name="State transitions") g = nx.DiGraph(name="State transitions")
if not options.tasks: if not options.tasks:
source = states._ALLOWED_FLOW_TRANSITIONS source = states._ALLOWED_FLOW_TRANSITIONS
@ -65,8 +73,8 @@ def main():
if not g.has_node(v): if not g.has_node(v):
g.add_node(v) g.add_node(v)
g.add_edge(u, v) g.add_edge(u, v)
make_svg(g, options.filename) make_svg(g, options.filename, options.format)
print("Created svg at '%s'" % (options.filename)) print("Created %s at '%s'" % (options.format, options.filename))
if __name__ == '__main__': if __name__ == '__main__':