Mercurial > hg > config
annotate python/gview.py @ 688:ad8146c60c4d
merge
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 02 Jun 2014 11:21:46 -0700 |
parents | 34af6d5ac73c |
children | 5e8d3f68a997 |
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', |
686 | 29 default='fdp', |
683
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 |
684 | 53 try: |
54 if options.viewer: | |
55 subprocess.call([options.viewer, output]) | |
56 finally: | |
57 if not options.output: | |
58 os.remove(output) | |
683
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 |
ce95c61cb435
add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
61 if __name__ == '__main__': |
ce95c61cb435
add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
62 main() |
ce95c61cb435
add viewer for graphviz; silly, but effective
Jeff Hammel <k0scist@gmail.com>
parents:
diff
changeset
|
63 |