1.1 --- a/graphviz.py Sat Jan 14 19:00:46 2012 +0100
1.2 +++ b/graphviz.py Sat Jan 14 19:17:36 2012 +0100
1.3 @@ -163,7 +163,7 @@
1.4
1.5 return None
1.6
1.7 - def get_chartname(self, digest, format, attrs):
1.8 + def get_chartname(self, digest, format, attrs=None):
1.9
1.10 "Return the chart name for the 'digest', 'format' and 'attrs'."
1.11
1.12 @@ -203,10 +203,32 @@
1.13 output in the given 'format'.
1.14 """
1.15
1.16 - p = subprocess.Popen([join(BINARY_PATH, filter), '-T%s' % format], shell=False, \
1.17 - stdin=subprocess.PIPE, \
1.18 - stdout=subprocess.PIPE, \
1.19 - stderr=subprocess.PIPE)
1.20 + need_output = format in ("cmapx", "svg")
1.21 +
1.22 + # Either write the output straight to a file.
1.23 +
1.24 + if not need_output:
1.25 + chart = self.get_chartname(digest, format)
1.26 + filename = join(self.attach_dir, chart).encode(config.charset)
1.27 +
1.28 + p = subprocess.Popen([
1.29 + join(BINARY_PATH, filter), '-T%s' % format, '-o%s' % filename
1.30 + ],
1.31 + shell=False,
1.32 + stdin=subprocess.PIPE,
1.33 + stdout=subprocess.PIPE,
1.34 + stderr=subprocess.PIPE)
1.35 +
1.36 + # Or intercept the output.
1.37 +
1.38 + else:
1.39 + p = subprocess.Popen([
1.40 + join(BINARY_PATH, filter), '-T%s' % format
1.41 + ],
1.42 + shell=False,
1.43 + stdin=subprocess.PIPE,
1.44 + stdout=subprocess.PIPE,
1.45 + stderr=subprocess.PIPE)
1.46
1.47 p.stdin.write(graph_def)
1.48 p.stdin.flush()
1.49 @@ -217,15 +239,26 @@
1.50 # Graph data always goes via standard output so that we can extract the
1.51 # width and height if possible.
1.52
1.53 - output, attrs = self.process_output(p.stdout, format)
1.54 + if need_output:
1.55 + output, attrs = self.process_output(p.stdout, format)
1.56 + else:
1.57 + output, attrs = None, {}
1.58 +
1.59 + # Test for errors.
1.60 +
1.61 errors = p.stderr.read()
1.62
1.63 if len(errors) > 0:
1.64 raise GraphVizError, errors
1.65
1.66 - # Copy to a file, returning the width and height if possible.
1.67 + # Return the output for imagemaps.
1.68
1.69 - if format != "cmapx":
1.70 + if format == "cmapx":
1.71 + return output
1.72 +
1.73 + # Copy to a file, if necessary.
1.74 +
1.75 + elif need_output:
1.76 chart = self.get_chartname(digest, format, attrs)
1.77 filename = join(self.attach_dir, chart).encode(config.charset)
1.78
1.79 @@ -235,12 +268,9 @@
1.80 finally:
1.81 f.close()
1.82
1.83 - return attrs
1.84 + # Return the dimensions, if defined.
1.85
1.86 - # Otherwise, return the output.
1.87 -
1.88 - else:
1.89 - return output
1.90 + return attrs
1.91
1.92 def process_output(self, output, format):
1.93
1.94 @@ -270,7 +300,7 @@
1.95
1.96 "Return a (width, height) tuple using the 'attrs' dictionary."
1.97
1.98 - if attrs.has_key("width") and attrs.has_key("height"):
1.99 + if attrs and attrs.has_key("width") and attrs.has_key("height"):
1.100 return attrs["width"], attrs["height"]
1.101 else:
1.102 return None
1.103 @@ -284,3 +314,5 @@
1.104 if attrs.has_key(key):
1.105 dattrs[key] = attrs[key]
1.106 return dattrs
1.107 +
1.108 +# vim: tabstop=4 expandtab shiftwidth=4