Example #1
0
def import_bom(api: Api, input_file: str) -> None:
    """
    Parse SolidWorks BOM export into pandas DataFrame.

    Upload each row from the excel file into ION as an MBOM item. If the part being
    referenced by the MBOM item does not exist, create that as well.

    Args:
        input_file (str): Path to SolidWorks BOM to be imported

    Returns:
        bool: Returns true if the BOM was successfully imported
    """
    # Read SolidWorks Level as string to correctly parse hierarchy.
    df = pd.read_excel(input_file, dtype={'Level': str, 'Part Number': str})
    # Use level as index
    df.set_index('Level', inplace=True)
    # Get top level part from file name
    top_level_part_number = args.input_file.split('/')[-1].split('.')[0]

    # TODO: Replace access token fetching with just using the Api object
    # and abstract access tokens and requests from business logic
    # Get API token
    access_token = api.get_access_token()
    part_dict, part_numbers = _get_parts_info(access_token, df,
                                              top_level_part_number)
    _create_mbom_items(access_token, df, part_dict, part_numbers)
    return True
Example #2
0
def import_values(api: Api, input_file: str) -> None:
    df = get_parts_df(input_file)
    logging.info('Starting part importer.')
    to_upload = get_upload_dict(df)
    to_upload = create_upload_items(df, to_upload)
    # Get API token
    access_token = api.get_access_token()
    cache = {}
    # Get pre existing uoms
    to_upload['uoms'], cache = get_existing_items(
        auth_token=access_token, values=to_upload['uoms'], unique_field='type',
        query_type='GET_UNITS_OF_MEASUREMENTS', cache_type='unit_of_measurement',
        cache=cache)
    # Get pre existing locations
    to_upload['locations'], cache = get_existing_items(
        auth_token=access_token, values=to_upload['locations'], unique_field='name',
        query_type='GET_LOCATIONS', cache_type='location', cache=cache)
    # Upload parts, units of measurment and locations
    resp = create_bulk_upload_request(
        access_token, CREATE_UNITS_OF_MEASUREMENT=to_upload['uoms'],
        CREATE_PART=to_upload['parts'], CREATE_LOCATION=to_upload['locations'])
    # Fill cache with ids from newely created objects
    cache = update_cache(resp, cache)
    to_upload = fill_from_cache(to_upload=to_upload, cache=cache,
                                to_fill=['parts_inventories'])
    # Upload part inventories and part lots
    resp = create_bulk_upload_request(
        access_token, CREATE_PART_INVENTORY=to_upload['parts_inventories'],)
    logging.info('Importing finished!')