kifield/kifield.py
def insert_part_fields_into_lib_V6(
    part_fields_dict, filename, recurse, group_components, backup, no_range
):
    """Insert the fields in the extracted part dictionary into a KiCad V6 library."""

    logger.log(
        DEBUG_OVERVIEW,
        "Inserting extracted fields into library file {}.".format(filename),
    )

    # Get an existing library or abort. (There's no way we can create
    # a viable library file just from part field values.)
    try:
        lib = SchLib_V6(filename)
    except IOError:
        logger.warn("Library file {} not found.".format(filename))
        return

    # Go through all the library components, replacing field values and
    # adding new fields from the part fields dictionary.
    for component in lib.components:

        # Get fields for the part with the same name as this component (or an empty list).
        part_fields = part_fields_dict.get(component.name, {})

        # Insert the fields from the part dictionary into the component fields.
        for field_name, field_value in part_fields.items():

            # Get [V] (visible) or [I] (invisible) flag prepended to entire field name
            # or individual field value. If no flag, visibility is set to None.
            field_vis, field_name = re.match(r"(\[([VI])\])?(.*)", field_name).group(
                2, 3
            )
            value_vis, field_value = re.match(r"(\[([VI])\])?(.*)", field_value).group(
                2, 3
            )
            if value_vis == "V":
                total_vis = True
            elif value_vis == "I":
                total_vis = False
            else:
                if field_vis == "V":
                    total_vis = True
                elif field_vis == "I":
                    total_vis = False
                else:
                    total_vis = None

            # Search for an existing field with a matching name in the component.
            for f in component.fields:

                if f["name"].lower() == field_name.lower():
                    # Update existing named field in component.
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Updating {} field {} from {} to {}".format(
                            component.name, f["name"], f["value"], quote(field_value)
                        ),
                    )
                    f["value"] = field_value
                    component.set_field_value(f["name"], field_value)
                    component.set_field_visibility(f["name"], total_vis)
                    break  # To keep following else from creating a new field.

            # No existing field to update, so add a new field.
            else:
                if field_value not in (None, ""):
                    # Add new named field and value to component.
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Adding {} field {} with value {} with visibility {}".format(
                            component.name, f["name"], f["value"], total_vis
                        ),
                    )
                    component.copy_field("Reference", field_name)
                    component.set_field_value(field_name, field_value)
                    pos = component.get_field_pos(field_name)
                    field = component.get_field(field_name)
                    pos[1] += 2.54 * field["id"]
                    component.set_field_pos(field_name, pos)
                    component.set_field_visibility(field_name, total_vis)

        # Remove non-default fields with empty values.
        for field in component.fields:
            name = field["name"]
            if name.lower() in (
                "reference",
                "value",
                "footprint",
                "datasheet",
                "ki_description",
                "ki_fp_filters",
                "ki_keywords",
                "ki_locked",
            ):
                # Skip default fields so they aren't removed.
                continue
            if field["value"] in (None, ""):
                # Remove empty field.
                component.del_field(name)

    # Save the updated library.
    lib.save(backup, filename)
Insert the fields in the extracted part dictionary into a KiCad V6 library.
Insert the fields in the extracted part dictionary into a KiCad V6 library.
Insert the fields in the extracted part dictionary into a KiCad V6 library .
def insert_part_fields_into_lib_V6(
    part_fields_dict, filename, recurse, group_components, backup, no_range
):
    logger.log(
        DEBUG_OVERVIEW,
        "Inserting extracted fields into library file {}.".format(filename),
    )
    try:
        lib = SchLib_V6(filename)
    except IOError:
        logger.warn("Library file {} not found.".format(filename))
        return
    for component in lib.components:
        part_fields = part_fields_dict.get(component.name, {})
        for field_name, field_value in part_fields.items():
            field_vis, field_name = re.match(r"(\[([VI])\])?(.*)", field_name).group(
                2, 3
            )
            value_vis, field_value = re.match(r"(\[([VI])\])?(.*)", field_value).group(
                2, 3
            )
            if value_vis == "V":
                total_vis = True
            elif value_vis == "I":
                total_vis = False
            else:
                if field_vis == "V":
                    total_vis = True
                elif field_vis == "I":
                    total_vis = False
                else:
                    total_vis = None
            for f in component.fields:
                if f["name"].lower() == field_name.lower():
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Updating {} field {} from {} to {}".format(
                            component.name, f["name"], f["value"], quote(field_value)
                        ),
                    )
                    f["value"] = field_value
                    component.set_field_value(f["name"], field_value)
                    component.set_field_visibility(f["name"], total_vis)
                    break  
            else:
                if field_value not in (None, ""):
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Adding {} field {} with value {} with visibility {}".format(
                            component.name, f["name"], f["value"], total_vis
                        ),
                    )
                    component.copy_field("Reference", field_name)
                    component.set_field_value(field_name, field_value)
                    pos = component.get_field_pos(field_name)
                    field = component.get_field(field_name)
                    pos[1] += 2.54 * field["id"]
                    component.set_field_pos(field_name, pos)
                    component.set_field_visibility(field_name, total_vis)
        for field in component.fields:
            name = field["name"]
            if name.lower() in (
                "reference",
                "value",
                "footprint",
                "datasheet",
                "ki_description",
                "ki_fp_filters",
                "ki_keywords",
                "ki_locked",
            ):
                continue
            if field["value"] in (None, ""):
                component.del_field(name)
    lib.save(backup, filename)
def insert_part_fields_into_lib_V6 ( part_fields_dict , filename , recurse , group_components , backup , no_range ) : logger . log ( DEBUG_OVERVIEW , "Inserting extracted fields into library file {}." . format ( filename ) , ) try : lib = SchLib_V6 ( filename ) except IOError : logger . warn ( "Library file {} not found." . format ( filename ) ) return for component in lib . components : part_fields = part_fields_dict . get ( component . name , { } ) for field_name , field_value in part_fields . items ( ) : field_vis , field_name = re . match ( r"(\[([VI])\])?(.*)" , field_name ) . group ( 2 , 3 ) value_vis , field_value = re . match ( r"(\[([VI])\])?(.*)" , field_value ) . group ( 2 , 3 ) if value_vis == "V" : total_vis = True elif value_vis == "I" : total_vis = False else : if field_vis == "V" : total_vis = True elif field_vis == "I" : total_vis = False else : total_vis = None for f in component . fields : if f [ "name" ] . lower ( ) == field_name . lower ( ) : logger . log ( DEBUG_OBSESSIVE , "Updating {} field {} from {} to {}" . format ( component . name , f [ "name" ] , f [ "value" ] , quote ( field_value ) ) , ) f [ "value" ] = field_value component . set_field_value ( f [ "name" ] , field_value ) component . set_field_visibility ( f [ "name" ] , total_vis ) break else : if field_value not in ( None , "" ) : logger . log ( DEBUG_OBSESSIVE , "Adding {} field {} with value {} with visibility {}" . format ( component . name , f [ "name" ] , f [ "value" ] , total_vis ) , ) component . copy_field ( "Reference" , field_name ) component . set_field_value ( field_name , field_value ) pos = component . get_field_pos ( field_name ) field = component . get_field ( field_name ) pos [ 1 ] += 2.54 * field [ "id" ] component . set_field_pos ( field_name , pos ) component . set_field_visibility ( field_name , total_vis ) for field in component . fields : name = field [ "name" ] if name . lower ( ) in ( "reference" , "value" , "footprint" , "datasheet" , "ki_description" , "ki_fp_filters" , "ki_keywords" , "ki_locked" , ) : continue if field [ "value" ] in ( None , "" ) : component . del_field ( name ) lib . save ( backup , filename )
Insert the fields in the extracted part dictionary into a KiCad V6 library.
Insert the fields in the extracted part dictionary into a KiCad V6 library .
"""Insert the fields in the extracted part dictionary into a KiCad V6 library.""" # Get an existing library or abort. (There's no way we can create # a viable library file just from part field values.) # Go through all the library components, replacing field values and # adding new fields from the part fields dictionary. # Get fields for the part with the same name as this component (or an empty list). # Insert the fields from the part dictionary into the component fields. # Get [V] (visible) or [I] (invisible) flag prepended to entire field name # or individual field value. If no flag, visibility is set to None. # Search for an existing field with a matching name in the component. # Update existing named field in component. # To keep following else from creating a new field. # No existing field to update, so add a new field. # Add new named field and value to component. # Remove non-default fields with empty values. # Skip default fields so they aren't removed. # Remove empty field. # Save the updated library.
part_fields_dict
filename
recurse
group_components
backup
no_range