Mercurial > hg > config
changeset 683:ce95c61cb435
add viewer for graphviz; silly, but effective
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Sat, 17 May 2014 00:15:45 -0700 |
parents | 3fe1024377ca |
children | 8b2787e98c01 |
files | python/gview.py |
diffstat | 1 files changed, 62 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/gview.py Sat May 17 00:15:45 2014 -0700 @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +view graphviz files + +http://www.graphviz.org/ +""" + +# imports +import argparse +import os +import subprocess +import sys +import tempfile + +__all__ = ['main'] +string = (str, unicode) + +class Parser(argparse.ArgumentParser): + """CLI option parser""" + def __init__(self): + argparse.ArgumentParser.__init__(self, description=__doc__) + self.add_argument('input', + help='graphviz file to view') + self.add_argument('-o', '--output', dest='output', + help="path to save to") + self.add_argument('-e', '--program', dest='program', + default='dot', + help="GraphViz program to invoke [DEFAULT: %(default)s]") + self.add_argument('-v', '--view', dest='viewer', default='feh', + help="viewer") + + +def main(args=sys.argv[1:]): + """CLI""" + + # parse command line + parser = Parser() + options = parser.parse_args(args) + + # + assert os.path.exists(options.input) + output = options.output or tempfile.mktemp(suffix='.png') + + command = [options.program, + options.input, + '-Tpng', + '-o', output] + subprocess.check_call(command) + assert os.path.exists(output) + + if options.viewer: + subprocess.call([options.viewer, output]) + + if not options.output: + os.remove(output) + + +if __name__ == '__main__': + main() +