import pickle


def write_graphs_from_dir(graphs_path, graphs_indices, outfile):
    """
    Write a list of graphs from a directory to file in graph transaction format (used by fsg)
    """

    graphs_indices_act = []
    with open(outfile, 'w') as fr:
        for ind in graphs_indices:
            with open(graphs_path + 'graph' + str(ind) + '.dat', 'rb') as f:
                g = pickle.load(f)

                if g.number_of_nodes() < 10:
                    continue

                fr.write(f't\n')
                for v in g.nodes:
                    v_label = g.nodes[v]['label'].split('-')[0]
                    fr.write(f'v {v} {v_label}\n')
                for u, v in g.edges:
                    edge_label = g.edges[u, v]['label']
                    fr.write(f'u {u} {v} {edge_label} \n')

                graphs_indices_act.append(ind)

    return graphs_indices_act