Mercurial > hg > config
comparison python/gview.py @ 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 | |
| children | 8b2787e98c01 |
comparison
equal
deleted
inserted
replaced
| 682:3fe1024377ca | 683:ce95c61cb435 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 # -*- coding: utf-8 -*- | |
| 3 | |
| 4 """ | |
| 5 view graphviz files | |
| 6 | |
| 7 http://www.graphviz.org/ | |
| 8 """ | |
| 9 | |
| 10 # imports | |
| 11 import argparse | |
| 12 import os | |
| 13 import subprocess | |
| 14 import sys | |
| 15 import tempfile | |
| 16 | |
| 17 __all__ = ['main'] | |
| 18 string = (str, unicode) | |
| 19 | |
| 20 class Parser(argparse.ArgumentParser): | |
| 21 """CLI option parser""" | |
| 22 def __init__(self): | |
| 23 argparse.ArgumentParser.__init__(self, description=__doc__) | |
| 24 self.add_argument('input', | |
| 25 help='graphviz file to view') | |
| 26 self.add_argument('-o', '--output', dest='output', | |
| 27 help="path to save to") | |
| 28 self.add_argument('-e', '--program', dest='program', | |
| 29 default='dot', | |
| 30 help="GraphViz program to invoke [DEFAULT: %(default)s]") | |
| 31 self.add_argument('-v', '--view', dest='viewer', default='feh', | |
| 32 help="viewer") | |
| 33 | |
| 34 | |
| 35 def main(args=sys.argv[1:]): | |
| 36 """CLI""" | |
| 37 | |
| 38 # parse command line | |
| 39 parser = Parser() | |
| 40 options = parser.parse_args(args) | |
| 41 | |
| 42 # | |
| 43 assert os.path.exists(options.input) | |
| 44 output = options.output or tempfile.mktemp(suffix='.png') | |
| 45 | |
| 46 command = [options.program, | |
| 47 options.input, | |
| 48 '-Tpng', | |
| 49 '-o', output] | |
| 50 subprocess.check_call(command) | |
| 51 assert os.path.exists(output) | |
| 52 | |
| 53 if options.viewer: | |
| 54 subprocess.call([options.viewer, output]) | |
| 55 | |
| 56 if not options.output: | |
| 57 os.remove(output) | |
| 58 | |
| 59 | |
| 60 if __name__ == '__main__': | |
| 61 main() | |
| 62 |
