def parse_fields():
    messages_sheet = workbook.sheet_by_name('Messages')

    fields = {}
    last_field = None
    last_f_def_num = None

    for row in range(1, messages_sheet.nrows):
        row_values = [str(v).strip() if isinstance(v, (str, unicode)) else v \
                      for v in messages_sheet.row_values(row, end_colx=4)]

        # Skip blank rows
        if not any(row_values):
            continue

        # Check if it's a seperator row, ie only third column
        if not any(row_values[:3]) and row_values[3]:
            # TODO: here row_values[3] describes what file type these messages belong to
            continue

        possible_message_name, f_def_num, f_name, f_type = row_values

        if possible_message_name:
            # Define a message here
            message_name = possible_message_name
            fields[message_name] = {}
        else:
            # Sip for now unless all rows are here
            if not (message_name and f_name and f_type):
                pass
            else:

                is_dynamic_field = False

                try:
                    f_def_num = int(f_def_num)
                except ValueError:
                    # f_def_num not defined, we have a dynamic field on last_field
                    is_dynamic_field = True

                    if not isinstance(last_field, DynamicField):
                        last_field = DynamicField(*(tuple(last_field) + ({},)))
                        fields[message_name][last_f_def_num] = last_field

                    ref_field_names = [str(n).strip() for n in messages_sheet.row_values(row)[11].split(',')]
                    ref_field_values = [str(n).strip() for n in messages_sheet.row_values(row)[12].split(',')]

                    if len(ref_field_names) != len(ref_field_values):
                        raise Exception("Number of ref fields != number of ref values for %s" % f_name)

                try:
                    f_scale = int(messages_sheet.row_values(row)[6])
                    if f_scale == 1:
                        raise ValueError
                except ValueError:
                    f_scale = None

                try:
                    f_offset = int(messages_sheet.row_values(row)[7])
                except ValueError:
                    f_offset = None

                f_units = str(messages_sheet.row_values(row)[8]).strip()
                if not f_units:
                    f_units = None

                field = Field(f_name, f_type, f_scale, f_units, f_offset)

                if is_dynamic_field:
                    for i in range(len(ref_field_names)):
                        last_field.possibilities.setdefault(ref_field_names[i], {})[ref_field_values[i]] = field

                else:
                    fields[message_name][f_def_num] = field
                    last_field = field
                    last_f_def_num = f_def_num

    # Special considerations on fields dict

    # Copy possiblities for event.data into event.data16
    event = fields['event']
    for k, v in event.iteritems():
        if v.name == 'data':
            data_num = k
        elif v.name == 'data16':
            data16_num = k
    try:
        event[data16_num] = DynamicField(*tuple(event[data16_num] + (event[data_num].possibilities.copy(),)))
    except NameError:
        raise Exception("Couldn't find fields data/data16 in message type event")

    return fields
dataset/ETHPy150Open dtcooper/python-fitparse/scripts/generate_profile.py/parse_fields/original