Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Casper Weiss Bang
committed
Apr 14, 2021
1 parent
4dfa5f6
commit a7342f2
Showing
5 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python3 | ||
""" | ||
These filters look at the depth of a graph, and | ||
return new graphs that are based on the so-called depth of a given node. | ||
With "depth" we mean how close to the root of a graph it is, i.e | ||
a depth=1 would only take the top-level modules, where depth 2 | ||
would take all nodes that are direct children of top-level modules. | ||
Depth can be found by recursively checking if the parent has a parent, | ||
and if yes check that parent too, and return how many steps one has to | ||
progress before returning a null value. | ||
""" | ||
|
||
from codoc.domain.helpers import get_node | ||
from codoc.domain.model import Graph, Node | ||
|
||
from .types import FilterType | ||
|
||
from .helpers import get_edges_where_both_ends_are_in_nodes | ||
|
||
|
||
def get_depth_based_filter(depth: int) -> FilterType: | ||
def depth_based_filter(graph: Graph) -> Graph: | ||
nodes = set( | ||
node for node in graph.nodes if get_depth_of_node(node, graph) <= depth | ||
) | ||
edges = get_edges_where_both_ends_are_in_nodes(graph.edges, nodes) | ||
return Graph(nodes=nodes, edges=edges) | ||
|
||
return depth_based_filter | ||
|
||
|
||
def get_depth_of_node(node: Node, graph: Graph) -> int: | ||
parent_id = node.parent_identifier | ||
if parent_id is None: | ||
return 1 | ||
parent = parent_id = get_node(parent_id, graph) | ||
return get_depth_of_node(parent, graph) + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,35 @@ | ||
#!/usr/bin/env python3 | ||
from codoc.domain.model import Node | ||
from typing import Set | ||
|
||
from codoc.domain.model import Dependency, Node | ||
|
||
|
||
def node_without_parent(node: Node) -> Node: | ||
kwargs = node.__dict__ | ||
kwargs["parent_identifier"] = None | ||
return Node(**kwargs) | ||
|
||
|
||
def get_edges_where_both_ends_are_in_nodes(edges: Set[Dependency], nodes: Set[Node]): | ||
node_identifiers = set(node.identifier for node in nodes) | ||
return set( | ||
edge | ||
for edge in edges | ||
if is_both_edges_of_edge_in_list_of_nodes(edge, node_identifiers) | ||
) | ||
|
||
|
||
def is_both_edges_of_edge_in_list_of_nodes( | ||
edge: Dependency, node_identifiers: Set[str] | ||
) -> bool: | ||
is_from_node_internal = edge.from_node in node_identifiers | ||
is_to_node_internal = edge.to_node in node_identifiers | ||
return is_from_node_internal and is_to_node_internal | ||
|
||
|
||
def is_either_edges_of_edge_in_list_of_nodes( | ||
edge: Dependency, node_identifiers: Set[str] | ||
) -> bool: | ||
is_from_node_internal = edge.from_node in node_identifiers | ||
is_to_node_internal = edge.to_node in node_identifiers | ||
return is_from_node_internal or is_to_node_internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters