annotate python/gview.py @ 787:d265e49b965e

shortcut
author Jeff Hammel <k0scist@gmail.com>
date Sun, 25 Sep 2016 16:29:22 -0700
parents 5e8d3f68a997
children
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
690
Jeff Hammel <k0scist@gmail.com>
parents: 686
diff changeset
13 import shlex
683
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14 import subprocess
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 import sys
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 import tempfile
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 __all__ = ['main']
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19 string = (str, unicode)
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 class Parser(argparse.ArgumentParser):
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """CLI option parser"""
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 def __init__(self):
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 argparse.ArgumentParser.__init__(self, description=__doc__)
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25 self.add_argument('input',
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 help='graphviz file to view')
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 self.add_argument('-o', '--output', dest='output',
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 help="path to save to")
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 self.add_argument('-e', '--program', dest='program',
686
34af6d5ac73c STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 684
diff changeset
30 default='fdp',
683
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 help="GraphViz program to invoke [DEFAULT: %(default)s]")
690
Jeff Hammel <k0scist@gmail.com>
parents: 686
diff changeset
32 self.add_argument('-v', '--view', dest='viewer', default='feh -F',
683
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 help="viewer")
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
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 def main(args=sys.argv[1:]):
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 """CLI"""
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 # parse command line
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 parser = Parser()
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 options = parser.parse_args(args)
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 #
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
44 assert os.path.exists(options.input)
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
45 output = options.output or tempfile.mktemp(suffix='.png')
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
46
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
47 command = [options.program,
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
48 options.input,
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
49 '-Tpng',
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
50 '-o', output]
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
51 subprocess.check_call(command)
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
52 assert os.path.exists(output)
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
53
684
8b2787e98c01 STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 683
diff changeset
54 try:
8b2787e98c01 STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 683
diff changeset
55 if options.viewer:
690
Jeff Hammel <k0scist@gmail.com>
parents: 686
diff changeset
56 viewer = shlex.split(options.viewer)
Jeff Hammel <k0scist@gmail.com>
parents: 686
diff changeset
57 subprocess.call(viewer + [output])
684
8b2787e98c01 STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 683
diff changeset
58 finally:
8b2787e98c01 STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 683
diff changeset
59 if not options.output:
8b2787e98c01 STUB: python/gview.py
Jeff Hammel <k0scist@gmail.com>
parents: 683
diff changeset
60 os.remove(output)
683
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
61
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
62
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
63 if __name__ == '__main__':
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
64 main()
ce95c61cb435 add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
65