summaryrefslogtreecommitdiff
path: root/ovsdb/dot2pic
blob: 2f858e19d5b6300a2c22360e29a033a79e2990fa (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3

# Copyright (c) 2009, 2010, 2011, 2013, 2017, 2020 Nicira, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import getopt
import sys

def dot2pic(src, dst):
    scale = 1.0
    while True:
        line = src.readline()
        if not line:
            break

        words = line.split()
        command = words[0]
        if command == 'graph':
            scale = float(words[1])
        elif command == 'node':
            name = words[1]
            x = float(words[2])
            y = float(words[3])
            width = float(words[4])
            height = float(words[5])
            label, style, shape, color, fillcolor = words[6:11]
            x *= scale
            y *= scale
            width *= scale
            height *= scale
            dst.write("linethick = %f;\n" % (0.5 if style == 'bold' else 1.0))
            dst.write('box at %f,%f wid %f height %f "%s"\n'
                      % (x, y, width, height, name))
            if style == 'bold':
                inset = 2.0 / 72.0
                width -= inset * 2
                height -= inset * 2
                dst.write("box at %f,%f wid %f height %f\n"
                          % (x, y, width, height))
        elif command == 'edge':
            tail = words[1]
            head = words[2]
            n = int(words[3])

            # Extract x,y coordinates.
            words = words[4:]
            xy = []
            for i in range(n):
                x = float(words[0]) * scale
                y = float(words[1]) * scale
                words = words[2:]
                xy.append((x, y))

            # Extract style and color from end of words.
            style, color = words[-2:]
            words = words[:-2]

            # If there's anything left, that's the label.
            if words:
                xl = float(words[-2]) * scale
                yl = float(words[-1]) * scale
                label = ' '.join(words[:-2])
                if label.startswith('"') and label.endswith('"'):
                    label = label[1:-1]
            else:
                label = None

            dst.write("linethick = %f;\n"
                      % (0.5 if style == 'dotted' else 1.0))
            dst.write("spline -> from %f,%f" % xy[0])
            for x, y in xy:
                dst.write(" to %f,%f" % (x, y))
            dst.write('\n')

            if label:
                dst.write('"%s" at %f,%f\n' % (label, xl, yl))
        elif command == 'stop':
            break
        else:
            sys.stderr.write("%s\n" % command)
            assert False


options, args = getopt.gnu_getopt(sys.argv[1:], 'f:', [])

font_scale = 0
for key, value in options:
    if key == '-f':
        font_scale = int(value)
    else:
        raise False

if font_scale:
    print(".ps %+d" % -font_scale)

print(".PS")
print("linethick = 1;")
if args:
    for arg in args:
        dot2pic(open(arg), sys.stdout)
else:
    dot2pic(sys.stdin, sys.stdout)
if font_scale:
    print(".ps %+d" % font_scale)
print(".PE")