~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to dotgraph.py

  • Committer: Aaron Bentley
  • Date: 2013-08-20 03:02:43 UTC
  • Revision ID: aaron@aaronbentley.com-20130820030243-r8v1xfbcnd8f10p4
Fix zap command for 2.6/7

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python2.4
 
1
# Copyright (C) 2004, 2005 Aaron Bentley
 
2
# <aaron@aaronbentley.com>
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
2
18
from subprocess import Popen, PIPE
3
 
from urllib import urlencode
4
 
from xml.sax.saxutils import escape
5
19
import os.path
6
20
import errno
7
21
import tempfile
8
22
import shutil
9
 
import time
10
23
 
11
24
RSVG_OUTPUT_TYPES = ('png', 'jpg')
12
 
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png', 
 
25
DOT_OUTPUT_TYPES = ('svg', 'svgz', 'gif', 'jpg', 'ps', 'fig', 'mif', 'png',
13
26
                    'cmapx')
14
27
 
15
28
class NoDot(Exception):
36
49
        self.message = message
37
50
        self.href = None
38
51
 
 
52
    @staticmethod
 
53
    def get_attribute(name, value):
 
54
        if value is None:
 
55
            return ''
 
56
        value = value.replace("\\", "\\\\")
 
57
        value = value.replace('"', '\\"')
 
58
        value = value.replace('\n', '\\n')
 
59
        return '%s="%s"' % (name, value)
 
60
 
39
61
    def define(self):
40
62
        attributes = []
41
63
        style = []
49
71
        if label is not None:
50
72
            attributes.append('label="%s"' % label)
51
73
        attributes.append('shape="box"')
52
 
        tooltip = ''
 
74
        tooltip = None
53
75
        if self.message is not None:
54
 
            tooltip += self.message.replace('"', '\\"')
55
 
        if tooltip:
56
 
            attributes.append('tooltip="%s"' % tooltip)
 
76
            tooltip = self.message
 
77
        attributes.append(self.get_attribute('tooltip', tooltip))
57
78
        if self.href is not None:
58
79
            attributes.append('href="%s"' % self.href)
59
80
        elif tooltip:
182
203
        shutil.rmtree(tempdir)
183
204
    return status
184
205
 
185
 
def invoke_dot(input, out_file=None, file_type='svg', antialias=None, 
 
206
def invoke_dot(input, out_file=None, file_type='svg', antialias=None,
186
207
               fontname="Helvetica", fontsize=11):
187
 
    cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname, 
 
208
    cmdline = ['dot', '-T%s' % file_type, '-Nfontname=%s' % fontname,
188
209
               '-Efontname=%s' % fontname, '-Nfontsize=%d' % fontsize,
189
210
               '-Efontsize=%d' % fontsize]
190
211
    if out_file is not None:
197
218
        else:
198
219
            raise
199
220
    for line in input:
200
 
        dot_proc.stdin.write(line)
 
221
        dot_proc.stdin.write(line.encode('utf-8'))
201
222
    dot_proc.stdin.close()
202
223
    return dot_proc.wait()
203
224