Skip to content
Permalink
Browse files

python: add sphinx docs (#6525)

* python: generate documentation with Sphinx and Read the Docs

Background:

Formerly, the Python protobuf reference documentation was built with
[Epydoc](http://epydoc.sourceforge.net/). This package has not been
updated since 2008, and it has inconsistent formatting (see internal
issue 131415575) with most Python documentation. Sphinx is used for the
official docs.python.org docs as well as most other Python packages,
including the Google client libraries and related packages, such as
https://googleapis.dev/python/google-api-core/latest/

To build the docs with Sphinx:

1. Install the needed packages (`sphinx`, `sphinxcontrib-napoleon` for
Google-style docstring support). I've created a conda environment file
to make this easier:

```
conda env create -f python/docs/environment.yml
```

2. (Optional) Generate reference docs files and regenerate index:

```
cd python
python generate_docs.py
cd ..
```

3. Run Sphinx.

```
cd python/docs
make html
```

About this change:

The script at `python/generate_docs.py` creates a ReStructured Text file
for each public module in the protobuf Python package. The script also
updates the table of contents in `python/docs/index.rst` to point to
these module references.

Future work:

Testing the docs build on PRs requires contributors to actually do some
setup work to configure builds on their fork. It'd be better if CI had a
docs build session to verify that the Sphinx docs generation at least
runs.

There are many warnings due to not-quite-correct docstrings in the
actual Python code itself. I'm choosing to ignore these errors to keep
the PR small, but I recommend you fix these and then enable "fail on
warnings" in the docs build on CI.

* add docs to EXTRA_DIST

* add instructions to build documentation to generate_docs.py

* exclude python/odcs from cpp_distcheck
  • Loading branch information
tswast committed Feb 11, 2020
1 parent 1a74ba4 commit 29c83baeccf4b5dc27308999e5cca65eb3774e86
Showing with 728 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +19 −0 python/docs/Makefile
  3. +239 −0 python/docs/conf.py
  4. +12 −0 python/docs/environment.yml
  5. +161 −0 python/docs/generate_docs.py
  6. +9 −0 python/docs/google/protobuf.rst
  7. +9 −0 python/docs/google/protobuf/any_pb2.rst
  8. +9 −0 python/docs/google/protobuf/descriptor.rst
  9. +9 −0 python/docs/google/protobuf/descriptor_database.rst
  10. +9 −0 python/docs/google/protobuf/descriptor_pb2.rst
  11. +9 −0 python/docs/google/protobuf/descriptor_pool.rst
  12. +9 −0 python/docs/google/protobuf/duration_pb2.rst
  13. +9 −0 python/docs/google/protobuf/empty_pb2.rst
  14. +9 −0 python/docs/google/protobuf/field_mask_pb2.rst
  15. +9 −0 python/docs/google/protobuf/json_format.rst
  16. +9 −0 python/docs/google/protobuf/message.rst
  17. +9 −0 python/docs/google/protobuf/message_factory.rst
  18. +9 −0 python/docs/google/protobuf/proto_builder.rst
  19. +9 −0 python/docs/google/protobuf/reflection.rst
  20. +9 −0 python/docs/google/protobuf/service.rst
  21. +9 −0 python/docs/google/protobuf/service_reflection.rst
  22. +9 −0 python/docs/google/protobuf/struct_pb2.rst
  23. +9 −0 python/docs/google/protobuf/symbol_database.rst
  24. +9 −0 python/docs/google/protobuf/text_encoding.rst
  25. +9 −0 python/docs/google/protobuf/text_format.rst
  26. +9 −0 python/docs/google/protobuf/timestamp_pb2.rst
  27. +9 −0 python/docs/google/protobuf/type_pb2.rst
  28. +9 −0 python/docs/google/protobuf/wrappers_pb2.rst
  29. +50 −0 python/docs/index.rst
  30. +35 −0 python/docs/make.bat
  31. +3 −0 python/docs/requirements.txt
  32. +1 −1 tests.sh
@@ -57,6 +57,7 @@ python/**/*.egg
python/.eggs/
python/.tox
python/build/
python/docs/_build/

src/js_embed
src/protoc
@@ -0,0 +1,19 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@@ -0,0 +1,239 @@
# -*- coding: utf-8 -*-
# Protocol Buffers - Google's data interchange format
# Copyright 2019 Google LLC. All rights reserved.
# https://developers.google.com/protocol-buffers/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))
import google.protobuf

# -- Project information -----------------------------------------------------

project = u"Protocol Buffers"
copyright = u"2008, Google LLC"
author = u"Google LLC"

# The short X.Y version
version = u""
# The full version, including alpha/beta/rc tags
release = google.protobuf.__version__


# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
"sphinx.ext.autosummary",
"sphinx.ext.intersphinx",
"sphinxcontrib.napoleon",
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = ".rst"

# The master toctree document.
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = [u"_build", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = "alabaster"

# Remove JavaScript.
html_js_files = []

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ["_static"]

html_show_sourcelink = True

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself. Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.

# Remove searchbox.html to avoid embedded JavaScript.
html_sidebars = {
"**": [
"globaltoc.html", "localtoc.html", "relations.html", "sourcelink.html",
],
}


# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = "ProtocolBuffersdoc"


# -- Options for LaTeX output ------------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(
master_doc,
"ProtocolBuffers.tex",
"Protocol Buffers Documentation",
"Google LLC",
"manual",
)
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(
master_doc, # source start file
"protocolbuffers", # name
"Protocol Buffers Documentation", # description
[author], # authors
1, # manual section
)
]


# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(
master_doc,
"ProtocolBuffers",
u"Protocol Buffers Documentation",
author,
"ProtocolBuffers",
"One line description of project.",
"Miscellaneous",
)
]


# -- Options for Epub output -------------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''

# A unique identification for the text.
#
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ["search.html"]


# -- Extension configuration -------------------------------------------------

# -- Options for autosummary extension ---------------------------------------
autosummary_generate = True

# -- Options for intersphinx extension ---------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {"https://docs.python.org/": None}
@@ -0,0 +1,12 @@
name: protobuf
channels:
- conda-forge
- defaults
dependencies:
- libprotobuf=3.11.2
- make=4.2.1
- pip=19.3.1
- python=3.7.6
- sphinx=2.3.1
- sphinx_rtd_theme=0.4.3
- sphinxcontrib-napoleon=0.7

0 comments on commit 29c83ba

Please sign in to comment.
You can’t perform that action at this time.