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