Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Library: Add remove_if util
Browse files Browse the repository at this point in the history
  • Loading branch information
mawildoer committed Sep 5, 2024
1 parent d82e61e commit 78e4466
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/faebryk/library/has_multi_picker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import logging
from abc import abstractmethod
from typing import Callable, Mapping
from typing import Callable, Mapping, Self

import faebryk.library._F as F
from faebryk.core.module import Module
from faebryk.core.node import Node
from faebryk.core.trait import TraitImpl
from faebryk.libs.picker.picker import PickError
from faebryk.libs.picker.picker import PickError, has_part_picked_remove

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,3 +84,19 @@ def handle_duplicate(self, other: TraitImpl, node: Node) -> bool:
other.pickers.extend(self.pickers)
other.pickers.sort(key=lambda x: x[0])
return False

@classmethod
def remove_if[T: Module](
cls, m: T, condition: Callable[[T], bool], prio: int = -10
) -> Self:
def replace(module: Module):
assert module is m

if condition(module):
module.add_trait(has_part_picked_remove())

raise PickError("", m)

m.add(has_multi_picker(prio, has_multi_picker.FunctionPicker(replace)))

return m

0 comments on commit 78e4466

Please sign in to comment.