# HG changeset patch # User Paul Boddie # Date 1326565056 -3600 # Node ID 30f695aa03b1c8414b722fbc28ce34740207aecd # Parent 0091f58eb0aef401b463826374abffcf3a3f6086 Re-added direct output to file where the dimensions cannot be extracted from a graph. diff -r 0091f58eb0ae -r 30f695aa03b1 graphviz.py --- a/graphviz.py Sat Jan 14 19:00:46 2012 +0100 +++ b/graphviz.py Sat Jan 14 19:17:36 2012 +0100 @@ -163,7 +163,7 @@ return None - def get_chartname(self, digest, format, attrs): + def get_chartname(self, digest, format, attrs=None): "Return the chart name for the 'digest', 'format' and 'attrs'." @@ -203,10 +203,32 @@ output in the given 'format'. """ - p = subprocess.Popen([join(BINARY_PATH, filter), '-T%s' % format], shell=False, \ - stdin=subprocess.PIPE, \ - stdout=subprocess.PIPE, \ - stderr=subprocess.PIPE) + need_output = format in ("cmapx", "svg") + + # Either write the output straight to a file. + + if not need_output: + chart = self.get_chartname(digest, format) + filename = join(self.attach_dir, chart).encode(config.charset) + + p = subprocess.Popen([ + join(BINARY_PATH, filter), '-T%s' % format, '-o%s' % filename + ], + shell=False, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + + # Or intercept the output. + + else: + p = subprocess.Popen([ + join(BINARY_PATH, filter), '-T%s' % format + ], + shell=False, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) p.stdin.write(graph_def) p.stdin.flush() @@ -217,15 +239,26 @@ # Graph data always goes via standard output so that we can extract the # width and height if possible. - output, attrs = self.process_output(p.stdout, format) + if need_output: + output, attrs = self.process_output(p.stdout, format) + else: + output, attrs = None, {} + + # Test for errors. + errors = p.stderr.read() if len(errors) > 0: raise GraphVizError, errors - # Copy to a file, returning the width and height if possible. + # Return the output for imagemaps. - if format != "cmapx": + if format == "cmapx": + return output + + # Copy to a file, if necessary. + + elif need_output: chart = self.get_chartname(digest, format, attrs) filename = join(self.attach_dir, chart).encode(config.charset) @@ -235,12 +268,9 @@ finally: f.close() - return attrs + # Return the dimensions, if defined. - # Otherwise, return the output. - - else: - return output + return attrs def process_output(self, output, format): @@ -270,7 +300,7 @@ "Return a (width, height) tuple using the 'attrs' dictionary." - if attrs.has_key("width") and attrs.has_key("height"): + if attrs and attrs.has_key("width") and attrs.has_key("height"): return attrs["width"], attrs["height"] else: return None @@ -284,3 +314,5 @@ if attrs.has_key(key): dattrs[key] = attrs[key] return dattrs + +# vim: tabstop=4 expandtab shiftwidth=4