How to use the importlab.graph.NodeSet function in importlab

To help you get started, we’ve selected a few importlab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / importlab / tests / test_graph.py View on Github external
def test_simple_cycle(self):
        g = FakeImportGraph(SIMPLE_CYCLIC_DEPS)
        g.add_file_recursive("a.py")
        g.build()
        cycles = [x for x, ys in g.deps_list()
                  if isinstance(x, graph.NodeSet)]
        self.assertEqual(len(cycles), 1)
        self.assertEqual(set(cycles[0].nodes), set(["a.py", "b.py"]))
        self.assertEqual(g.get_all_unresolved(), set(["e", "f"]))
        sources = g.ordered_sorted_source_files()
        self.check_order(sources, ["d.py"], ["a.py", "b.py"])
        self.check_order(sources, ["c.py"], ["a.py", "b.py"])
github google / importlab / tests / test_graph.py View on Github external
def test_simple_cycle(self):
        g = FakeImportGraph(SIMPLE_CYCLIC_DEPS)
        g.add_file_recursive("a.py")
        g.build()
        cycles = [x for x, ys in g.deps_list()
                  if isinstance(x, graph.NodeSet)]
        self.assertEqual(len(cycles), 1)
        self.assertEqual(set(cycles[0].nodes), set(["a.py", "b.py"]))
        self.assertEqual(g.get_all_unresolved(), set(["e", "f"]))
        sources = g.ordered_sorted_source_files()
        self.check_order(sources, ["d.py"], ["a.py", "b.py"])
        self.check_order(sources, ["c.py"], ["a.py", "b.py"])
github google / importlab / importlab / output.py View on Github external
def format_node(import_graph, node, indent):
    """Helper function for print_tree"""
    if isinstance(node, graph.NodeSet):
        ind = '  ' * indent
        out = [ind + 'cycle {'] + [
                format_file_node(import_graph, n, indent + 1)
                for n in node.nodes
        ] + [ind + '}']
        return '\n'.join(out)
    else:
        return format_file_node(import_graph, node, indent)
github google / importlab / importlab / graph.py View on Github external
def sorted_source_files(self):
        """Returns a list of targets in topologically sorted order."""

        assert self.final, 'Call build() before using the graph.'
        out = []
        for node in nx.topological_sort(self.graph):
            if isinstance(node, NodeSet):
                out.append(node.nodes)
            else:
                # add a one-element list for uniformity
                out.append([node])
        return list(reversed(out))
github google / importlab / importlab / graph.py View on Github external
def transform_node(node):
            if isinstance(node, Cycle):
                return NodeSet(node)
            else:
                return node
        self.graph = nx.relabel_nodes(self.graph, transform_node)
github google / importlab / importlab / graph.py View on Github external
def build(self):
        """Finalise the graph, after adding all input files to it."""

        assert not self.final, 'Trying to mutate a final graph.'

        # Replace each strongly connected component with a single node `NodeSet`
        for scc in sorted(nx.kosaraju_strongly_connected_components(self.graph),
                          key=len, reverse=True):
            if len(scc) == 1:
                break

            self.shrink_to_node(NodeSet(scc))

        self.final = True
github google / importlab / importlab / graph.py View on Github external
def sorted_source_files(self):
        """Returns a list of targets in topologically sorted order."""

        assert self.final, 'Call build() before using the graph.'
        out = []
        for node in nx.topological_sort(self.graph):
            if isinstance(node, NodeSet):
                out.append(node.nodes)
            elif node.endswith('.py'):
                # add a one-element list for uniformity
                out.append([node])
            else:
                # We don't care about other deps
                pass
        return list(reversed(out))
github google / importlab / importlab / graph.py View on Github external
def format(self, node):
        if isinstance(node, (Cycle, NodeSet)):
            return node.pp()
        else:
            return node