def _programs_default(self): """ Trait initaliser. """ progs = find_graphviz() if progs is None: logger.warning("GraphViz's executables not found") return {} else: return progs
def create(self, dotdata, prog="dot", format="xdot"): """ Creates and returns a representation of the graph using the Graphviz layout program given by 'prog', according to the given format. Writes the graph to a temporary dot file and processes it with the program given by 'prog' (which defaults to 'dot'), reading the output and returning it as a string if the operation is successful. On failure None is returned. Based on PyDot by Ero Carrera. """ import os, tempfile from dot2tex.dotparsing import find_graphviz # Map Graphviz executable names to their paths. progs = find_graphviz() if progs is None: logger.warning("GraphViz executables not found.") return None if not progs.has_key(prog): logger.warning('Invalid program [%s]. Available programs are: %s' % \ (prog, progs.keys())) return None # Make a temporary file ... tmp_fd, tmp_name = tempfile.mkstemp() os.close(tmp_fd) # ... and save the graph to it. dot_fd = file(tmp_name, "w+b") dot_fd.write(dotdata) # DOT language. dot_fd.close() # Get the temporary file directory name. tmp_dir = os.path.dirname(tmp_name) # Process the file using the layout program, specifying the format. p = subprocess.Popen((progs[prog], '-T' + format, tmp_name), cwd=tmp_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stderr = p.stderr stdout = p.stdout # Make sense of the standard output form the process. stdout_output = list() while True: data = stdout.read() if not data: break stdout_output.append(data) stdout.close() if stdout_output: stdout_output = ''.join(stdout_output) # Similarly so for any standard error. if not stderr.closed: stderr_output = list() while True: data = stderr.read() if not data: break stderr_output.append(data) stderr.close() if stderr_output: stderr_output = ''.join(stderr_output) status = p.wait() if status != 0: logger.error("Program [%s] terminated with status: %d. stderr " \ "follows: %s" % ( prog, status, stderr_output ) ) elif stderr_output: logger.error("%s", stderr_output) # Remove the temporary file. os.unlink(tmp_name) return stdout_output
def create(self, dotdata, prog="dot", format="xdot"): """ Creates and returns a representation of the graph using the Graphviz layout program given by 'prog', according to the given format. Writes the graph to a temporary dot file and processes it with the program given by 'prog' (which defaults to 'dot'), reading the output and returning it as a string if the operation is successful. On failure None is returned. Based on PyDot by Ero Carrera. """ import os, tempfile from dot2tex.dotparsing import find_graphviz # Map Graphviz executable names to their paths. progs = find_graphviz() if progs is None: logger.warning("GraphViz executables not found.") return None if not progs.has_key(prog): logger.warning('Invalid program [%s]. Available programs are: %s' % \ (prog, progs.keys())) return None # Make a temporary file ... tmp_fd, tmp_name = tempfile.mkstemp() os.close(tmp_fd) # ... and save the graph to it. dot_fd = file(tmp_name, "w+b") dot_fd.write(dotdata) # DOT language. dot_fd.close() # Get the temporary file directory name. tmp_dir = os.path.dirname(tmp_name) # Process the file using the layout program, specifying the format. p = subprocess.Popen((progs[prog], '-T'+format, tmp_name), cwd=tmp_dir, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stderr = p.stderr stdout = p.stdout # Make sense of the standard output form the process. stdout_output = list() while True: data = stdout.read() if not data: break stdout_output.append(data) stdout.close() if stdout_output: stdout_output = ''.join(stdout_output) # Similarly so for any standard error. if not stderr.closed: stderr_output = list() while True: data = stderr.read() if not data: break stderr_output.append(data) stderr.close() if stderr_output: stderr_output = ''.join(stderr_output) status = p.wait() if status != 0 : logger.error("Program [%s] terminated with status: %d. stderr " \ "follows: %s" % ( prog, status, stderr_output ) ) elif stderr_output: logger.error( "%s", stderr_output ) # Remove the temporary file. os.unlink(tmp_name) return stdout_output