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

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

    if backup:
        create_backup(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(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:
        component_name = component.definition["name"]

        # 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 the field id associated with this field name (if there is one).
            field_id = lib_field_name_to_id.get(field_name, None)

            # Search for an existing field with a matching name in the component.
            for id, f in enumerate(component.fields):

                if unquote(f.get("fieldname", "")).lower() == field_name.lower():
                    # Update existing named field in component.
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Updating {} field {} from {} to {}".format(
                            component_name, field_name, f["name"], quote(field_value)
                        ),
                    )
                    f["name"] = quote(field_value)
                    break

                elif str(id) == field_id:
                    if id == 0:
                        # Update the F0 field of the component.
                        logger.log(
                            DEBUG_OBSESSIVE,
                            "Updating {} field {} from {} to {}".format(
                                component_name,
                                field_id,
                                f["reference"],
                                quote(field_value),
                            ),
                        )
                        f["reference"] = quote(field_value)
                    else:
                        # Update one of the F1, F2, or F3 fields in the component.
                        logger.log(
                            DEBUG_OBSESSIVE,
                            "Updating {} field {} from {} to {}".format(
                                component_name, field_id, f["name"], quote(field_value)
                            ),
                        )
                        f["name"] = quote(field_value)
                    break

            # No existing field to update, so add a new field.
            else:
                if field_value not in (None, ""):
                    # Copy an existing field from the component and then
                    # update its name and value to create a new field.
                    new_field = deepcopy(component.fields[-1])
                    new_field["fieldname"] = quote(field_name)
                    new_field["name"] = quote(field_value)
                    component.fields.append(new_field)
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Adding {} field {} with value {}".format(
                            component_name, field_name, quote(field_value)
                        ),
                    )

        # Remove any named fields with empty values.
        component.fields = [
            f
            for f in component.fields
            if unquote(f.get("fieldname", None)) in (None, "", "~")
            or unquote(f.get("name", None)) not in (None, "")
        ]

    # Save the updated library.
    lib.save(filename)
Insert the fields in the extracted part dictionary into a library.
Insert the fields in the extracted part dictionary into a library.
Insert the fields in the extracted part dictionary into a library .
def insert_part_fields_into_lib(
    part_fields_dict, filename, recurse, group_components, backup, no_range
):
    logger.log(
        DEBUG_OVERVIEW,
        "Inserting extracted fields into library file {}.".format(filename),
    )
    if backup:
        create_backup(filename)
    try:
        lib = SchLib(filename)
    except IOError:
        logger.warn("Library file {} not found.".format(filename))
        return
    for component in lib.components:
        component_name = component.definition["name"]
        part_fields = part_fields_dict.get(component_name, {})
        for field_name, field_value in part_fields.items():
            field_id = lib_field_name_to_id.get(field_name, None)
            for id, f in enumerate(component.fields):
                if unquote(f.get("fieldname", "")).lower() == field_name.lower():
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Updating {} field {} from {} to {}".format(
                            component_name, field_name, f["name"], quote(field_value)
                        ),
                    )
                    f["name"] = quote(field_value)
                    break
                elif str(id) == field_id:
                    if id == 0:
                        logger.log(
                            DEBUG_OBSESSIVE,
                            "Updating {} field {} from {} to {}".format(
                                component_name,
                                field_id,
                                f["reference"],
                                quote(field_value),
                            ),
                        )
                        f["reference"] = quote(field_value)
                    else:
                        logger.log(
                            DEBUG_OBSESSIVE,
                            "Updating {} field {} from {} to {}".format(
                                component_name, field_id, f["name"], quote(field_value)
                            ),
                        )
                        f["name"] = quote(field_value)
                    break
            else:
                if field_value not in (None, ""):
                    new_field = deepcopy(component.fields[-1])
                    new_field["fieldname"] = quote(field_name)
                    new_field["name"] = quote(field_value)
                    component.fields.append(new_field)
                    logger.log(
                        DEBUG_OBSESSIVE,
                        "Adding {} field {} with value {}".format(
                            component_name, field_name, quote(field_value)
                        ),
                    )
        component.fields = [
            f
            for f in component.fields
            if unquote(f.get("fieldname", None)) in (None, "", "~")
            or unquote(f.get("name", None)) not in (None, "")
        ]
    lib.save(filename)
def insert_part_fields_into_lib ( part_fields_dict , filename , recurse , group_components , backup , no_range ) : logger . log ( DEBUG_OVERVIEW , "Inserting extracted fields into library file {}." . format ( filename ) , ) if backup : create_backup ( filename ) try : lib = SchLib ( filename ) except IOError : logger . warn ( "Library file {} not found." . format ( filename ) ) return for component in lib . components : component_name = component . definition [ "name" ] part_fields = part_fields_dict . get ( component_name , { } ) for field_name , field_value in part_fields . items ( ) : field_id = lib_field_name_to_id . get ( field_name , None ) for id , f in enumerate ( component . fields ) : if unquote ( f . get ( "fieldname" , "" ) ) . lower ( ) == field_name . lower ( ) : logger . log ( DEBUG_OBSESSIVE , "Updating {} field {} from {} to {}" . format ( component_name , field_name , f [ "name" ] , quote ( field_value ) ) , ) f [ "name" ] = quote ( field_value ) break elif str ( id ) == field_id : if id == 0 : logger . log ( DEBUG_OBSESSIVE , "Updating {} field {} from {} to {}" . format ( component_name , field_id , f [ "reference" ] , quote ( field_value ) , ) , ) f [ "reference" ] = quote ( field_value ) else : logger . log ( DEBUG_OBSESSIVE , "Updating {} field {} from {} to {}" . format ( component_name , field_id , f [ "name" ] , quote ( field_value ) ) , ) f [ "name" ] = quote ( field_value ) break else : if field_value not in ( None , "" ) : new_field = deepcopy ( component . fields [ - 1 ] ) new_field [ "fieldname" ] = quote ( field_name ) new_field [ "name" ] = quote ( field_value ) component . fields . append ( new_field ) logger . log ( DEBUG_OBSESSIVE , "Adding {} field {} with value {}" . format ( component_name , field_name , quote ( field_value ) ) , ) component . fields = [ f for f in component . fields if unquote ( f . get ( "fieldname" , None ) ) in ( None , "" , "~" ) or unquote ( f . get ( "name" , None ) ) not in ( None , "" ) ] lib . save ( filename )
Insert the fields in the extracted part dictionary into a library.
Insert the fields in the extracted part dictionary into a library .
"""Insert the fields in the extracted part dictionary into a 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 the field id associated with this field name (if there is one). # Search for an existing field with a matching name in the component. # Update existing named field in component. # Update the F0 field of the component. # Update one of the F1, F2, or F3 fields in the component. # No existing field to update, so add a new field. # Copy an existing field from the component and then # update its name and value to create a new field. # Remove any named fields with empty values. # Save the updated library.
part_fields_dict
filename
recurse
group_components
backup
no_range