1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# drf-yasg documentation build configuration file, created by
# sphinx-quickstart on Sun Dec 10 15:20:34 2017.
import inspect
import os
import re
import sys
import shutil
import sphinx_rtd_theme
from docutils import nodes, utils
from docutils.parsers.rst import roles
from docutils.parsers.rst.roles import set_classes
try:
from importlib import metadata
except ImportError: # Python < 3.8
from pkg_resources import get_distribution
# -- 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.autodoc', 'sphinx.ext.viewcode']
# 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'
# General information about the project.
project = 'drf-yasg'
copyright = '2018, Cristi V.'
author = 'Cristi V.'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
# The full version, including alpha/beta/rc tags.
#try:
# release = metadata.version('drf_yasg')
#except NameError: # Python < 3.8
# release = get_distribution('drf_yasg').version
#if 'noscm' in release:
# raise AssertionError('Invalid package version string: %s. \n'
# 'The documentation must be built with drf_yasg installed from a distribution package, '
# 'which must have been built with a proper version number (i.e. from a full source checkout).'
# % (release,))
# The short X.Y.Z version.
#version = '.'.join(release.split('.')[:3])
version = os.environ['DRF_YASG_UPSTREAM_VERSION']
# 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 = 'en'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
modindex_common_prefix = ['drf_yasg.']
# -- 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 = 'default'
html_theme = "sphinx_rtd_theme"
# 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']
# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# This is required for the alabaster theme
# refs: http://alabaster.readthedocs.io/en/latest/installation.html#sidebars
html_sidebars = {
'**': [
'relations.html', # needs 'show_related': True theme option to display
'searchbox.html',
]
}
# -- Options for HTMLHelp output ------------------------------------------
# Output file base name for HTML help builder.
htmlhelp_basename = 'drf-yasgdoc'
# -- 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, 'drf-yasg.tex', 'drf-yasg Documentation',
'Cristi V.', '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, 'drf-yasg', 'drf-yasg Documentation',
[author], 1)
]
# -- 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, 'drf-yasg', 'drf-yasg Documentation',
author, 'drf-yasg', 'One line description of project.',
'Miscellaneous'),
]
autodoc_default_options = {
'private-members': None
}
autodoc_member_order = 'bysource'
autoclass_content = 'both'
autodoc_mock_imports = []
nitpick_ignore = [
('py:class', 'object'),
('py:class', 'bool'),
('py:class', 'dict'),
('py:class', 'list'),
('py:class', 'str'),
('py:class', 'int'),
('py:class', 'bytes'),
('py:class', 'tuple'),
('py:class', 'function'),
('py:class', 'type'),
('py:class', 'OrderedDict'),
('py:class', 'None'),
('py:obj', 'None'),
('py:class', 'Exception'),
('py:class', 'collections.OrderedDict'),
('py:class', 'yaml.CSafeDumper'),
('py:class', 'rest_framework.serializers.Serializer'),
('py:class', 'rest_framework.renderers.BaseRenderer'),
('py:class', 'rest_framework.parsers.BaseParser'),
('py:class', 'rest_framework.schemas.generators.EndpointEnumerator'),
('py:class', 'rest_framework.views.APIView'),
('py:class', 'OpenAPICodecYaml'),
('py:class', 'OpenAPICodecJson'),
('py:class', 'OpenAPISchemaGenerator'),
('py:class', 'coreapi.Field'),
('py:class', 'BaseFilterBackend'),
('py:class', 'BasePagination'),
('py:class', 'Request'),
('py:class', 'rest_framework.request.Request'),
('py:class', 'rest_framework.serializers.Field'),
('py:class', 'serializers.Field'),
('py:class', 'serializers.BaseSerializer'),
('py:class', 'Serializer'),
('py:class', 'BaseSerializer'),
('py:class', 'APIView'),
]
# even though the package should be already installed, the sphinx build on RTD
# for some reason needs the sources dir to be in the path in order for viewcode to work
sys.path.insert(0, os.path.abspath('../src'))
# activate the Django testproj to be able to successfully import drf_yasg
sys.path.insert(0, os.path.abspath('../testproj'))
os.putenv('DJANGO_SETTINGS_MODULE', 'testproj.settings.local')
from django.conf import settings # noqa: E402
settings.configure()
# instantiate a SchemaView in the views module to make it available to autodoc
import drf_yasg.views # noqa: E402
drf_yasg.views.SchemaView = drf_yasg.views.get_schema_view(None)
# monkey patch to stop sphinx from trying to find classes by their real location instead of the
# top-level __init__ alias; this allows us to document only `drf_yasg.inspectors` and avoid broken references or
# double documenting
import drf_yasg.inspectors # noqa: E402
def redirect_cls(cls):
if cls.__module__.startswith('drf_yasg.inspectors'):
return getattr(drf_yasg.inspectors, cls.__name__)
return cls
for cls_name in drf_yasg.inspectors.__all__:
# first pass - replace all classes' module with the top level module
real_cls = getattr(drf_yasg.inspectors, cls_name)
if not inspect.isclass(real_cls):
continue
patched_dict = dict(real_cls.__dict__)
patched_dict.update({'__module__': 'drf_yasg.inspectors'})
patched_cls = type(cls_name, real_cls.__bases__, patched_dict)
setattr(drf_yasg.inspectors, cls_name, patched_cls)
for cls_name in drf_yasg.inspectors.__all__:
# second pass - replace the inheritance bases for all classes to point to the new clean classes
real_cls = getattr(drf_yasg.inspectors, cls_name)
if not inspect.isclass(real_cls):
continue
patched_bases = tuple(redirect_cls(base) for base in real_cls.__bases__)
patched_cls = type(cls_name, patched_bases, dict(real_cls.__dict__))
setattr(drf_yasg.inspectors, cls_name, patched_cls)
# custom interpreted role for linking to GitHub issues and pull requests
# use as :issue:`14` or :pr:`17`
gh_issue_uri = "https://github.com/axnsan12/drf-yasg/issues/{}"
gh_pr_uri = "https://github.com/axnsan12/drf-yasg/pull/{}"
gh_user_uri = "https://github.com/{}"
suppress_warnings = [
'image.not_readable'
]
def sphinx_err(inliner, lineno, rawtext, msg):
msg = inliner.reporter.error(msg, line=lineno)
prb = inliner.problematic(rawtext, rawtext, msg)
return [prb], [msg]
def sphinx_ref(options, rawtext, text, ref):
set_classes(options)
node = nodes.reference(rawtext, text, refuri=ref, **options)
return [node], []
def role_github_user(name, rawtext, text, lineno, inliner, options=None, content=None):
options = options or {}
content = content or []
if not re.match(r"^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$", text, re.IGNORECASE):
return sphinx_err(inliner, lineno, rawtext, '"%s" is not a valid GitHub username.' % text)
ref = gh_user_uri.format(text)
text = '@' + utils.unescape(text)
return sphinx_ref(options, rawtext, text, ref)
def role_github_pull_request_or_issue(name, rawtext, text, lineno, inliner, options=None, content=None):
options = options or {}
content = content or []
try:
if int(text) <= 0:
raise ValueError
except ValueError:
return sphinx_err(
inliner, lineno, rawtext,
'GitHub pull request or issue number must be a number greater than or equal to 1; "%s" is invalid.' % text
)
if name == 'pr':
ref = gh_pr_uri
elif name == 'issue':
ref = gh_issue_uri
else:
return sphinx_err(inliner, lineno, rawtext, 'unknown role name for GitHub reference - "%s"' % name)
ref = ref.format(text)
text = '#' + utils.unescape(text)
return sphinx_ref(options, rawtext, text, ref)
def copy_static(app, build):
shutil.copytree("docs/images", "docs/_build/html/docs/images", dirs_exist_ok=True)
roles.register_local_role('pr', role_github_pull_request_or_issue)
roles.register_local_role('issue', role_github_pull_request_or_issue)
roles.register_local_role('ghuser', role_github_user)
def setup(app):
app.add_css_file('css/style.css')
app.connect("build-finished", copy_static)
|