def save(self, filename): """ Save the svg of this tree visualization into filename argument. Mac platform can save any file type (.pdf, .png, .svg). Other platforms would fail with errors. See https://github.com/parrt/dtreeviz/issues/4 """ path = Path(filename) if not path.parent.exists: os.makedirs(path.parent) g = graphviz.Source(self.dot, format='svg') dotfilename = g.save(directory=path.parent.as_posix(), filename=path.stem) format = path.suffix[1:] # ".svg" -> "svg" etc... if not filename.endswith(".svg"): # Mac I think could do any format if we required: # brew reinstall pango librsvg cairo raise (Exception(f"{PLATFORM} can only save .svg files: {filename}")) # Gen .svg file from .dot but output .svg has image refs to other files cmd = ["dot", f"-T{format}", "-o", filename, dotfilename] # print(' '.join(cmd)) run(cmd, capture_output=True, check=True, quiet=False) if filename.endswith(".svg"): # now merge in referenced SVG images to make all-in-one file with open(filename, encoding='UTF-8') as f: svg = f.read() svg = inline_svg_images(svg) with open(filename, "w", encoding='UTF-8') as f: f.write(svg)
def save(self, filename): path = Path(filename) if not path.parent.exists: makedirs(path.parent) format = path.suffix[1:] # ".svg" -> "svg" etc... if format == 'svg': pdffilename = f"{path.parent}/{path.stem}.pdf" g = graphviz.Source(self.dot, format='pdf') g.render(directory=path.parent, filename=path.stem, view=False, cleanup=True) # cmd = ["pdftocairo", "-svg", pdffilename, filename] cmd = ["pdf2svg", pdffilename, filename] # print(' '.join(cmd)) # print(f"pdftocairo -svg {pdffilename} {filename}") stdout, stderr = run(cmd, capture_output=True, check=True, quiet=False) remove(pdffilename) else: g = graphviz.Source(self.dot, format=format) fname = g.save(directory=path.parent, filename=path.stem) cmd = ["dot", "-Tpng", "-o", filename, fname] # print(' '.join(cmd)) stdout, stderr = run(cmd, capture_output=True, check=True, quiet=False)
def savefig(self, filename): path = Path(filename) if not path.parent.exists: os.makedirs(path.parent) dotfilename = self.save(directory=path.parent.as_posix(), filename=path.stem) format = path.suffix[1:] # ".svg" -> "svg" etc... cmd = ["dot", f"-T{format}", "-o", filename, dotfilename] # print(' '.join(cmd)) run(cmd, capture_output=True, check=True, quiet=False)
def test_run_encoding_mocked(mocker, Popen, input=u'sp\xe4m', encoding='utf-8'): proc = Popen.return_value proc.returncode = 0 mocks = [ mocker.create_autospec(bytes, instance=True, name=n) for n in ('out', 'err') ] proc.communicate.return_value = mocks result = run(mocker.sentinel.cmd, input=input, capture_output=True, encoding=encoding) Popen.assert_called_once_with(mocker.sentinel.cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=mocker.ANY) check_startupinfo(Popen.call_args.kwargs['startupinfo']) proc.communicate.assert_called_once_with(input.encode(encoding)) assert result == tuple(m.decode.return_value for m in mocks) for m in mocks: m.decode.assert_called_once_with(encoding)
def save(self, filename): """ Save the svg of this tree visualization into filename argument. Mac platform can save any file type (.pdf, .png, .svg). Other platforms would fail with errors. See https://github.com/parrt/dtreeviz/issues/4 """ path = Path(filename) if not path.parent.exists: makedirs(path.parent) g = graphviz.Source(self.dot, format='svg') dotfilename = g.save(directory=path.parent.as_posix(), filename=path.stem) if PLATFORM == 'darwin': # dot seems broken in terms of fonts if we use -Tsvg. Force users to # brew install graphviz with librsvg (else metrics are off) and # use -Tsvg:cairo which fixes bug and also automatically embeds images format = path.suffix[1:] # ".svg" -> "svg" etc... cmd = ["dot", f"-T{format}:cairo", "-o", filename, dotfilename] # print(' '.join(cmd)) stdout, stderr = run(cmd, capture_output=True, check=True, quiet=False) else: if not filename.endswith(".svg"): raise (Exception( f"{PLATFORM} can only save .svg files: {filename}")) # Gen .svg file from .dot but output .svg has image refs to other files #orig_svgfilename = filename.replace('.svg', '-orig.svg') cmd = ["dot", "-Tsvg", "-o", filename, dotfilename] # print(' '.join(cmd)) stdout, stderr = run(cmd, capture_output=True, check=True, quiet=False) # now merge in referenced SVG images to make all-in-one file with open(filename, encoding='UTF-8') as f: svg = f.read() svg = inline_svg_images(svg) with open(filename, "w", encoding='UTF-8') as f: f.write(svg)
def test_run_oserror(): with pytest.raises(OSError) as e: run(['']) assert e.value.errno in (errno.EACCES, errno.EINVAL)
def test_run_oserror(): with pytest.raises(OSError) as e: run(['']) assert e.value.errno in (errno.EACCES, errno.EINVAL)