From c89546e0f9b752c12bda3a89de9a2588d61fc85c Mon Sep 17 00:00:00 2001 From: iopapamanoglou Date: Wed, 28 Aug 2024 00:41:07 +0200 Subject: [PATCH] move around util import --- src/faebryk/core/node.py | 2 ++ src/faebryk/exporters/esphome/esphome.py | 6 +++-- src/faebryk/exporters/netlist/graph.py | 8 +++---- src/faebryk/exporters/netlist/netlist.py | 2 +- .../parameters/parameters_to_file.py | 2 +- .../exporters/pcb/kicad/transformer.py | 8 +++---- .../exporters/pcb/layout/typehierarchy.py | 2 +- src/faebryk/exporters/pcb/routing/routing.py | 7 +++--- src/faebryk/exporters/pcb/routing/util.py | 23 +++++++++++-------- src/faebryk/library/Mounting_Hole.py | 13 ++++++++--- src/faebryk/library/_F.py | 12 +++++----- src/faebryk/libs/app/parameters.py | 3 ++- src/faebryk/libs/picker/picker.py | 19 +++++++++++---- 13 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/faebryk/core/node.py b/src/faebryk/core/node.py index 09bcee4f..495a8645 100644 --- a/src/faebryk/core/node.py +++ b/src/faebryk/core/node.py @@ -477,3 +477,5 @@ def get_trait[V: "Trait"](self, trait: Type[V]) -> V: raise TraitNotFound(self, trait) return cast_assert(trait, impl) + + # Graph stuff ---------------------------------------------------------------------- diff --git a/src/faebryk/exporters/esphome/esphome.py b/src/faebryk/exporters/esphome/esphome.py index a28d942d..11b8dac2 100644 --- a/src/faebryk/exporters/esphome/esphome.py +++ b/src/faebryk/exporters/esphome/esphome.py @@ -6,8 +6,8 @@ import yaml -from faebryk.core.graphinterface import Graph, Parameter -from faebryk.core.util import get_all_nodes_with_trait +from faebryk.core.graphinterface import Graph +from faebryk.core.parameter import Parameter from faebryk.library.Constant import Constant from faebryk.library.has_esphome_config import has_esphome_config @@ -54,6 +54,8 @@ def merge_dicts(*dicts: dict) -> dict: def make_esphome_config(G: Graph) -> dict: + from faebryk.core.util import get_all_nodes_with_trait + esphome_components = get_all_nodes_with_trait(G, has_esphome_config) esphome_config = merge_dicts(*[t.get_config() for _, t in esphome_components]) diff --git a/src/faebryk/exporters/netlist/graph.py b/src/faebryk/exporters/netlist/graph.py index bf2b1aeb..1bb338e3 100644 --- a/src/faebryk/exporters/netlist/graph.py +++ b/src/faebryk/exporters/netlist/graph.py @@ -8,11 +8,6 @@ from faebryk.core.graphinterface import Graph from faebryk.core.module import Module -from faebryk.core.util import ( - get_all_nodes_with_trait, - get_children, - get_connected_mifs, -) from faebryk.exporters.netlist.netlist import T2Netlist from faebryk.library.Electrical import Electrical from faebryk.library.Footprint import Footprint @@ -112,6 +107,8 @@ def get_kicad_obj(self): def add_or_get_net(interface: Electrical): + from faebryk.core.util import get_connected_mifs + mifs = get_connected_mifs(interface.connected) nets = { p[0] @@ -128,6 +125,7 @@ def add_or_get_net(interface: Electrical): def attach_nets_and_kicad_info(g: Graph): + from faebryk.core.util import get_all_nodes_with_trait, get_children # g has to be closed Gclosed = g diff --git a/src/faebryk/exporters/netlist/netlist.py b/src/faebryk/exporters/netlist/netlist.py index f2ba6b62..cd8cb571 100644 --- a/src/faebryk/exporters/netlist/netlist.py +++ b/src/faebryk/exporters/netlist/netlist.py @@ -5,7 +5,6 @@ from dataclasses import dataclass from faebryk.core.graphinterface import Graph -from faebryk.core.util import get_all_nodes_of_type, get_all_nodes_with_trait from faebryk.library.has_footprint import has_footprint from faebryk.library.has_overriden_name import has_overriden_name @@ -38,6 +37,7 @@ class Vertex: def make_t2_netlist_from_graph(G: Graph) -> T2Netlist: + from faebryk.core.util import get_all_nodes_of_type, get_all_nodes_with_trait from faebryk.exporters.netlist.graph import can_represent_kicad_footprint from faebryk.library.Net import Net as FNet diff --git a/src/faebryk/exporters/parameters/parameters_to_file.py b/src/faebryk/exporters/parameters/parameters_to_file.py index 8a295dc3..1b35db65 100644 --- a/src/faebryk/exporters/parameters/parameters_to_file.py +++ b/src/faebryk/exporters/parameters/parameters_to_file.py @@ -6,13 +6,13 @@ from faebryk.core.module import Module from faebryk.core.parameter import Parameter -from faebryk.core.util import get_all_modules, get_children logger = logging.getLogger(__name__) def export_parameters_to_file(module: Module, path: Path): """Write all parameters of the given module to a file.""" + from faebryk.core.util import get_all_modules, get_children # {module_name: [{param_name: param_value}, {param_name: param_value},...]} parameters = dict[str, list[dict[str, Parameter]]]() diff --git a/src/faebryk/exporters/pcb/kicad/transformer.py b/src/faebryk/exporters/pcb/kicad/transformer.py index f03c4984..a16ccfb4 100644 --- a/src/faebryk/exporters/pcb/kicad/transformer.py +++ b/src/faebryk/exporters/pcb/kicad/transformer.py @@ -19,11 +19,6 @@ from faebryk.core.module import Module from faebryk.core.moduleinterface import ModuleInterface from faebryk.core.node import Node -from faebryk.core.util import ( - get_all_nodes_with_trait, - get_all_nodes_with_traits, - get_children, -) from faebryk.libs.geometry.basic import Geometry from faebryk.libs.kicad.fileformats import ( UUID, @@ -230,6 +225,7 @@ def attach(self): footprints = { (f.propertys["Reference"].value, f.name): f for f in self.pcb.footprints } + from faebryk.core.util import get_all_nodes_with_trait, get_children for node, fpt in get_all_nodes_with_trait(self.graph, F.has_footprint): if not node.has_trait(F.has_overriden_name): @@ -701,6 +697,8 @@ def insert_zone(self, net: Net, layers: str | list[str], polygon: list[Point2D]) # Positioning ---------------------------------------------------------------------- def move_footprints(self): + from faebryk.core.util import get_all_nodes_with_traits + # position modules with defined positions pos_mods = get_all_nodes_with_traits( self.graph, (F.has_pcb_position, self.has_linked_kicad_footprint) diff --git a/src/faebryk/exporters/pcb/layout/typehierarchy.py b/src/faebryk/exporters/pcb/layout/typehierarchy.py index fe286ba7..3eb1f6e3 100644 --- a/src/faebryk/exporters/pcb/layout/typehierarchy.py +++ b/src/faebryk/exporters/pcb/layout/typehierarchy.py @@ -6,7 +6,6 @@ from faebryk.core.module import Module from faebryk.core.node import Node -from faebryk.core.util import get_node_direct_children from faebryk.exporters.pcb.layout.layout import Layout from faebryk.libs.util import NotNone, find_or, flatten, groupby @@ -27,6 +26,7 @@ def apply(self, *node: Node): """ Tip: Make sure at least one parent of node has an absolute position defined """ + from faebryk.core.util import get_node_direct_children # Find the layout for each node and group by matched level levels = groupby( diff --git a/src/faebryk/exporters/pcb/routing/routing.py b/src/faebryk/exporters/pcb/routing/routing.py index 57b76576..177f72b4 100644 --- a/src/faebryk/exporters/pcb/routing/routing.py +++ b/src/faebryk/exporters/pcb/routing/routing.py @@ -10,10 +10,6 @@ import networkx as nx -from faebryk.core.util import ( - get_all_nodes_of_type, - get_net, -) from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer from faebryk.exporters.pcb.routing.grid import ( Coord, @@ -129,6 +125,7 @@ def draw_circle(self, coord: OutCoord, size=0.5, layer="User.9"): ) def route_all(self): + from faebryk.core.util import get_all_nodes_of_type from faebryk.library.Net import Net as FNet nets = get_all_nodes_of_type(self.transformer.graph, FNet) @@ -239,6 +236,8 @@ def route_net(self, net: Net): ) def route_if_net(self, mif): + from faebryk.core.util import get_net + net = get_net(mif) assert net is not None self.route_net(net) diff --git a/src/faebryk/exporters/pcb/routing/util.py b/src/faebryk/exporters/pcb/routing/util.py index 4f8bdeef..824a3868 100644 --- a/src/faebryk/exporters/pcb/routing/util.py +++ b/src/faebryk/exporters/pcb/routing/util.py @@ -3,23 +3,19 @@ import logging from dataclasses import dataclass -from typing import Iterable, Sequence +from typing import TYPE_CHECKING, Iterable, Sequence from faebryk.core.module import Module from faebryk.core.moduleinterface import ModuleInterface from faebryk.core.node import Node -from faebryk.core.util import ( - get_all_nodes, - get_connected_mifs, - get_net, - get_parent_of_type, -) -from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer from faebryk.library.Electrical import Electrical from faebryk.library.Net import Net from faebryk.library.Pad import Pad from faebryk.libs.geometry.basic import Geometry +if TYPE_CHECKING: + from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer + # logging settings logger = logging.getLogger(__name__) @@ -119,7 +115,7 @@ def net(self): return net -def apply_route_in_pcb(route: Route, transformer: PCB_Transformer): +def apply_route_in_pcb(route: Route, transformer: "PCB_Transformer"): pcb_net = transformer.get_net(route.net) logger.debug(f"Insert tracks for net {pcb_net.name}, {pcb_net.number}, {route}") @@ -180,6 +176,11 @@ def get_internal_nets_of_node( For Nets returns all connected mifs """ + from faebryk.core.util import ( + get_all_nodes, + get_connected_mifs, + get_net, + ) from faebryk.libs.util import groupby if isinstance(node, Net): @@ -192,6 +193,8 @@ def get_internal_nets_of_node( def get_pads_pos_of_mifs(mifs: Sequence[Electrical]): + from faebryk.exporters.pcb.kicad.transformer import PCB_Transformer + return { pad_pos[0]: pad_pos[1] for mif in mifs @@ -215,6 +218,8 @@ def group_pads_that_are_connected_already( def get_routes_of_pad(pad: Pad): + from faebryk.core.util import get_parent_of_type + return { route for mif in pad.pcb.get_direct_connections() diff --git a/src/faebryk/library/Mounting_Hole.py b/src/faebryk/library/Mounting_Hole.py index 87c7e8c1..2babd569 100644 --- a/src/faebryk/library/Mounting_Hole.py +++ b/src/faebryk/library/Mounting_Hole.py @@ -21,6 +21,13 @@ def __preinit__(self): # Only 3.2mm supported for now self.diameter.merge(F.Constant(3.2 * P.mm)) - footprint = L.f_field(F.has_footprint_defined)( - F.KicadFootprint("MountingHole:MountingHole_3.2mm_M3_Pad", pin_names=[]) - ) + # footprint = L.f_field(F.has_footprint_defined)( + # F.KicadFootprint("MountingHole:MountingHole_3.2mm_M3_Pad", pin_names=[]) + # ) + + # TODO make back to f_field, rt_field because of imports + @L.rt_field + def footprint(self): + return F.has_footprint_defined( + F.KicadFootprint("MountingHole:MountingHole_3.2mm_M3_Pad", pin_names=[]) + ) diff --git a/src/faebryk/library/_F.py b/src/faebryk/library/_F.py index 670f73b8..634431b9 100644 --- a/src/faebryk/library/_F.py +++ b/src/faebryk/library/_F.py @@ -16,10 +16,10 @@ # flake8: noqa: E501 from faebryk.library.has_pcb_position import has_pcb_position -from faebryk.library.Constant import Constant from faebryk.library.Operation import Operation -from faebryk.library.TBD import TBD from faebryk.library.Range import Range +from faebryk.library.TBD import TBD +from faebryk.library.Constant import Constant from faebryk.library.has_overriden_name import has_overriden_name from faebryk.library.has_capacitance import has_capacitance from faebryk.library.has_footprint import has_footprint @@ -46,9 +46,9 @@ from faebryk.library.has_pcb_position_defined_relative import has_pcb_position_defined_relative from faebryk.library.has_pcb_position_defined import has_pcb_position_defined from faebryk.library.has_pcb_position_defined_relative_to_parent import has_pcb_position_defined_relative_to_parent +from faebryk.library.Logic import Logic from faebryk.library.Electrical import Electrical from faebryk.library.ANY import ANY -from faebryk.library.Logic import Logic from faebryk.library.Set import Set from faebryk.library.has_overriden_name_defined import has_overriden_name_defined from faebryk.library.has_defined_capacitance import has_defined_capacitance @@ -73,10 +73,10 @@ from faebryk.library.has_footprint_requirement_defined import has_footprint_requirement_defined from faebryk.library.has_multi_picker import has_multi_picker from faebryk.library.is_representable_by_single_value_defined import is_representable_by_single_value_defined +from faebryk.library.LogicOps import LogicOps from faebryk.library.has_pin_association_heuristic import has_pin_association_heuristic from faebryk.library.SPI import SPI from faebryk.library.DifferentialPair import DifferentialPair -from faebryk.library.LogicOps import LogicOps from faebryk.library.has_footprint_impl import has_footprint_impl from faebryk.library.can_attach_to_footprint import can_attach_to_footprint from faebryk.library.has_kicad_footprint import has_kicad_footprint @@ -90,19 +90,19 @@ from faebryk.library.Pad import Pad from faebryk.library.GDT import GDT from faebryk.library.Button import Button +from faebryk.library.LogicGate import LogicGate from faebryk.library.has_pin_association_heuristic_lookup_table import has_pin_association_heuristic_lookup_table from faebryk.library.RS485 import RS485 from faebryk.library.Ethernet import Ethernet -from faebryk.library.LogicGate import LogicGate from faebryk.library.has_footprint_defined import has_footprint_defined from faebryk.library.Net import Net from faebryk.library.has_equal_pins import has_equal_pins from faebryk.library.has_kicad_manual_footprint import has_kicad_manual_footprint from faebryk.library.can_attach_via_pinmap_pinlist import can_attach_via_pinmap_pinlist +from faebryk.library.LogicGates import LogicGates from faebryk.library.MOSFET import MOSFET from faebryk.library.Diode import Diode from faebryk.library.BJT import BJT -from faebryk.library.LogicGates import LogicGates from faebryk.library.can_attach_to_footprint_via_pinmap import can_attach_to_footprint_via_pinmap from faebryk.library.can_attach_to_footprint_symmetrically import can_attach_to_footprint_symmetrically from faebryk.library.has_pcb_routing_strategy_via_to_layer import has_pcb_routing_strategy_via_to_layer diff --git a/src/faebryk/libs/app/parameters.py b/src/faebryk/libs/app/parameters.py index 40cb59ee..79d1f3b4 100644 --- a/src/faebryk/libs/app/parameters.py +++ b/src/faebryk/libs/app/parameters.py @@ -5,7 +5,6 @@ from faebryk.core.module import Module from faebryk.core.parameter import Parameter -from faebryk.core.util import get_all_modules, get_children from faebryk.library.ANY import ANY from faebryk.library.TBD import TBD @@ -19,6 +18,8 @@ def replace_tbd_with_any(module: Module, recursive: bool, loglvl: int | None = N :param module: The module to replace TBD instances in. :param recursive: If True, replace TBD instances in submodules as well. """ + from faebryk.core.util import get_all_modules, get_children + lvl = logger.getEffectiveLevel() if loglvl is not None: logger.setLevel(loglvl) diff --git a/src/faebryk/libs/picker/picker.py b/src/faebryk/libs/picker/picker.py index 68cd3702..0a36e665 100644 --- a/src/faebryk/libs/picker/picker.py +++ b/src/faebryk/libs/picker/picker.py @@ -16,11 +16,6 @@ from faebryk.core.module import Module from faebryk.core.moduleinterface import ModuleInterface from faebryk.core.parameter import Parameter -from faebryk.core.util import ( - get_all_modules, - get_children, - pretty_params, -) from faebryk.libs.util import NotNone, flatten logger = logging.getLogger(__name__) @@ -92,6 +87,8 @@ def get_all_children(self): class PickErrorParams(PickError): def __init__(self, module: Module, options: list[PickerOption]): + from faebryk.core.util import pretty_params + self.options = options MAX = 5 @@ -142,6 +139,8 @@ def get_part(self) -> Part: def pick_module_by_params(module: Module, options: Iterable[PickerOption]): + from faebryk.core.util import get_children + if module.has_trait(has_part_picked): logger.debug(f"Ignoring already picked module: {module}") return @@ -185,6 +184,8 @@ def pick_module_by_params(module: Module, options: Iterable[PickerOption]): def _get_mif_top_level_modules(mif: ModuleInterface) -> set[Module]: + from faebryk.core.util import get_children + return get_children(mif, direct_only=True, types=Module) | { m for nmif in get_children(mif, direct_only=True, types=ModuleInterface) @@ -200,10 +201,14 @@ def __init__(self): @classmethod def from_module(cls, module: Module) -> "PickerProgress": self = cls() + from faebryk.core.util import get_all_modules + self.progress.update(self.task, total=len(get_all_modules(module))) return self def advance(self, module: Module): + from faebryk.core.util import get_all_modules + self.progress.advance(self.task, len(get_all_modules(module))) @contextmanager @@ -226,6 +231,8 @@ def pick_part_recursively(module: Module): # check if lowest children are picked def get_not_picked(m: Module): + from faebryk.core.util import get_children + ms = m.get_most_special() # check if parent is picked @@ -259,6 +266,8 @@ def get_not_picked(m: Module): def _pick_part_recursively(module: Module, progress: PickerProgress | None = None): + from faebryk.core.util import get_children + assert isinstance(module, Module) # pick only for most specialized module