Exemplo n.º 1
0
def process_library(vehicle, library, pathprefix=None):
    '''process one library'''
    paths = library.Path.split(',')
    for path in paths:
        path = path.strip()
        global current_file
        current_file = path
        debug("\n Processing file '%s'" % path)
        if pathprefix is not None:
            libraryfname = os.path.join(pathprefix, path)
        elif path.find('/') == -1:
            libraryfname = os.path.join(vehicle.path, path)
        else:
            libraryfname = os.path.normpath(
                os.path.join(apm_path + '/libraries/' + path))
        if path and os.path.exists(libraryfname):
            f = open(libraryfname)
            p_text = f.read()
            f.close()
        else:
            error("Path %s not found for library %s (fname=%s)" %
                  (path, library.name, libraryfname))
            continue

        param_matches = prog_param.findall(p_text)
        debug("Found %u documented parameters" % len(param_matches))
        for param_match in param_matches:
            (only_vehicles, param_name,
             field_text) = (param_match[0], param_match[1], param_match[2])
            if len(only_vehicles):
                only_vehicles_list = [
                    x.strip() for x in only_vehicles.split(",")
                ]
                for only_vehicle in only_vehicles_list:
                    if only_vehicle not in valid_truenames:
                        raise ValueError("Invalid only_vehicle %s" %
                                         only_vehicle)
                if vehicle.name not in only_vehicles_list:
                    continue
            p = Parameter(library.name + param_name, current_file)
            debug(p.name + ' ')
            global current_param
            current_param = p.name
            fields = prog_param_fields.findall(field_text)
            non_vehicle_specific_values_seen = False
            for field in fields:
                if field[0] in known_param_fields:
                    value = re.sub('@PREFIX@', library.name, field[1])
                    setattr(p, field[0], value)
                    if field[0] == "Values":
                        non_vehicle_specific_values_seen = True
                else:
                    error("param: unknown parameter metadata field %s" %
                          field[0])
            debug("matching %s" % field_text)
            fields = prog_param_tagged_fields.findall(field_text)
            this_vehicle_values_seen = False
            this_vehicle_value = None
            other_vehicle_values_seen = False
            for field in fields:
                only_for_vehicles = field[1].split(",")
                only_for_vehicles = [
                    x.rstrip().lstrip() for x in only_for_vehicles
                ]
                delta = set(only_for_vehicles) - set(truename_map.values())
                if len(delta):
                    error("Unknown vehicles (%s)" % delta)
                debug(
                    "field[0]=%s vehicle=%s field[1]=%s only_for_vehicles=%s\n"
                    %
                    (field[0], vehicle.name, field[1], str(only_for_vehicles)))
                value = re.sub('@PREFIX@', library.name, field[2])
                if field[0] in ['Values', 'Bitmask']:
                    if vehicle.name in only_for_vehicles:
                        this_vehicle_values_seen = True
                        this_vehicle_value = value
                        if len(only_for_vehicles) > 1:
                            other_vehicle_values_seen = True
                    elif len(only_for_vehicles):
                        other_vehicle_values_seen = True
                if field[0] in known_param_fields:
                    setattr(p, field[0], value)
                else:
                    error(
                        "tagged param<: unknown parameter metadata field '%s'"
                        % field[0])
                if ((non_vehicle_specific_values_seen
                     or not other_vehicle_values_seen)
                        or this_vehicle_values_seen):
                    if this_vehicle_values_seen and field[0] in [
                            'Values', 'Bitmask'
                    ]:
                        setattr(p, field[0], this_vehicle_value)
    #                debug("Appending (non_vehicle_specific_values_seen=%u "
    #                      "other_vehicle_values_seen=%u this_vehicle_values_seen=%u)" %
    #                      (non_vehicle_specific_values_seen, other_vehicle_values_seen, this_vehicle_values_seen))
            p.path = path  # Add path. Later deleted - only used for duplicates
            library.params.append(p)

        group_matches = prog_groups.findall(p_text)
        debug("Found %u groups" % len(group_matches))
        debug(group_matches)
        done_groups = dict()
        for group_match in group_matches:
            group = group_match[0]
            debug("Group: %s" % group)
            do_append = True
            if group in done_groups:
                # this is to handle cases like the RangeFinder
                # parameters, where the wasp stuff gets tack into the
                # same RNGFND1_ group
                lib = done_groups[group]
                do_append = False
            else:
                lib = Library(group)
                done_groups[group] = lib

            fields = prog_param_fields.findall(group_match[1])
            for field in fields:
                if field[0] in known_group_fields:
                    setattr(lib, field[0], field[1])
                else:
                    error("unknown parameter metadata field '%s'" % field[0])
            if not any(lib.name == parsed_l.name for parsed_l in libraries):
                if do_append:
                    lib.set_name(library.name + lib.name)
                debug("Group name: %s" % lib.name)
                process_library(vehicle, lib, os.path.dirname(libraryfname))
                if do_append:
                    alllibs.append(lib)

    current_file = None
Exemplo n.º 2
0
def process_library(vehicle, library, pathprefix=None):
    '''process one library'''
    paths = library.Path.split(',')
    for path in paths:
        path = path.strip()
        global current_file
        current_file = path
        debug("\n Processing file '%s'" % path)
        if pathprefix is not None:
            libraryfname = os.path.join(pathprefix, path)
        elif path.find('/') == -1:
            libraryfname = os.path.join(vehicle.path, path)
        else:
            libraryfname = os.path.normpath(
                os.path.join(apm_path + '/libraries/' + path))
        if path and os.path.exists(libraryfname):
            f = open(libraryfname)
            p_text = f.read()
            f.close()
        else:
            error("Path %s not found for library %s (fname=%s)" %
                  (path, library.name, libraryfname))
            continue

        param_matches = prog_param.findall(p_text)
        debug("Found %u documented parameters" % len(param_matches))
        for param_match in param_matches:
            (only_vehicles, param_name,
             field_text) = (param_match[0], param_match[1], param_match[2])
            if len(only_vehicles):
                only_vehicles_list = [
                    x.strip() for x in only_vehicles.split(",")
                ]
                for only_vehicle in only_vehicles_list:
                    if only_vehicle not in valid_truenames:
                        raise ValueError("Invalid only_vehicle %s" %
                                         only_vehicle)
                if vehicle.name not in only_vehicles_list:
                    continue
            p = Parameter(library.name + param_name, current_file)
            debug(p.name + ' ')
            global current_param
            current_param = p.name
            fields = prog_param_fields.findall(field_text)
            for field in fields:
                if field[0] in known_param_fields:
                    value = re.sub('@PREFIX@', library.name, field[1])
                    setattr(p, field[0], value)
                elif field[0] == "CopyValuesFrom":
                    setattr(p, field[0], field[1])
                else:
                    error("param: unknown parameter metadata field %s" %
                          field[0])
            debug("matching %s" % field_text)
            fields = prog_param_tagged_fields.findall(field_text)
            # a parameter is considered to be vehicle-specific if
            # there does not exist a Values: or Values{VehicleName}
            # for that vehicle but @Values{OtherVehicle} exists.
            seen_values_or_bitmask_for_other_vehicle = False
            for field in fields:
                only_for_vehicles = field[1].split(",")
                only_for_vehicles = [
                    x.rstrip().lstrip() for x in only_for_vehicles
                ]
                delta = set(only_for_vehicles) - set(truename_map.values())
                if len(delta):
                    error("Unknown vehicles (%s)" % delta)
                debug(
                    "field[0]=%s vehicle=%s field[1]=%s only_for_vehicles=%s\n"
                    %
                    (field[0], vehicle.name, field[1], str(only_for_vehicles)))
                if field[0] not in known_param_fields:
                    error(
                        "tagged param: unknown parameter metadata field '%s'" %
                        field[0])
                    continue
                if vehicle.name not in only_for_vehicles:
                    if len(only_for_vehicles) and field[0] in [
                            'Values', 'Bitmask'
                    ]:
                        seen_values_or_bitmask_for_other_vehicle = True
                    continue
                value = re.sub('@PREFIX@', library.name, field[2])
                setattr(p, field[0], value)

            if (getattr(p, 'Values', None) is not None
                    and getattr(p, 'Bitmask', None) is not None):
                error("Both @Values and @Bitmask present")

            if (getattr(p, 'Values', None) is None
                    and getattr(p, 'Bitmask', None) is None):
                # values and Bitmask available for this vehicle
                if seen_values_or_bitmask_for_other_vehicle:
                    # we've (e.g.) seen @Values{Copter} when we're
                    # processing for Rover, and haven't seen either
                    # @Values: or @Vales{Rover} - so we omit this
                    # parameter on the assumption that it is not
                    # applicable for this vehicle.
                    continue

            p.path = path  # Add path. Later deleted - only used for duplicates
            library.params.append(p)

        group_matches = prog_groups.findall(p_text)
        debug("Found %u groups" % len(group_matches))
        debug(group_matches)
        done_groups = dict()
        for group_match in group_matches:
            group = group_match[0]
            debug("Group: %s" % group)
            do_append = True
            if group in done_groups:
                # this is to handle cases like the RangeFinder
                # parameters, where the wasp stuff gets tack into the
                # same RNGFND1_ group
                lib = done_groups[group]
                do_append = False
            else:
                lib = Library(group)
                done_groups[group] = lib

            fields = prog_param_fields.findall(group_match[1])
            for field in fields:
                if field[0] in known_group_fields:
                    setattr(lib, field[0], field[1])
                else:
                    error("unknown parameter metadata field '%s'" % field[0])
            if not any(lib.name == parsed_l.name for parsed_l in libraries):
                if do_append:
                    lib.set_name(library.name + lib.name)
                debug("Group name: %s" % lib.name)
                process_library(vehicle, lib, os.path.dirname(libraryfname))
                if do_append:
                    alllibs.append(lib)

    current_file = None