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

Commit

Permalink
mif fixes step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
iopapamanoglou committed Nov 5, 2024
1 parent 70a8ef9 commit aa87ff2
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 39 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,5 @@ _local

# OS stuff
.DS_Store

.envrc
5 changes: 0 additions & 5 deletions src/faebryk/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@

logger = logging.getLogger(__name__)

LINK_TB = ConfigFlag(
"LINK_TB",
False,
"Save stack trace for each link. Warning: Very slow! Just use for debug",
)
ID_REPR = ConfigFlag("ID_REPR", False, "Add object id to repr")


Expand Down
2 changes: 1 addition & 1 deletion src/faebryk/core/cpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ def cleanup():
set_leak_warnings(bool(LEAK_WARNINGS))


at_exit(cleanup)
at_exit(cleanup, on_exception=False)
1 change: 1 addition & 0 deletions src/faebryk/core/cpp/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class GraphInterface:
def __repr__(self) -> str: ...
def get_graph(self) -> Graph: ...
def get_gif_edges(self) -> set[GraphInterface]: ...
def get_direct_connections(self) -> set[GraphInterface]: ...
@property
def edges(self) -> dict[GraphInterface, Link]: ...
@property
Expand Down
2 changes: 2 additions & 0 deletions src/faebryk/core/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ PYMOD(m) {
.def("__repr__", &GI::repr)
.def("get_graph", &GI::get_graph)
.def("get_gif_edges", &GI::get_gif_edges)
// TODO deprecate
.def("get_direct_connections", &GI::get_gif_edges)
.def_prop_ro("edges", &GI::get_edges)
.def_prop_rw("node", &GI::get_node, &GI::set_node)
.def("is_connected", &GI::is_connected)
Expand Down
50 changes: 27 additions & 23 deletions src/faebryk/core/moduleinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
from typing import (
Iterable,
Sequence,
cast,
)

from typing_extensions import Self

from faebryk.core.core import LINK_TB
from faebryk.core.graphinterface import (
GraphInterface,
GraphInterfaceHierarchical,
)
from faebryk.core.link import (
Link,
LinkDirect,
LinkDirectShallow,
LinkDirectConditional,
LinkFilteredException,
_TLinkDirectShallow,
)
from faebryk.core.node import Node
from faebryk.core.node import CNode, Node
from faebryk.core.trait import Trait
from faebryk.libs.util import cast_assert, once, print_stack
from faebryk.libs.util import cast_assert, once

logger = logging.getLogger(__name__)

Expand All @@ -41,12 +40,12 @@ def _resolve_link_transitive(links: Iterable[type[Link]]) -> type[Link]:
if len(uniq) == 1:
return next(iter(uniq))

if is_type_set_subclasses(uniq, {_TLinkDirectShallow}):
if is_type_set_subclasses(uniq, {LinkDirectConditional}):
# TODO this only works if the filter is identical
raise NotImplementedError()

if is_type_set_subclasses(uniq, {LinkDirect, _TLinkDirectShallow}):
return [u for u in uniq if issubclass(u, _TLinkDirectShallow)][0]
if is_type_set_subclasses(uniq, {LinkDirect, LinkDirectConditional}):
return [u for u in uniq if issubclass(u, LinkDirectConditional)][0]

raise NotImplementedError()

Expand All @@ -61,8 +60,8 @@ def _resolve_link_duplicate(links: Iterable[type[Link]]) -> type[Link]:
if len(uniq) == 1:
return next(iter(uniq))

if is_type_set_subclasses(uniq, {LinkDirect, _TLinkDirectShallow}):
return [u for u in uniq if not issubclass(u, _TLinkDirectShallow)][0]
if is_type_set_subclasses(uniq, {LinkDirect, LinkDirectConditional}):
return [u for u in uniq if not issubclass(u, LinkDirectConditional)][0]

raise NotImplementedError()

Expand Down Expand Up @@ -116,20 +115,27 @@ class TraitT(Trait): ...
specialized: GraphInterface
connected: GraphInterfaceModuleConnection

# TODO rename
@classmethod
@once
def LinkDirectShallow(cls):
class _LinkDirectShallow(LinkDirectConditional):
"""
Make link that only connects up but not down
"""

def test(node: Node):
return not any(isinstance(p[0], cls) for p in node.get_hierarchy()[:-1])
def test(self, node: CNode):
return not any(
isinstance(p[0], self.type_test) for p in node.get_hierarchy()[:-1]
)

def __init__(self, type_test: type["ModuleInterface"]):
self.type_test = type_test
super().__init__(lambda src, dst: self.test(dst.node))

class _LinkDirectShallowMif(
LinkDirectShallow(lambda link, gif: test(gif.node))
): ...
# TODO rename
@classmethod
@once
def LinkDirectShallow(cls):
class _LinkDirectShallowMif(ModuleInterface._LinkDirectShallow):
def __init__(self):
super().__init__(cls)

return _LinkDirectShallowMif

Expand Down Expand Up @@ -278,16 +284,14 @@ def _connect_across_hierarchies(
resolved = _resolve_link_duplicate([type(existing_link), linkcls])
if resolved is type(existing_link):
return
if LINK_TB:
print(print_stack(existing_link.tb))
raise NotImplementedError(
"Overriding existing links not implemented, tried to override "
+ f"{existing_link} with {resolved}"
)

# level 0 connect
try:
self.connected.connect(other.connected, linkcls=linkcls)
self.connected.connect(other.connected, linkcls())
except LinkFilteredException:
return

Expand Down Expand Up @@ -360,4 +364,4 @@ def specialize[T: ModuleInterface](self, special: T) -> T:
# Establish sibling relationship
self.specialized.connect(special.specializes)

return special
return cast(T, special)
8 changes: 1 addition & 7 deletions src/faebryk/libs/app/erc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from faebryk.core.moduleinterface import ModuleInterface
from faebryk.library.Operation import Operation
from faebryk.libs.picker.picker import has_part_picked
from faebryk.libs.util import groupby, print_stack
from faebryk.libs.util import groupby

logger = logging.getLogger(__name__)

Expand All @@ -26,14 +26,8 @@ class ERCFaultShort(ERCFault):
def __init__(self, faulting_ifs: Sequence[ModuleInterface], *args: object) -> None:
link = faulting_ifs[0].is_connected_to(faulting_ifs[1])
assert link
from faebryk.core.core import LINK_TB

stack = ""
if LINK_TB:
stack = print_stack(link.tb)

super().__init__(faulting_ifs, *args)
print(stack)


class ERCFaultElectricPowerUndefinedVoltage(ERCFault):
Expand Down
5 changes: 3 additions & 2 deletions src/faebryk/libs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,15 +646,16 @@ def __call__(self, *args, **kwargs) -> Any:
return self.f(*args, **kwargs)


def at_exit(func: Callable):
def at_exit(func: Callable, on_exception: bool = True):
import atexit
import sys

f = CallOnce(func)

atexit.register(f)
hook = sys.excepthook
sys.excepthook = lambda *args: (f(), hook(*args))
if on_exception:
sys.excepthook = lambda *args: (f(), hook(*args))

# get main thread
import threading
Expand Down
14 changes: 13 additions & 1 deletion test/core/cpp/test_importcpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class SubNode(Node):
print(sn.a)


def test_derived_pynodes():
from faebryk.core.moduleinterface import ModuleInterface

mif1 = ModuleInterface()
mif2 = ModuleInterface()

mif1.connect(mif2)

print(mif1)


def test_cobject():
from faebryk.core.cpp import (
GraphInterface,
Expand Down Expand Up @@ -70,4 +81,5 @@ def test_cobject():
# test_add()
# test_cobject()
# test_cnodes()
test_pynode()
# test_pynode()
test_derived_pynodes()

0 comments on commit aa87ff2

Please sign in to comment.