def test_pipe_invalid_data(capsys, quiet, engine='dot', format_='svg'): with pytest.raises(subprocess.CalledProcessError) as e: graphviz.pipe(engine, format_, b'nongraph', quiet=quiet) assert e.value.returncode == 1 assert 'syntax error in line' in str(e.value) out, err = capsys.readouterr() assert out == '' if quiet: assert err == '' else: assert 'syntax error in line' in err
def rendered_content(self): source = super().rendered_content return graphviz.pipe( self.engine, self.format, source.encode("utf-8"), renderer=self.renderer, formatter=self.formatter, )
def test_pipe(capsys, engine, format_, renderer, formatter, pattern, data=b'graph { spam }'): out = graphviz.pipe(engine, format_, data, renderer, formatter).decode('ascii') if pattern is not None: assert re.match(pattern, out) assert capsys.readouterr() == ('', '')
def convert_dot_to_svg(version=None): if version is None: version = get_version() #print(version) for i in range(mlc.get_n_US_folds('PFT')): for j in range(n_graphs): input_filename = path_prefix + version + 'dot/' + str( i) + '/' + str(j) + '.dot' op_format, engine = 'svg', 'dot' svg = pipe(engine, op_format, read_DOT(input_filename)) #print(svg.decode('utf-8')) output_directory = path_prefix + version + 'svg/' + str(i) + '/' write_SVG(str(j) + '.svg', output_directory, svg.decode('utf-8'))
def test_pipe_pipe_invalid_data_mocked(mocker, sentinel, mock_run, quiet): mock_sys_stderr = mocker.patch('sys.stderr', autospec=True, flush=mocker.Mock(), encoding=sentinel.encoding) mock_out = mocker.create_autospec(bytes, instance=True, name='mock_out') mock_err = mocker.create_autospec(bytes, instance=True, name='mock_err', **{'__len__.return_value': 1}) mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD, returncode=5, stdout=mock_out, stderr=mock_err) with pytest.raises(subprocess.CalledProcessError) as e: graphviz.pipe('dot', 'png', b'nongraph', quiet=quiet) assert e.value.returncode == 5 assert e.value.cmd == _common.INVALID_CMD assert e.value.stdout is mock_out assert e.value.stderr is mock_err e.value.stdout = sentinel.new_stdout assert e.value.stdout is sentinel.new_stdout mock_run.assert_called_once_with( [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpng'], input=b'nongraph', stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=_common.StartupinfoMatcher()) if not quiet: mock_out.decode.assert_not_called() mock_err.decode.assert_called_once_with(sentinel.encoding) mock_sys_stderr.write.assert_called_once_with( mock_err.decode.return_value) mock_sys_stderr.flush.assert_called_once_with()
def test_pipe_mocked(capsys, mock_run, quiet): mock_run.return_value = subprocess.CompletedProcess(_common.INVALID_CMD, returncode=0, stdout=b'stdout', stderr=b'stderr') assert graphviz.pipe('dot', 'png', b'nongraph', quiet=quiet) == b'stdout' mock_run.assert_called_once_with( [_common.EXPECTED_DOT_BINARY, '-Kdot', '-Tpng'], input=b'nongraph', stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=_common.StartupinfoMatcher()) assert capsys.readouterr() == ('', '' if quiet else 'stderr')
def load_and_verify(version=None): if version is None: version = get_version() for i in range(mlc.get_n_US_folds('PFT')): for j in range(n_graphs): dot_filename = path_prefix + version + 'dot/' + str(i) + '/' + str( j) + '.dot' computed_svg = pipe('dot', 'svg', read_DOT(dot_filename)) svg_filename = path_prefix + version + 'svg/' + str(i) + '/' + str( j) + '.svg' loaded_svg = load_SVG(svg_filename) if loaded_svg != computed_svg.decode('utf-8'): print('Not matching : Model - ' + str(i) + ', Graph - ' + str(j)) return False print('Matched') return True
def render( source: gv.Source, out_path: str, format: str = None, engine: str = None, renderer: str = None, formatter: str = None, view: bool = False, ) -> None: """ Render source with Graphviz. Parameters ---------- source Graphviz source to render out_path path to visualisation file to write format Graphviz render file format engine Graphviz layout engine renderer Graphviz output renderer formatter Graphviz output formatter view after rendering, display with the default application """ image_bytes = gv.pipe( engine or source.engine, format, str(source).encode(), renderer=renderer, formatter=formatter, ) out_path = Path(out_path).with_suffix('.' + format) out_path.parent.mkdir(parents=True, exist_ok=True) out_path.write_bytes(image_bytes) if view: gv.view(str(out_path))
def visit_end_of_graph(self): self._dump_raw('}\n') if not self._dump_dot_only: import sys import tempfile def write_temp_file(suffix, data): fd, filename = tempfile.mkstemp(suffix=suffix) print('Writing "%s"...' % filename) with os.fdopen(fd, 'w') as fp: fp.write(data) print('Done! Please remember to remove the file.') return filename try: import graphviz except ImportError: # The fallback behavior if graphviz is not installed! print('Python graphviz not found. Please invoke') print(' $ pip install graphviz') print('in order to enable automatic conversion to HTML.') print() print('You may also convert DOT to SVG manually via') print(' $ dot -Tsvg input.dot -o output.svg') print() write_temp_file('.dot', self.output()) return svg = graphviz.pipe('dot', 'svg', self.output()) filename = write_temp_file( '.html', '<html><body bgcolor="%s">%s</body></html>' % ( '#1a1a1a' if self._dark_mode else 'white', svg)) if sys.platform == 'win32': os.startfile(filename) elif sys.platform == 'darwin': os.system('open "%s"' % filename) else: os.system('xdg-open "%s"' % filename)
def dot_to_svg(dot_str): return pipe('dot', 'svg', dot_str.encode()).decode('utf-8')