'Write out an ascii graph of the provided spec.
Arguments:
spec -- spec to graph.  This only handles one spec at a time.
Optional arguments:
out -- file object to write out to (default is sys.stdout)
color -- whether to write in color.  Default is to autodetect
based on output file.'

def write(self, spec, color=None, out=None):

    if (out is None):
        out = sys.stdout
    if (color is None):
        color = out.isatty()
    self._out = ColorStream(out, color=color)
    topo_order = topological_sort(spec, reverse=True, deptype=self.deptype)
    spec = spec.copy()
    self._nodes = spec.index()
    self._name_to_color = dict(((name, self.colors[(i % len(self.colors))]) for (i, name) in enumerate(topo_order)))
    self._frontier = [[spec.name]]
    while self._frontier:
        i = find(self._frontier, (lambda f: (len(f) > 1)))
        if (i >= 0):
            back = []
            for d in self._frontier[i]:
                b = find(self._frontier[:i], (lambda f: (f == [d])))
                if (b != (-1)):
                    back.append((b, d))
            if back:
                back.sort()
                prev_ends = []
                collapse_l1 = False
                for (j, (b, d)) in enumerate(back):
                    self._frontier[i].remove(d)
                    if ((i - b) > 1):
                        collapse_l1 = any(((not e) for e in self._frontier))
                        self._back_edge_line(prev_ends, b, i, collapse_l1, 'left-1')
                        del prev_ends[:]
                    prev_ends.append(b)
                pop = (not self._frontier[i])
                collapse_l2 = pop
                if collapse_l1:
                    collapse_l2 = False
                if pop:
                    self._frontier.pop(i)
                self._back_edge_line(prev_ends, (-1), (-1), collapse_l2, 'left-2')
            elif (len(self._frontier[i]) > 1):
                if (((i + 1) < len(self._frontier)) and (len(self._frontier[(i + 1)]) == 1) and (self._frontier[(i + 1)][0] in self._frontier[i])):
                    name = self._frontier[(i + 1)][0]
                    self._frontier[i].remove(name)
                    self._merge_right_line(i)
                else:
                    name = self._frontier[i].pop(0)
                    deps = [name]
                    self._frontier.insert(i, deps)
                    self._expand_right_line(i)
                    self._frontier.pop(i)
                    self._connect_deps(i, deps, 'post-expand')
            j = (i + 1)
            while (j < len(self._frontier)):
                deps = self._frontier.pop(j)
                if (not self._connect_deps(j, deps, 'back-from-right')):
                    j += 1
        else:
            name = topo_order.pop()
            node = self._nodes[name]
            i = find(self._frontier, (lambda f: (name in f)))
            self._node_line(i, name)
            self._frontier.pop(i)
            deps = node.dependencies(self.deptype)
            if deps:
                deps = sorted((d.name for d in deps), reverse=True)
                self._connect_deps(i, deps, 'new-deps')
            elif self._frontier:
                self._collapse_line(i)
