from typing import Any from ee import EeException from ee.kicad.read_schematic import read_schematic from ee.kicad.to_bom import to_bom, to_bom_xml from .model import * __all__ = [ "Position", "ComponentField", "Component", "Schematic", "read_schematic", "to_bom", "to_bom_xml", "to_pandas", ] def to_pandas(obj: Any, **kwarg): import pandas def run_filter(filters, obj): for f in filters: if not f(obj): return False return True def to_pandas_schematic(sch: Schematic): # These fields will always be put first. special_fields = ["ref", "ref_type", "ref_num", "value", "footprint"] def make_dict(c: Component): fields = { "ref": c.ref, "ref_type": c.ref_type, "ref_num": c.ref_num, "value": c.value, "footprint": c.footprint, } fields.update({f.name: f.value for f in c.fields if f.is_custom}) return fields components = sch.components filters = [] include_pwr = kwarg.get("include_pwr", False) if not include_pwr: filters.append(lambda c: not c.is_pwr) include_flg = kwarg.get("include_flg", False) if not include_flg: filters.append(lambda c: c.ref_type != "#FLG") data = [make_dict(c) for c in components if run_filter(filters, c)] columns = set([key for row in data for key in list(row)]) - set(special_fields) columns = special_fields + list(columns) return pandas.DataFrame(data=data, columns=columns). \ set_index("ref"). \ sort_values(["ref_type", "ref_num"]) if isinstance(obj, Schematic): return to_pandas_schematic(obj) else: raise EeException("Unsupported type: {}".format(type(obj)))