def rst_for_package(root, dirs, files):
    """Given ../mylib/path/to/package and lists of dir/file names, write rst.
    """

    doc_path = root[3:]
    if not os.path.isdir(doc_path):
        os.mkdir(doc_path)


    # Start a rst doc for this package.
    # =================================

    f = rst_for_module(doc_path)


    # Add a table of contents.
    # ========================

    w(f, ".. toctree::")

    def toc(doc_path, name):
        parent = os.path.dirname(doc_path)
        toc_path = os.path.join(doc_path[len(parent):].lstrip('/'), name)
        if toc_path.endswith('.py'):
            toc_path = toc_path[:-len('.py')]
        w(f, "    {}", toc_path)
        return os.path.join(parent, toc_path)

    for name in sorted(dirs + files):
        if name in dirs:
            toc(doc_path, name)
        else:
            if not name.endswith('.py'): continue
            if name == '__init__.py': continue

            toc_path = toc(doc_path, name)


            # Write a rst file for each module.
            # =================================

            rst_for_module(toc_path)
dataset/ETHPy150Open gratipay/gratipay.com/docs/autolib.py/rst_for_package/original