Skip to content

Commit

Permalink
Eliminate dependency on nr-stream (#266)
Browse files Browse the repository at this point in the history
* Eliminate dependency on `nr-stream`

* improve docstring

* add .envrc

* add changelog
  • Loading branch information
NiklasRosenstein authored Aug 1, 2024
1 parent 8d0edc4 commit ad00bd6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ id = "b7f209cb-7ff7-414d-b368-bde8cb57109d"
type = "refactor"
description = "move `kraken.std.git.gitignore.parser` to `kraken.common.gitignore`"
author = "@NiklasRosenstein"

[[entries]]
id = "0a74ab07-f895-48e8-8842-6c5382ba8402"
type = "refactor"
description = "Eliminate `nr-stream` dependency"
author = "@NiklasRosenstein"
5 changes: 5 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

if which slap >/dev/null; then
eval "$(slap venv -a)"
fi
1 change: 0 additions & 1 deletion kraken-build/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ httpx = "^0.27.0"
keyring = "^25.0.0"
networkx = "^3.1"
nr-io-graphviz = "^0.1.1"
nr-stream = "^1.1.0"
packaging = "^23.1"
pex = "^2.1.156"
python = ">=3.10"
Expand Down
18 changes: 18 additions & 0 deletions kraken-build/src/kraken/common/iter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from collections.abc import Iterable
from itertools import filterfalse, tee
from typing import Callable, TypeVar

T_co = TypeVar("T_co", covariant=True)


def bipartition(predicate: Callable[[T_co], bool], it: Iterable[T_co]) -> tuple[Iterable[T_co], Iterable[T_co]]:
"""
Partition a stream into two separate streams based on a predicate.
Returns:
A tuple of two iterators, where the first iterator contains only the elements for which the *predicate*
returned `False`, and the second iterator contains only the elements for which it returned `True`.
"""

t1, t2 = tee(it)
return filterfalse(predicate, t1), filter(predicate, t2)
6 changes: 2 additions & 4 deletions kraken-build/src/kraken/core/system/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from pathlib import Path
from typing import Any, ClassVar, TypeAlias, TypeVar, overload

from nr.stream import Stream

from kraken.common import CurrentDirectoryProjectFinder, ProjectFinder, ScriptRunner
from kraken.core.address import Address, AddressSpace, resolve_address
from kraken.core.base import Currentable, MetadataContainer
Expand Down Expand Up @@ -303,11 +301,11 @@ def _resolve_single_address(
address = relative_to.concat(address).normalize(keep_container=True)

matches = list(resolve_address(space, self.root_project, address).matches())
tasks = Stream(matches).of_type(Task).collect() # type: ignore[type-abstract]
tasks = [t for t in matches if isinstance(t, Task)]
if set_selected:
for task in tasks:
task.selected = True
projects = Stream(matches).of_type(Project).collect()
projects = [p for p in matches if isinstance(p, Project)]
if projects:
# Using the address of a project means we want to select its default tasks
for proj in projects:
Expand Down
4 changes: 2 additions & 2 deletions kraken-build/src/kraken/core/system/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

from networkx import DiGraph, restricted_view, transitive_reduction
from networkx.algorithms import topological_sort
from nr.stream import Stream

from kraken.common import not_none
from kraken.common.iter import bipartition
from kraken.core.address import Address
from kraken.core.system.executor import Graph
from kraken.core.system.task import GroupTask, Task, TaskStatus, TaskTag
Expand Down Expand Up @@ -503,7 +503,7 @@ def ready(self) -> list[Task]:
# NOTE(NiklasRosenstein): We don't need to return GroupTasks, we can mark them as skipped right away.
# In a future version of Kraken, we want to represent groups not as task objects, so this special
# handling code will be obsolete.
result, groups = map(lambda x: list(x), Stream(tasks).bipartition(lambda t: isinstance(t, GroupTask)))
result, groups = map(lambda x: list(x), bipartition((lambda t: isinstance(t, GroupTask)), tasks))
for group in groups:
self.set_status(group, TaskStatus.skipped())
if not result:
Expand Down

0 comments on commit ad00bd6

Please sign in to comment.