code: def generate_dir_rst(
      directory,
      fhindex,
      example_dir,
      root_dir,
      plot_gallery,
      seen_backrefs,
      display=True,
  ):
      """Generate the rst file for an example directory.
      display=True means it wil be added to the examples gallery
      """
      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 display:
          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)
              if display:
                  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)
      if display:
          fhindex.write(
              """
  .. raw:: html
  
      <div class="clearer"></div>
      """
          )
  
imports: from __future__ import division, print_function
  from time import time
  import ast
  import os
  import re
  import shutil
  import traceback
  import glob
  import sys
  import gzip
  import posixpath
  import subprocess
  import warnings
  from sklearn.externals import six
  from docutils.core import publish_string
  import token
  import tokenize
  import numpy
  import joblib
label_desc: Use image node_id 8 for calling a global function with example usage: generate_dir_rst(directory, fhindex, example_dir, root_dir, plot_gallery, seen_backrefs, display) without return types