Last active
May 24, 2018 19:18
-
-
Save cr0hn/7145ced2b4ac37f01dad to your computer and use it in GitHub Desktop.
Generate python documentation project, using sphinx, dynamically. The snippet must be pasted in the top of config.py file, the sphinx configuration file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__author__ = 'cr0hn - cr0hn<-at->cr0hn.com (@ggdaniel)' | |
import os | |
# -------------------------------------------------------------------------- | |
# Config | |
# -------------------------------------------------------------------------- | |
# Do not process this files/dirs | |
EXCLUDE_FILES_OR_DIRS = ["bin"] | |
# Readme file | |
README_FILE_PATH = os.path.abspath(os.path.join(os.getcwd(), "../../.", "README.rst")) | |
# Documentation base directory | |
DOC_DIR = os.path.join(os.getcwd(), "source") | |
# Source code base directory | |
PROJECT_DIR = os.path.abspath(os.path.join(os.path.join(os.getcwd()), "../.")) | |
# -------------------------------------------------------------------------- | |
# Find all .py files | |
# -------------------------------------------------------------------------- | |
# Files found | |
found_py_files = [] | |
for root, dirs, files in os.walk(PROJECT_DIR): | |
# Looking for .py files and packages, not explicitly excluded | |
if any(x.endswith(".py") for x in files) and \ | |
any("__init__.py" in x for x in files) and \ | |
any(x not in EXCLUDE_FILES_OR_DIRS for x in files): | |
# Process each file | |
for f in files: | |
print(f) | |
if "__init__" in f or not f.endswith(".py"): # Not process not .py or package maker files | |
continue | |
# Get and clean filename path | |
f = os.path.join(root, f) | |
file_name = f.replace(PROJECT_DIR, "").replace(".py", "").replace(os.path.sep, ".")[1:] | |
# Get path in doc path | |
rst_file = "%s.rst" % os.path.join(DOC_DIR, file_name) | |
# Write info | |
with open(rst_file, "w") as fw: | |
content = ( | |
"%s\n" | |
"%s\n\n" | |
".. automodule:: %s\n" | |
" :members:\n" | |
" :special-members:\n" | |
) % ( | |
file_name, | |
("-" * len(file_name)), | |
file_name | |
) | |
fw.write(content) | |
# Get file path to add in index.rst | |
found_py_files.append(file_name) | |
# -------------------------------------------------------------------------- | |
# Create index.rst | |
# -------------------------------------------------------------------------- | |
with open(os.path.join(DOC_DIR, "index.rst"), "w") as f: | |
# Readme | |
f.write("Readme\n^^^^^^\n\n") | |
with open(README_FILE_PATH, "rU") as readme: | |
for l in readme.readlines(): | |
f.write(l) | |
# API | |
f.write("\nAPI\n^^^^^^^^^^^^^\n\n") | |
f.write("Content: \n\n\n") | |
f.write(".. toctree::\n\n") | |
for index in found_py_files: | |
f.write(" %s <%s>\n" % (index.replace("_", " "), index)) | |
# Index | |
f.write("""\nIndices and tables | |
^^^^^^^^^^^^^^^^^^ | |
* :ref:`genindex` | |
* :ref:`modindex` | |
* :ref:`search` | |
""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment