def make_graph_from_hosts(hosts):
    #hosts = parser.get_root().search_children('host', deep=True)
    graph = Graph()
    nodes = list()
    index = 1

    # Setting initial reference host
    nodes.append(NetNode(0))
    node = nodes[-1]

    node.set_info({"ip": "127.0.0.1/8", "hostname": "localhost"})
    node.set_draw_info({"color": (0, 0, 0), "radius": NONE_RADIUS})

    # For each host in hosts just mount the graph
    for host in hosts:
        trace = host.trace

        hops = trace.get("hops")
        # If host has traceroute information mount graph
        if hops is not None and len(hops) > 0:
            prev_node = nodes[0]
            hops = trace.get("hops", [])
            ttls = [int(hop["ttl"]) for hop in hops]

            # Getting nodes of host by ttl
            for ttl in range(1, max(ttls) + 1):
                if ttl in ttls:
                    # Find a hop by ttl
                    hop = None
                    for h in hops:
                        if ttl == int(h["ttl"]):
                            hop = h
                            break

                    for node in nodes:
                        if hop["ipaddr"] == node.get_info("ip"):
                            break
                    else:
                        nodes.append(NetNode(index))
                        node = nodes[-1]
                        index += 1

                        node.set_draw_info({"valid": True})
                        node.set_info({"ip": hop["ipaddr"]})
                        node.set_draw_info({
                            "color": (1, 1, 1),
                            "radius": NONE_RADIUS
                        })

                        if hop["host"] != "":
                            node.set_info({"hostname": hop["host"]})

                    rtt = hop["rtt"]
                    if rtt != "--":
                        graph.set_connection(node, prev_node, float(rtt))
                    else:
                        graph.set_connection(node, prev_node)
                else:
                    nodes.append(NetNode(index))
                    node = nodes[-1]
                    index += 1

                    node.set_draw_info({"valid": False})
                    node.set_info({"ip": None, "hostname": None})
                    node.set_draw_info({
                        "color": (1, 1, 1),
                        "radius": NONE_RADIUS
                    })

                    graph.set_connection(node, prev_node)

                prev_node = node

    # For each fully scanned host
    for host in hosts:
        ip = host.ip
        if len(ip) == 0:
            ip = host.ipv6

        for node in nodes:
            if ip["addr"] == node.get_info("ip"):
                break
        else:
            nodes.append(NetNode(index))
            node = nodes[-1]
            index += 1

            node.set_draw_info({"no_route": True})

            graph.set_connection(node, nodes[0])

        node.set_draw_info({"valid": True})
        node.set_info({"scanned": True})
        set_node_info(node, host)

    graph.set_nodes(nodes)
    graph.set_main_node_by_id(0)

    return graph
def make_graph_from_nmap_parser(parser):
    """
    """
    hosts = parser.get_root().search_children('host', deep=True)
    graph = Graph()
    nodes = list()
    index = 1

    # setting initial reference host
    nodes.append(NetNode(0))
    node = nodes[-1]

    node.set_info({'ip': '127.0.0.1/8', 'hostname': 'localhost'})
    node.set_draw_info({'color': (0, 0, 0), 'radius': NONE_RADIUS})

    # for each host in hosts just mount the graph
    for host in hosts:

        trace = host.search_children('trace', True, True)

        # if host has traceroute information mount graph
        if trace != None:

            prev_node = nodes[0]

            hops = trace.search_children('hop')
            ttls = [int(hop.get_attr('ttl')) for hop in hops]

            # getting nodes of host by ttl
            for ttl in range(1, max(ttls) + 1):

                if ttl in ttls:

                    hop = trace.query_children('hop', 'ttl', ttl, True)

                    for node in nodes:
                        if hop.get_attr('ipaddr') == node.get_info('ip'):
                            break

                    else:

                        nodes.append(NetNode(index))
                        node = nodes[-1]
                        index += 1

                        node.set_draw_info({'valid': True})
                        node.set_info({'ip': hop.get_attr('ipaddr')})
                        node.set_draw_info({
                            'color': (1, 1, 1),
                            'radius': NONE_RADIUS
                        })

                        if hop.get_attr('host') != None:
                            node.set_info({'hostname': hop.get_attr('host')})

                    rtt = hop.get_attr('rtt')

                    if rtt != '--':
                        graph.set_connection(node, prev_node, float(rtt))

                    else:
                        graph.set_connection(node, prev_node)

                else:

                    nodes.append(NetNode(index))
                    node = nodes[-1]
                    index += 1

                    node.set_draw_info({'valid': False})
                    node.set_info({'ip': None, 'hostname': None})
                    node.set_draw_info({
                        'color': (1, 1, 1),
                        'radius': NONE_RADIUS
                    })

                    graph.set_connection(node, prev_node)

                prev_node = node

    # for each full scanned host
    for host in hosts:

        ip = host.query_children('address', 'addrtype', 'ipv4', True)

        if ip == None:
            ip = host.query_children('address', 'addrtype', 'ipv6', True)

        for node in nodes:
            if ip.get_attr('addr') == node.get_info('ip'):
                break

        else:

            nodes.append(NetNode(index))
            node = nodes[-1]
            index += 1

            node.set_draw_info({'no_route': True})

            graph.set_connection(node, nodes[0])

        node.set_draw_info({'valid': True})
        node.set_info({'scanned': True})
        set_node_info(node, host)

    graph.set_nodes(nodes)
    graph.set_main_node_by_id(0)

    return graph