kifield/kifield.py
def extract_part_fields_from_sch(
    filename, inc_field_names=None, exc_field_names=None, recurse=False, depth=0
):
    """Return a dictionary of part fields extracted from a schematic."""

    logger.log(
        DEBUG_OVERVIEW,
        "Extracting fields {}, -{} from schematic file {}.".format(
            inc_field_names, exc_field_names, filename
        ),
    )

    part_fields_dict = {}  # Start with an empty part fields dictionary.

    sch = Schematic(filename)  # Read in the schematic.

    # Get all the part fields in the schematic and keep only the desired ones.
    # Remove the reference field (F0) from the list because that's used as as the dict key.
    field_names = sch.get_field_names()
    cull_list(field_names, None, ["reference"])
    cull_list(field_names, inc_field_names, exc_field_names)

    # Go through each component of the schematic, extracting its fields.
    for component in sch.components:

        # Get the fields and their values from the component.
        part_fields = {}
        for f in component.fields:
            value = unquote(f["ref"])
            name = unquote(f["name"])

            # Store the field and its value if the field name is in the list of
            # allowed fields.
            if name in field_names:
                part_fields[name] = value

        # Create a dictionary entry for each ref and assign the part fields to it.
        for ref in component.get_refs():
            if ref[0] == "#" or ref[-1] == "?":
                continue  # Skip pseudo-parts (e.g. power nets) and unallocated parts.

            # Some components (like resistor arrays) contain multiple units.
            # Add the fields for a unit to the part fields dict. By the end of
            # the loop, the part fields dict will have the union of the fields
            # of all of the units.
            part_fields.update(part_fields_dict.get(ref, {}))
            part_fields_dict[ref] = part_fields

    # If this schematic references other schematic sheets, then extract the part fields from those.
    if recurse:
        for sheet in sch.sheets:
            for field in sheet.fields:
                if field["id"] == "F1":
                    sheet_file = os.path.join(
                        os.path.dirname(filename), unquote(field["value"])
                    )
                    part_fields_dict.update(
                        extract_part_fields_from_sch(
                            sheet_file,
                            inc_field_names,
                            exc_field_names,
                            recurse,
                            depth + 1,
                        )
                    )
                    break

    # Print part fields for debugging if this is the top-level sheet of the schematic.
    if depth == 0:
        if logger.isEnabledFor(DEBUG_DETAILED):
            print("Extracted Part Fields:")
            pprint(part_fields_dict)

    return part_fields_dict
Return a dictionary of part fields extracted from a schematic.
Return a dictionary of part fields extracted from a schematic.
Return a dictionary of part fields extracted from a schematic .
def extract_part_fields_from_sch(
    filename, inc_field_names=None, exc_field_names=None, recurse=False, depth=0
):
    logger.log(
        DEBUG_OVERVIEW,
        "Extracting fields {}, -{} from schematic file {}.".format(
            inc_field_names, exc_field_names, filename
        ),
    )
    part_fields_dict = {}  
    sch = Schematic(filename)  
    field_names = sch.get_field_names()
    cull_list(field_names, None, ["reference"])
    cull_list(field_names, inc_field_names, exc_field_names)
    for component in sch.components:
        part_fields = {}
        for f in component.fields:
            value = unquote(f["ref"])
            name = unquote(f["name"])
            if name in field_names:
                part_fields[name] = value
        for ref in component.get_refs():
            if ref[0] == "#" or ref[-1] == "?":
                continue  
            part_fields.update(part_fields_dict.get(ref, {}))
            part_fields_dict[ref] = part_fields
    if recurse:
        for sheet in sch.sheets:
            for field in sheet.fields:
                if field["id"] == "F1":
                    sheet_file = os.path.join(
                        os.path.dirname(filename), unquote(field["value"])
                    )
                    part_fields_dict.update(
                        extract_part_fields_from_sch(
                            sheet_file,
                            inc_field_names,
                            exc_field_names,
                            recurse,
                            depth + 1,
                        )
                    )
                    break
    if depth == 0:
        if logger.isEnabledFor(DEBUG_DETAILED):
            print("Extracted Part Fields:")
            pprint(part_fields_dict)
    return part_fields_dict
def extract_part_fields_from_sch ( filename , inc_field_names = None , exc_field_names = None , recurse = False , depth = 0 ) : logger . log ( DEBUG_OVERVIEW , "Extracting fields {}, -{} from schematic file {}." . format ( inc_field_names , exc_field_names , filename ) , ) part_fields_dict = { } sch = Schematic ( filename ) field_names = sch . get_field_names ( ) cull_list ( field_names , None , [ "reference" ] ) cull_list ( field_names , inc_field_names , exc_field_names ) for component in sch . components : part_fields = { } for f in component . fields : value = unquote ( f [ "ref" ] ) name = unquote ( f [ "name" ] ) if name in field_names : part_fields [ name ] = value for ref in component . get_refs ( ) : if ref [ 0 ] == "#" or ref [ - 1 ] == "?" : continue part_fields . update ( part_fields_dict . get ( ref , { } ) ) part_fields_dict [ ref ] = part_fields if recurse : for sheet in sch . sheets : for field in sheet . fields : if field [ "id" ] == "F1" : sheet_file = os . path . join ( os . path . dirname ( filename ) , unquote ( field [ "value" ] ) ) part_fields_dict . update ( extract_part_fields_from_sch ( sheet_file , inc_field_names , exc_field_names , recurse , depth + 1 , ) ) break if depth == 0 : if logger . isEnabledFor ( DEBUG_DETAILED ) : print ( "Extracted Part Fields:" ) pprint ( part_fields_dict ) return part_fields_dict
Return a dictionary of part fields extracted from a schematic.
Return a dictionary of part fields extracted from a schematic .
"""Return a dictionary of part fields extracted from a schematic.""" # Start with an empty part fields dictionary. # Read in the schematic. # Get all the part fields in the schematic and keep only the desired ones. # Remove the reference field (F0) from the list because that's used as as the dict key. # Go through each component of the schematic, extracting its fields. # Get the fields and their values from the component. # Store the field and its value if the field name is in the list of # allowed fields. # Create a dictionary entry for each ref and assign the part fields to it. # Skip pseudo-parts (e.g. power nets) and unallocated parts. # Some components (like resistor arrays) contain multiple units. # Add the fields for a unit to the part fields dict. By the end of # the loop, the part fields dict will have the union of the fields # of all of the units. # If this schematic references other schematic sheets, then extract the part fields from those. # Print part fields for debugging if this is the top-level sheet of the schematic.
filename
inc_field_names
exc_field_names
recurse
depth