From e0fa9c1003b3385d20b83fabd5355783ce31759f Mon Sep 17 00:00:00 2001 From: Matthew Wildoer Date: Thu, 31 Oct 2024 16:52:40 -0700 Subject: [PATCH] Protect indexing files --- .../exporters/schematic/kicad/transformer.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/faebryk/exporters/schematic/kicad/transformer.py b/src/faebryk/exporters/schematic/kicad/transformer.py index e98f44ff..9a2a17d8 100644 --- a/src/faebryk/exporters/schematic/kicad/transformer.py +++ b/src/faebryk/exporters/schematic/kicad/transformer.py @@ -107,7 +107,7 @@ def __init__( self.sch = sch self.graph = graph self.app = app - self._symbol_files_index: dict[str, Path] = {} + self._symbol_files_index: dict[str, Path] | None = None self.missing_symbols: list[F.Symbol] = [] @@ -185,6 +185,12 @@ def attach_symbol(self, f_symbol: F.Symbol, sym_inst: SCH.C_symbol_instance): def index_symbol_files( self, fp_lib_tables: PathLike | list[PathLike], load_globals: bool = True ) -> None: + """ + Index the symbol files in the given library tables + """ + if self._symbol_files_index is None: + self._symbol_files_index = {} + if isinstance(fp_lib_tables, (str, Path)): fp_lib_table_paths = [Path(fp_lib_tables)] else: @@ -229,8 +235,11 @@ def get_all_symbols(self) -> List[tuple[Module, F.Symbol]]: @once def get_symbol_file(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") + if lib_name not in self._symbol_files_index: - raise FaebrykException(f"Symbol file {lib_name} not found") + raise FaebrykException(f"Symbol file \"{lib_name}\" not found") path = self._symbol_files_index[lib_name] return C_kicad_sym_file.loads(path) @@ -369,9 +378,7 @@ def _mark[R: _HasUUID | _HasPropertys](cls, obj: R) -> R: if hasattr(obj, "propertys"): obj.propertys[cls.MARK_NAME] = C_property( - name=cls.MARK_NAME, - value=hashed_contents, - effects=C_effects(hide=True) + name=cls.MARK_NAME, value=hashed_contents, effects=C_effects(hide=True) ) return obj