def generate_dir_rst(directory, fhindex, example_dir, root_dir, plot_gallery, seen_backrefs):
    """ Generate the rst file for an example directory.
    """
    if not directory == '.':
        target_dir = os.path.join(root_dir, directory)
        src_dir = os.path.join(example_dir, directory)
    else:
        target_dir = root_dir
        src_dir = example_dir
    if not os.path.exists(os.path.join(src_dir, 'README.txt')):
        raise ValueError('Example directory %s does not have a README.txt' %
                         src_dir)

    fhindex.write("""


%s


""" % open(os.path.join(src_dir, 'README.txt')).read())
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)
    sorted_listdir = line_count_sort(os.listdir(src_dir),
                                     src_dir)
    if not os.path.exists(os.path.join(directory, 'images', 'thumb')):
        os.makedirs(os.path.join(directory, 'images', 'thumb'))
    for fname in sorted_listdir:
        if fname.endswith('py'):
            backrefs = generate_file_rst(fname, target_dir, src_dir, root_dir, plot_gallery)
            new_fname = os.path.join(src_dir, fname)
            _, snippet, _ = extract_docstring(new_fname, True)
            fhindex.write(_thumbnail_div(directory, directory, fname, snippet))
            fhindex.write("""

.. toctree::
   :hidden:

   %s/%s

""" % (directory, fname[:-3]))
            for backref in backrefs:
                include_path = os.path.join(root_dir, '../modules/generated/%s.examples' % backref)
                seen = backref in seen_backrefs
                with open(include_path, 'a' if seen else 'w') as ex_file:
                    if not seen:
                        # heading
                        print(file=ex_file)
                        print('Examples using ``%s``' % backref, file=ex_file)
                        print('-----------------%s--' % ('-' * len(backref)),
                              file=ex_file)
                        print(file=ex_file)
                    rel_dir = os.path.join('../../auto_examples', directory)
                    ex_file.write(_thumbnail_div(directory, rel_dir, fname, snippet, is_backref=True))
                    seen_backrefs.add(backref)
    fhindex.write("""
.. raw:: html

    <div class="clearer"></div>
    """)  # clear at the end of the section

# modules for which we embed links into example code
dataset/ETHPy150Open scikit-learn/scikit-learn/doc/sphinxext/gen_rst.py/generate_dir_rst/original