Mercurial > hg > config
annotate python/gview.py @ 761:9e4a21ca48af
add a front end to rst2s5; stub
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 04 Jan 2016 12:54:51 -0800 |
parents | 5e8d3f68a997 |
children |
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 | 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 | 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 | 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 | 54 try: |
55 if options.viewer: | |
690 | 56 viewer = shlex.split(options.viewer) |
57 subprocess.call(viewer + [output]) | |
684 | 58 finally: |
59 if not options.output: | |
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 |