Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/http_naive_client.py
# Copyright (C) 2021 The Software Heritage developers | # Copyright (C) 2021-2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import functools | import functools | ||||
import inspect | import inspect | ||||
import itertools | |||||
import re | import re | ||||
import statistics | import statistics | ||||
from typing import ( | from typing import ( | ||||
Callable, | Callable, | ||||
Dict, | Dict, | ||||
Iterable, | Iterable, | ||||
Iterator, | Iterator, | ||||
List, | List, | ||||
▲ Show 20 Lines • Show All 129 Lines • ▼ Show 20 Lines | class NaiveClient: | ||||
@check_arguments | @check_arguments | ||||
def leaves( | def leaves( | ||||
self, | self, | ||||
src: str, | src: str, | ||||
edges: str = "*", | edges: str = "*", | ||||
direction: str = "forward", | direction: str = "forward", | ||||
max_edges: int = 0, | max_edges: int = 0, | ||||
return_types: str = "*", | return_types: str = "*", | ||||
max_matching_nodes: int = 0, | |||||
) -> Iterator[str]: | ) -> Iterator[str]: | ||||
# TODO: max_edges | # TODO: max_edges | ||||
yield from filter_node_types( | leaves = filter_node_types( | ||||
return_types, | return_types, | ||||
[ | [ | ||||
node | node | ||||
for node in self.graph.get_subgraph(src, edges, direction) | for node in self.graph.get_subgraph(src, edges, direction) | ||||
if not self.graph.get_filtered_neighbors(node, edges, direction) | if not self.graph.get_filtered_neighbors(node, edges, direction) | ||||
], | ], | ||||
) | ) | ||||
if max_matching_nodes > 0: | |||||
leaves = itertools.islice(leaves, max_matching_nodes) | |||||
return leaves | |||||
@check_arguments | @check_arguments | ||||
def neighbors( | def neighbors( | ||||
self, | self, | ||||
src: str, | src: str, | ||||
edges: str = "*", | edges: str = "*", | ||||
direction: str = "forward", | direction: str = "forward", | ||||
max_edges: int = 0, | max_edges: int = 0, | ||||
return_types: str = "*", | return_types: str = "*", | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | def random_walk( | ||||
direction: str = "forward", | direction: str = "forward", | ||||
limit: Optional[int] = None, | limit: Optional[int] = None, | ||||
): | ): | ||||
# TODO: limit | # TODO: limit | ||||
yield from self.walk(src, dst, edges, "dfs", direction, limit) | yield from self.walk(src, dst, edges, "dfs", direction, limit) | ||||
@check_arguments | @check_arguments | ||||
def count_leaves( | def count_leaves( | ||||
self, src: str, edges: str = "*", direction: str = "forward" | self, | ||||
src: str, | |||||
edges: str = "*", | |||||
direction: str = "forward", | |||||
max_matching_nodes: int = 0, | |||||
) -> int: | ) -> int: | ||||
return len(list(self.leaves(src, edges, direction))) | return len( | ||||
list( | |||||
self.leaves( | |||||
src, edges, direction, max_matching_nodes=max_matching_nodes | |||||
) | |||||
) | |||||
) | |||||
@check_arguments | @check_arguments | ||||
def count_neighbors( | def count_neighbors( | ||||
self, src: str, edges: str = "*", direction: str = "forward" | self, src: str, edges: str = "*", direction: str = "forward" | ||||
) -> int: | ) -> int: | ||||
return len(self.graph.get_filtered_neighbors(src, edges, direction)) | return len(self.graph.get_filtered_neighbors(src, edges, direction)) | ||||
@check_arguments | @check_arguments | ||||
▲ Show 20 Lines • Show All 132 Lines • Show Last 20 Lines |