forked from binaryage/firelogger.py
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmini_graphviz.py
More file actions
85 lines (63 loc) · 2.08 KB
/
mini_graphviz.py
File metadata and controls
85 lines (63 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import sys
import optparse
import tempfile
import subprocess
__all__ = [
'main',
]
DEFAULT_DOT = 'dot'
DEFAULT_VIEWER = 'eog'
USAGE = "%prog [options]"
OPTIONS = (
(('-D', '--dot-exe'),
dict(dest='dot', action='store', default=DEFAULT_DOT,
help='dot executable to use for making png, '
'default=%r' % DEFAULT_DOT)),
(('-V', '--viewer'),
dict(dest='viewer', action='store', default=DEFAULT_VIEWER,
help='viewer with which to open resulting png, '
'default=%r' % DEFAULT_VIEWER)),
)
def main(sysargs=sys.argv[:]):
parser = get_option_parser()
opts, targets = parser.parse_args(sysargs[1:])
for target in targets:
graphviz = MiniGraphviz(dot=opts.dot, viewer=opts.viewer)
graphviz.view_as_png(target)
return 0
def get_option_parser():
parser = optparse.OptionParser(usage=USAGE)
for args, kwargs in OPTIONS:
parser.add_option(*args, **kwargs)
return parser
class MiniGraphviz(object):
def __init__(self, dot=DEFAULT_DOT, viewer=DEFAULT_VIEWER):
self.dot = dot
self.viewer = viewer
def view_as_png(self, dot_input_file):
png_maker = Dot2PngMaker(dot_input_file, dot=self.dot)
png_path = png_maker.get_png()
self._open_png_with_viewer(png_path)
return png_path
def _open_png_with_viewer(self, png_path):
if self.viewer:
cmd = [self.viewer, png_path]
subprocess.call(cmd)
class Dot2PngMaker(object):
_tempfile = ''
def __init__(self, dot_input_file, dot=DEFAULT_DOT):
self.dot_input_file = dot_input_file
self.dot = dot
def get_png(self):
self._get_tempfile()
self._get_png_from_dot()
return self._tempfile
def _get_tempfile(self):
self._tempfile = tempfile.mkstemp('.png', __name__)[1]
def _get_png_from_dot(self):
cmd = [self.dot, '-T', 'png', '-o', self._tempfile,
self.dot_input_file]
subprocess.call(cmd)
if __name__ == '__main__':
sys.exit(main())
# vim:filetype=python