Changeset View
Changeset View
Standalone View
Standalone View
swh/graph/server/app.py
Show First 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | def get_edges(self): | ||||
node_type != "*" and node_type not in EXTENDED_SWHID_TYPES | node_type != "*" and node_type not in EXTENDED_SWHID_TYPES | ||||
for edge in s.split(":") | for edge in s.split(":") | ||||
for node_type in edge.split(",", maxsplit=1) | for node_type in edge.split(",", maxsplit=1) | ||||
] | ] | ||||
): | ): | ||||
raise aiohttp.web.HTTPBadRequest(text=f"invalid edge restriction: {s}") | raise aiohttp.web.HTTPBadRequest(text=f"invalid edge restriction: {s}") | ||||
return s | return s | ||||
def get_return_types(self): | |||||
"""Validate HTTP query parameter 'return types', i.e, | |||||
a set of types which we will filter the query results with""" | |||||
s = self.request.query.get("return_types", "*") | |||||
if any( | |||||
node_type != "*" and node_type not in EXTENDED_SWHID_TYPES | |||||
for node_type in s.split(",") | |||||
): | |||||
raise aiohttp.web.HTTPBadRequest( | |||||
vlorentz: no need to create a temporary list, instead we can use a generator expression to pass a… | |||||
text=f"invalid type for filtering res: {s}" | |||||
) | |||||
# if the user puts a star, | |||||
# then we filter nothing, we don't need the other information | |||||
if "*" in s: | |||||
Not Done Inline Actionsuse comments vlorentz: use comments | |||||
return "*" | |||||
else: | |||||
return s | |||||
def get_traversal(self): | def get_traversal(self): | ||||
"""Validate HTTP query parameter `traversal`, i.e., visit order""" | """Validate HTTP query parameter `traversal`, i.e., visit order""" | ||||
s = self.request.query.get("traversal", "dfs") | s = self.request.query.get("traversal", "dfs") | ||||
if s not in ("bfs", "dfs"): | if s not in ("bfs", "dfs"): | ||||
raise aiohttp.web.HTTPBadRequest(text=f"invalid traversal order: {s}") | raise aiohttp.web.HTTPBadRequest(text=f"invalid traversal order: {s}") | ||||
return s | return s | ||||
def get_limit(self): | def get_limit(self): | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | class SimpleTraversalView(StreamingGraphView): | ||||
simple_traversal_type: Optional[str] = None | simple_traversal_type: Optional[str] = None | ||||
async def prepare_response(self): | async def prepare_response(self): | ||||
src = self.request.match_info["src"] | src = self.request.match_info["src"] | ||||
self.src_node = self.node_of_swhid(src) | self.src_node = self.node_of_swhid(src) | ||||
self.edges = self.get_edges() | self.edges = self.get_edges() | ||||
self.direction = self.get_direction() | self.direction = self.get_direction() | ||||
self.return_types = self.get_return_types() | |||||
async def stream_response(self): | async def stream_response(self): | ||||
async for res_node in self.backend.simple_traversal( | async for res_node in self.backend.simple_traversal( | ||||
self.simple_traversal_type, self.direction, self.edges, self.src_node | self.simple_traversal_type, | ||||
self.direction, | |||||
self.edges, | |||||
self.src_node, | |||||
self.return_types, | |||||
): | ): | ||||
res_swhid = self.swhid_of_node(res_node) | res_swhid = self.swhid_of_node(res_node) | ||||
await self.stream_line(res_swhid) | await self.stream_line(res_swhid) | ||||
class LeavesView(SimpleTraversalView): | class LeavesView(SimpleTraversalView): | ||||
simple_traversal_type = "leaves" | simple_traversal_type = "leaves" | ||||
Show All 15 Lines | async def prepare_response(self): | ||||
self.dst_thing = self.node_of_swhid(dst) | self.dst_thing = self.node_of_swhid(dst) | ||||
else: | else: | ||||
self.dst_thing = dst | self.dst_thing = dst | ||||
self.edges = self.get_edges() | self.edges = self.get_edges() | ||||
self.direction = self.get_direction() | self.direction = self.get_direction() | ||||
self.algo = self.get_traversal() | self.algo = self.get_traversal() | ||||
self.limit = self.get_limit() | self.limit = self.get_limit() | ||||
self.return_types = self.get_return_types() | |||||
async def get_walk_iterator(self): | async def get_walk_iterator(self): | ||||
return self.backend.walk( | return self.backend.walk( | ||||
self.direction, self.edges, self.algo, self.src_node, self.dst_thing | self.direction, self.edges, self.algo, self.src_node, self.dst_thing | ||||
) | ) | ||||
async def stream_response(self): | async def stream_response(self): | ||||
it = self.get_walk_iterator() | it = self.get_walk_iterator() | ||||
Show All 13 Lines | async def stream_response(self): | ||||
count += 1 | count += 1 | ||||
else: | else: | ||||
break | break | ||||
class RandomWalkView(WalkView): | class RandomWalkView(WalkView): | ||||
def get_walk_iterator(self): | def get_walk_iterator(self): | ||||
return self.backend.random_walk( | return self.backend.random_walk( | ||||
self.direction, self.edges, RANDOM_RETRIES, self.src_node, self.dst_thing | self.direction, | ||||
self.edges, | |||||
RANDOM_RETRIES, | |||||
self.src_node, | |||||
self.dst_thing, | |||||
self.return_types, | |||||
) | ) | ||||
class VisitEdgesView(SimpleTraversalView): | class VisitEdgesView(SimpleTraversalView): | ||||
async def stream_response(self): | async def stream_response(self): | ||||
it = self.backend.visit_edges(self.direction, self.edges, self.src_node) | it = self.backend.visit_edges(self.direction, self.edges, self.src_node) | ||||
async for (res_src, res_dst) in it: | async for (res_src, res_dst) in it: | ||||
res_src_swhid = self.swhid_of_node(res_src) | res_src_swhid = self.swhid_of_node(res_src) | ||||
▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines |
no need to create a temporary list, instead we can use a generator expression to pass a generator to any.