def crop(config):
    """
    This function takes an OpenVAS XML with results and return Vulnerability info.

    :param config: Config instance
    :type config: Config

    :raises: TypeError, InvalidFormat
    """
    import copy

    if not isinstance(config, Config):
        raise TypeError("Expected Config, got '%s' instead" % type(config))

    try:
        from xml.etree import cElementTree as ET
    except __HOLE__:
        from xml.etree import ElementTree as ET

    output_file = config.output_file
    file_path = config.input_files
    scope_hosts = config.scope
    excluded_hosts = config.excluded

    # Check format
    with open(file_path, "rU") as f:
        first_line = f.readline()

        if not first_line.startswith("<report ") or \
                not all(True for x in ("extension", "format_id", "content_type") if x in first_line):
            raise IOError("Invalid report format")

    # Read input file
    root = ET.parse(file_path).getroot()
    report = root.getchildren()[0]

    # --------------------------------------------------------------------------
    # Create clone
    # --------------------------------------------------------------------------
    root_clone = ET.Element(root.tag)
    root_clone.attrib=root.attrib

    report_clone = ET.SubElement(root_clone, "report")
    report_clone.attrib = report.attrib

    # Copy all elements
    for child in report.getchildren():

        # Add only these tags that not contain target information
        if child.tag not in ("host_start", "host_end", "host", "ports", "results"):
            tag = copy.deepcopy(child)

            # Add to clone
            report_clone.append(tag)

    # --------------------------------------------------------------------------
    # Add results tag
    # --------------------------------------------------------------------------
    results_tag = ET.Element("results")
    results_tag.attrib = root.find(".//results").attrib
    for vuln in root.findall(".//results/result"):

        # --------------------------------------------------------------------------
        # Target info
        # --------------------------------------------------------------------------
        vuln_host = vuln.find(".//host").text

        # Apply filter to include hosts or not
        if vuln_host in excluded_hosts:
            continue

        if scope_hosts is not None:
            if vuln_host not in scope_hosts:
                continue

        # Add to clone
        results_tag.append(vuln)

    # Add to clone
    report_clone.append(results_tag)

    # --------------------------------------------------------------------------
    # Add ports tag
    # --------------------------------------------------------------------------
    ports_tag = ET.Element("ports")
    ports_tag.attrib = root.find(".//ports").attrib
    for port in root.findall(".//ports/port"):

        # --------------------------------------------------------------------------
        # Target info
        # --------------------------------------------------------------------------
        vuln_host = port.find(".//host").text

        # Apply filter to include hosts or not
        if vuln_host in excluded_hosts:
            continue

        if scope_hosts is not None:
            if vuln_host not in scope_hosts:
                continue

        # Add to clone
        ports_tag.append(port)

    # Add to clone
    report_clone.append(ports_tag)

    # --------------------------------------------------------------------------
    # Add host tag
    # --------------------------------------------------------------------------
    for host in root.findall(".//report/host"):

        # --------------------------------------------------------------------------
        # Target info
        # --------------------------------------------------------------------------
        vuln_host = host.find(".//ip").text

        # Apply filter to include hosts or not
        if vuln_host in excluded_hosts:
            continue

        if scope_hosts is not None:
            if vuln_host not in scope_hosts:
                continue

        #host_tag = ET.Element("host", attrib=host.attrib, text=host.text)

        # Add to clone
        report_clone.append(host)

    # --------------------------------------------------------------------------
    # Add host_start tag
    # --------------------------------------------------------------------------
    for host_start in root.findall(".//report/host_start"):

        # --------------------------------------------------------------------------
        # Target info
        # --------------------------------------------------------------------------
        vuln_host = host_start.find(".//host").text

        # Apply filter to include hosts or not
        if vuln_host in excluded_hosts:
            continue

        if scope_hosts is not None:
            if vuln_host not in scope_hosts:
                continue

        # host_tag = ET.Element("host", attrib=host_start.attrib)

        # Add to clone
        report_clone.append(host_start)

    # --------------------------------------------------------------------------
    # Add host_end tag
    # --------------------------------------------------------------------------
    for host_end in root.findall(".//report/host_end"):

        # --------------------------------------------------------------------------
        # Target info
        # --------------------------------------------------------------------------
        vuln_host = host_end.find(".//host").text

        # Apply filter to include hosts or not
        if vuln_host in excluded_hosts:
            continue

        if scope_hosts is not None:
            if vuln_host not in scope_hosts:
                continue

        # host_tag = ET.Element("host", attrib=host_start.attrib)

        # Add to clone
        report_clone.append(host_end)

    # Save to file
    tree = ET.ElementTree(root_clone)
    # Un IO Memory and delete first line.
    tree.write(output_file, encoding="UTF-8", xml_declaration=False)


# ----------------------------------------------------------------------
dataset/ETHPy150Open cr0hn/openvas_to_report/openvas_to_report/api.py/crop