def gg_figure(r, name, ggraph, do_png=True, do_pdf=True, do_svg=True, do_dot=True): """ Adds a figure to the Report r that displays this graph and also its source. """ f = r.figure(name, cols=1) # save file in dot file with tmpfile(".dot") as filename_dot: with open(filename_dot, 'w') as fo: s = get_dot_string(ggraph) fo.write(s) # if False: # ff = '%s.dot' % id(r) # print('writing to %r' % ff) # with open(ff, 'w') as f2: # f2.write(s) prog = 'dot' try: if do_png: with f.data_file('graph', MIME_PNG) as filename: graphviz_run(filename_dot, filename, prog=prog) if do_pdf: with f.data_file('graph_pdf', MIME_PDF) as filename: graphviz_run(filename_dot, filename, prog=prog) if do_svg: with f.data_file('graph_svg', MIME_SVG) as filename: graphviz_run(filename_dot, filename, prog=prog) from mcdp_report.embedded_images import embed_svg_images data = open(filename).read() soup = bs(data) embed_svg_images(soup) # does not keep doctype: s = to_html_stripping_fragment(soup) # this will keep the doctype s = str(soup) s = s.replace('<fragment>', '') s = s.replace('</fragment>', '') write_bytes_to_file_as_utf8(s, filename) except CmdException: if MCDPConstants.test_ignore_graphviz_errors: mcdp_dev_warning('suppressing errors from graphviz') logger.error('Graphivz failed, but I will ignore it ' 'because of MCDPConstants.test_ignore_graphviz_errors.') else: raise # MIME_GRAPHVIZ if do_dot: with f.data_file('dot', MIME_PLAIN) as filename: with open(filename, 'w') as f: f.write(s) return f
def get_dot_string(gg): with tmpfile(".dot") as filename_dot: if False: with open(filename_dot, 'w') as fo: gg.dot(fo) contents = open(filename_dot).read() else: contents = gg.dot2() contents = contents.replace('"<TABLE', '<<TABLE') contents = contents.replace('</TABLE>"', '</TABLE>>') return contents
def graphvizgen_plot(ggraph, output, prog='dot'): gg = gg_deepcopy(ggraph) with tmpfile(".dot") as filename_dot: with open(filename_dot, 'w') as f: gg.dot(f) try: graphviz_run(filename_dot, output, prog=prog) except: contents = open(filename_dot).read() s = get_md5(contents) filename = 'out-%s.dot' % s with open(filename, 'w') as f: f.write(contents) print('Saved problematic dot as %r.' % filename) raise
def nx_generic_graphviz_plot(G, output, prog='dot'): """ Converts to dot and writes on the file output """ with tmpfile(".dot") as filename_dot: nx.write_dot(G, filename_dot) # @UndefinedVariable graphviz_run(filename_dot, output, prog=prog)