From e42b4fd6f3dc685f7ffcc376f03ac66c9a8479a7 Mon Sep 17 00:00:00 2001 From: Matthew Wildoer Date: Tue, 5 Nov 2024 20:26:03 -0800 Subject: [PATCH] Improve lib indexing --- examples/dev_schematics.py | 4 +- .../exporters/schematic/kicad/transformer.py | 46 ++++++++++--------- .../schematic/kicad/test_transformer.py | 16 +++---- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/examples/dev_schematics.py b/examples/dev_schematics.py index 49f06eb3..f8c6e696 100644 --- a/examples/dev_schematics.py +++ b/examples/dev_schematics.py @@ -21,8 +21,8 @@ def build(module) -> Path: build_dir = Path(".") / "build" + lib_path = build_dir / "kicad" / "libs" - fp_lib_path_path = build_dir / "kicad/source/fp-lib-table" sch_file = C_kicad_sch_file.skeleton() logger.info("Building app") @@ -33,7 +33,7 @@ def build(module) -> Path: logger.info("Generating schematic") full_transformer = Transformer(sch_file.kicad_sch, app.get_graph(), app) - full_transformer.index_symbol_files(fp_lib_path_path, load_globals=False) + full_transformer.index_symbol_files(lib_path, load_globals=False) full_transformer.generate_schematic() diff --git a/src/faebryk/exporters/schematic/kicad/transformer.py b/src/faebryk/exporters/schematic/kicad/transformer.py index eb325347..0ca5022f 100644 --- a/src/faebryk/exporters/schematic/kicad/transformer.py +++ b/src/faebryk/exporters/schematic/kicad/transformer.py @@ -181,22 +181,9 @@ def attach_symbol(self, f_symbol: F.Symbol, sym_inst: SCH.C_symbol_instance): Transformer.has_linked_sch_pins([pin], sym_inst) ) - def index_symbol_libraries( - self, symbol_lib_paths: PathLike | list[PathLike] - ) -> None: - """Index the symbol libraries""" - if isinstance(symbol_lib_paths, (str, Path)): - symbol_lib_paths = [Path(symbol_lib_paths)] - else: - symbol_lib_paths = [Path(p) for p in symbol_lib_paths] - - for path in symbol_lib_paths: - self._symbol_files_index[path.stem] = path - def index_symbol_files( self, symbol_lib_paths: PathLike | list[PathLike], - symbol_lib_search_paths: PathLike | list[PathLike], load_globals: bool = False, ) -> None: """ @@ -210,13 +197,26 @@ def index_symbol_files( if self._symbol_files_index is None: self._symbol_files_index = {} - if isinstance(symbol_lib_search_paths, (str, Path)): - symbol_lib_search_paths = [Path(symbol_lib_search_paths)] + if isinstance(symbol_lib_paths, (str, Path)): + symbol_lib_paths = [Path(symbol_lib_paths)] else: - symbol_lib_search_paths = [Path(p) for p in symbol_lib_search_paths] + symbol_lib_paths = [Path(p) for p in symbol_lib_paths] - for path in chain(symbol_lib_search_paths, symbol_lib_paths): - self.index_symbol_libraries(path) + sym_paths = [p.glob("*.kicad_sym") for p in symbol_lib_paths] + [ + symbol_lib_paths + ] + + for path in chain.from_iterable(sym_paths): + assert isinstance(path, Path) + if not path.exists() or not path.is_file(): + continue + + try: + self.get_symbol_file(path) + except Exception: + continue + + self._symbol_files_index[path.stem] = path @staticmethod def flipped[T](input_list: list[tuple[T, int]]) -> list[tuple[T, int]]: @@ -234,7 +234,11 @@ def get_all_symbols(self) -> List[tuple[Module, F.Symbol]]: ] @once - def get_symbol_file(self, lib_name: str) -> C_kicad_sym_file: + def get_symbol_file(self, path: Path) -> C_kicad_sym_file: + return C_kicad_sym_file.loads(path) + + @once + def get_symbol_by_name(self, lib_name: str) -> C_kicad_sym_file: # primary caching handled by @once if self._symbol_files_index is None: raise ValueError("Symbol files index not indexed") @@ -243,7 +247,7 @@ def get_symbol_file(self, lib_name: str) -> C_kicad_sym_file: raise FaebrykException(f'Symbol file "{lib_name}" not found') path = self._symbol_files_index[lib_name] - return C_kicad_sym_file.loads(path) + return self.get_symbol_file(path) @staticmethod def get_sub_syms( @@ -517,7 +521,7 @@ def _ensure_lib_symbol( lib_name, symbol_name = lib_id.split(":") lib_sym = deepcopy( - self.get_symbol_file(lib_name).kicad_symbol_lib.symbols[symbol_name] + self.get_symbol_by_name(lib_name).kicad_symbol_lib.symbols[symbol_name] ) lib_sym.name = lib_id self.sch.lib_symbols.symbols[lib_id] = lib_sym diff --git a/test/exporters/schematic/kicad/test_transformer.py b/test/exporters/schematic/kicad/test_transformer.py index a13bcc35..aed06c25 100644 --- a/test/exporters/schematic/kicad/test_transformer.py +++ b/test/exporters/schematic/kicad/test_transformer.py @@ -30,8 +30,8 @@ def test_dir(): @pytest.fixture -def fp_lib_path_path(test_dir: Path): - return test_dir / "common/resources/fp-lib-table" +def sch_libs_path(test_dir: Path): + return test_dir / "common" / "libs" @pytest.fixture @@ -64,24 +64,24 @@ def test_wire_transformer(transformer: Transformer): ] -def test_index_symbol_files(transformer: Transformer, fp_lib_path_path: Path): +def test_index_symbol_files(transformer: Transformer, sch_libs_path: Path): assert transformer._symbol_files_index is None - transformer.index_symbol_files(fp_lib_path_path, load_globals=False) + transformer.index_symbol_files(sch_libs_path, load_globals=False) assert transformer._symbol_files_index is not None assert len(transformer._symbol_files_index) == 1 @pytest.fixture -def full_transformer(transformer: Transformer, fp_lib_path_path: Path): - transformer.index_symbol_files(fp_lib_path_path, load_globals=False) +def full_transformer(transformer: Transformer, sch_libs_path: Path): + transformer.index_symbol_files(sch_libs_path, load_globals=False) return transformer def test_get_symbol_file(full_transformer: Transformer): with pytest.raises(FaebrykException): - full_transformer.get_symbol_file("notta-lib") + full_transformer.get_symbol_by_name("notta-lib") - sym_flie = full_transformer.get_symbol_file("test") + sym_flie = full_transformer.get_symbol_by_name("test") assert ( sym_flie.kicad_symbol_lib.symbols["AudioJack-CUI-SJ-3523-SMT"].name == "AudioJack-CUI-SJ-3523-SMT"