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

Commit

Permalink
Improve lib indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
mawildoer committed Nov 6, 2024
1 parent d44e838 commit e42b4fd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions examples/dev_schematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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()

Expand Down
46 changes: 25 additions & 21 deletions src/faebryk/exporters/schematic/kicad/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand All @@ -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]]:
Expand All @@ -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")
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions test/exporters/schematic/kicad/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit e42b4fd

Please sign in to comment.