| 1 | # |
| 2 | # pyglet documentation build configuration file. |
| 3 | # |
| 4 | # This file is execfile()d with the current directory set to its containing dir. |
| 5 | import os |
| 6 | import sys |
| 7 | import time |
| 8 | import typing |
| 9 | import datetime |
| 10 | |
| 11 | from typing import TypeVar |
| 12 | |
| 13 | import sphinx_autodoc_typehints |
| 14 | |
| 15 | # -- Extensions to the Napoleon GoogleDocstring class --------------------- |
| 16 | from sphinx.ext.napoleon.docstring import GoogleDocstring |
| 17 | from sphinx.parsers import RSTParser |
| 18 | |
| 19 | |
| 20 | def parse_attributes_section(self, section): |
| 21 | # Combination of _format_fields and _parse_attributes_section to get type hints loaded properly using |
| 22 | # the theme and getting the custom name instead of Specifying just 'Variables' |
| 23 | field_type = "Class Variables" |
| 24 | fields = self._consume_fields() |
| 25 | |
| 26 | field_type = ":%s:" % field_type.strip() |
| 27 | padding = " " * len(field_type) |
| 28 | multi = len(fields) > 1 |
| 29 | lines: list[str] = [] |
| 30 | for _name, _type, _desc in fields: |
| 31 | if not _type: |
| 32 | _type = self._lookup_annotation(_name) |
| 33 | if _name in self._annotations: |
| 34 | _type = sphinx_autodoc_typehints.add_type_css_class( |
| 35 | sphinx_autodoc_typehints.format_annotation(self._annotations[_name], self._app.config)) |
| 36 | |
| 37 | field = self._format_field(_name, _type, _desc) |
| 38 | if multi: |
| 39 | if lines: |
| 40 | lines.extend(self._format_block(padding + " * ", field)) |
| 41 | else: |
| 42 | lines.extend(self._format_block(field_type + " * ", field)) |
| 43 | else: |
| 44 | lines.extend(self._format_block(field_type + " ", field)) |
| 45 | if lines and lines[-1]: |
| 46 | lines.append("") |
| 47 | return lines |
| 48 | |
| 49 | |
| 50 | GoogleDocstring._parse_attributes_section = parse_attributes_section |
| 51 | |
| 52 | |
| 53 | # Unpatch... the patch. |
| 54 | def _revert_patch(): |
| 55 | pass |
| 56 | |
| 57 | |
| 58 | sphinx_autodoc_typehints.patches._patch_google_docstring_lookup_annotation = _revert_patch |
| 59 | |
| 60 | |
| 61 | # Patch to fix our RST insertion issues. |
| 62 | class _RstSnippetParser(RSTParser): |
| 63 | @staticmethod |
| 64 | def decorate(_content) -> None: |
| 65 | """Override to skip processing rst_epilog/rst_prolog for typing.""" |
| 66 | |
| 67 | |
| 68 | sphinx_autodoc_typehints.parser.RSTParser = _RstSnippetParser |
| 69 | |
| 70 | |
| 71 | def write_build(data, filename): |
| 72 | with open(os.path.join("internal", filename), "w") as f: |
| 73 | f.write(".. list-table::\n") |
| 74 | f.write(" :widths: 50 50\n") |
| 75 | f.write("\n") |
| 76 | for var, val in data: |
| 77 | f.write(" * - " + var + "\n - " + val + "\n") |
| 78 | |
| 79 | |
| 80 | sys.is_pyglet_doc_run = True |
| 81 | |
| 82 | document_modules = ["pyglet"] |
| 83 | |
| 84 | # Patched extensions base path. |
| 85 | sys.path.insert(0, os.path.abspath(".")) |
| 86 | |
| 87 | # import the pyglet package. |
| 88 | sys.path.insert(0, os.path.abspath("..")) |
| 89 | |
| 90 | try: |
| 91 | import pyglet |
| 92 | |
| 93 | print(f"Generating pyglet {pyglet.version} Documentation") |
| 94 | except ImportError: |
| 95 | print("ERROR: pyglet not found") |
| 96 | sys.exit(1) |
| 97 | |
| 98 | # -- PYGLET DOCUMENTATION CONFIGURATION ---------------------------------------- |
| 99 | |
| 100 | |
| 101 | # Set up substitutions that can be referenced later. |
| 102 | # IMPORTANT: |
| 103 | # Substitutions are moody and NEED control-like characters to be escaped |
| 104 | # after them. For example, in |min_python_version|\+ , escaping the plus |
| 105 | # sign is crucial to avoiding incomprehensible errors. |
| 106 | # For convenience, |min_python_version_fancy_str| is defined below to |
| 107 | # avoid having to deal with syntax errors. |
| 108 | # Also, please note that there does not appear to be a good way to use |
| 109 | # substitutions within link text by default. |
| 110 | rst_epilog = rf""" |
| 111 | .. |min_python_version| replace:: {pyglet.MIN_PYTHON_VERSION_STR} |
| 112 | .. |min_python_version_package_name| replace:: ``python{pyglet.MIN_PYTHON_VERSION_STR}`` |
| 113 | .. |min_python_version_fancy_str| replace:: Python {pyglet.MIN_PYTHON_VERSION_STR}\+ |
| 114 | """ |
| 115 | |
| 116 | implementations = ["cocoa", "win32", "xlib"] |
| 117 | |
| 118 | # For each module, a list of submodules that should not be imported. |
| 119 | # If value is None, do not try to import any submodule. |
| 120 | skip_modules = {"pyglet": { |
| 121 | "pyglet.lib": None, |
| 122 | "pyglet.libs": None, |
| 123 | "pyglet.app": implementations, |
| 124 | "pyglet.display": implementations + ["xlib_vidmoderestore"], |
| 125 | "pyglet.extlibs": None, |
| 126 | "pyglet.font": ["quartz", |
| 127 | "win32", |
| 128 | "freetype", |
| 129 | "freetype_lib", |
| 130 | "fontconfig", |
| 131 | "win32query"], |
| 132 | "pyglet.input": ["darwin_hid", |
| 133 | "directinput", |
| 134 | "evdev", |
| 135 | "wintab", |
| 136 | "x11_xinput", "x11_xinput_tablet"], |
| 137 | "pyglet.image.codecs": ["gdiplus", |
| 138 | "gdkpixbuf2", |
| 139 | "pil", |
| 140 | "quartz", |
| 141 | "quicktime"], |
| 142 | "pyglet.gl": implementations + ["agl", |
| 143 | "glext_arb", "glext_nv", |
| 144 | "glx", "glx_info", |
| 145 | "glxext_arb", "glxext_mesa", "glxext_nv", |
| 146 | "lib_agl", "lib_glx", "lib_wgl", |
| 147 | "wgl", "wgl_info", "wglext_arb", "wglext_nv"], |
| 148 | "pyglet.media.drivers": ["directsound", |
| 149 | "openal", |
| 150 | "pulse"], |
| 151 | "pyglet.window": implementations, |
| 152 | }, |
| 153 | } |
| 154 | |
| 155 | now = datetime.datetime.fromtimestamp(time.time()) |
| 156 | build_data = (("Date", now.strftime("%Y/%m/%d %H:%M:%S")), |
| 157 | ("pyglet version", pyglet.version)) |
| 158 | write_build(build_data, "build.rst") |
| 159 | |
| 160 | # -- SPHINX STANDARD OPTIONS --------------------------------------------------- |
| 161 | |
| 162 | autosummary_generate = False |
| 163 | |
| 164 | # -- General configuration ----------------------------------------------------- |
| 165 | # |
| 166 | # Note that not all possible configuration values are present in this file. |
| 167 | # |
| 168 | # All configuration values have a default; values that are commented out |
| 169 | # serve to show the default. |
| 170 | |
| 171 | inheritance_graph_attrs = dict(rankdir="LR", size='""') |
| 172 | |
| 173 | # Add any Sphinx extension module names here, as strings. They can be extensions |
| 174 | # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. |
| 175 | extensions = ["sphinx.ext.autodoc", |
| 176 | "sphinx.ext.napoleon", |
| 177 | "sphinx.ext.intersphinx", |
| 178 | "sphinx.ext.inheritance_diagram", |
| 179 | "sphinx.ext.todo", |
| 180 | "sphinx_autodoc_typehints", |
| 181 | ] |
| 182 | |
| 183 | # Autodoc settings. |
| 184 | autodoc_member_order = "groupwise" |
| 185 | |
| 186 | # Separate init from the class header as RTD theme makes it all the same color, reducing readability. |
| 187 | autodoc_class_signature = "separated" |
| 188 | |
| 189 | # Add type hints to description and parameters in docs. |
| 190 | autodoc_typehints = "signature" |
| 191 | autodoc_typehints_format = "short" |
| 192 | |
| 193 | # Configuration for sphinx_autodoc_typehints |
| 194 | typehints_use_signature = True |
| 195 | typehints_use_signature_return = True |
| 196 | always_use_bars_union = True |
| 197 | |
| 198 | # Enable links to Python's main doc |
| 199 | intersphinx_mapping = { |
| 200 | "python": ("https://docs.python.org/3", None), |
| 201 | } |
| 202 | |
| 203 | # Add any paths that contain templates here, relative to this directory. |
| 204 | templates_path = ["_templates"] |
| 205 | |
| 206 | # The suffix of source filenames. |
| 207 | source_suffix = ".rst" |
| 208 | |
| 209 | # The master toctree document. |
| 210 | master_doc = "index" |
| 211 | |
| 212 | # General information about the project. |
| 213 | project = "pyglet" |
| 214 | copyright = f"2006-2008, Alex Holkner. 2008-{now.year} pyglet contributors" |
| 215 | |
| 216 | # The version info for the project you're documenting, acts as replacement for |
| 217 | # |version| and |release|, also used in various other places throughout the |
| 218 | # built documents. |
| 219 | # |
| 220 | # The short X.Y version. |
| 221 | if pyglet.version.count(".") == 1: |
| 222 | version = pyglet.version |
| 223 | else: |
| 224 | version = pyglet.version.rsplit('.', 1)[0] |
| 225 | # The full version, including alpha/beta/rc tags. |
| 226 | release = pyglet.version |
| 227 | |
| 228 | # The language for content autogenerated by Sphinx. Refer to documentation |
| 229 | # for a list of supported languages. |
| 230 | # language = 'en' |
| 231 | |
| 232 | # There are two options for replacing |today|: either, you set today to some |
| 233 | # non-false value, then it is used: |
| 234 | # today = '' |
| 235 | # Else, today_fmt is used as the format for a strftime call. |
| 236 | # today_fmt = '%B %d, %Y' |
| 237 | |
| 238 | # List of patterns, relative to source directory, that match files and |
| 239 | # directories to ignore when looking for source files. |
| 240 | exclude_patterns = ["_build", "_templates", "api"] |
| 241 | |
| 242 | # The reST default role (used for this markup: `text`) to use for all documents. |
| 243 | # default_role = None |
| 244 | |
| 245 | # If true, '()' will be appended to :func: etc. cross-reference text. |
| 246 | # add_function_parentheses = True |
| 247 | |
| 248 | # If true, the current module name will be prepended to all description |
| 249 | # unit titles (such as .. function::). |
| 250 | add_module_names = False |
| 251 | |
| 252 | # If true, sectionauthor and moduleauthor directives will be shown in the |
| 253 | # output. They are ignored by default. |
| 254 | # show_authors = False |
| 255 | |
| 256 | # The name of the Pygments (syntax highlighting) style to use. |
| 257 | pygments_style = "sphinx" |
| 258 | |
| 259 | # A list of ignored prefixes for module index sorting. |
| 260 | modindex_common_prefix = ["pyglet."] |
| 261 | |
| 262 | # -- Options for HTML output --------------------------------------------------- |
| 263 | |
| 264 | # The theme to use for HTML and HTML Help pages. See the documentation for |
| 265 | # a list of builtin themes. |
| 266 | html_theme = "sphinx_rtd_theme" |
| 267 | |
| 268 | # Theme options are theme-specific and customize the look and feel of a theme |
| 269 | # further. For a list of options available for each theme, see the |
| 270 | # documentation. |
| 271 | # html_theme_options = {} |
| 272 | |
| 273 | # Force custom css to allow multiline inits. |
| 274 | html_css_files = [ |
| 275 | "css/custom.css", |
| 276 | ] |
| 277 | html_style = "css/custom.css" |
| 278 | |
| 279 | # Add any paths that contain custom themes here, relative to this directory. |
| 280 | # html_theme_path = ["ext/theme"] |
| 281 | |
| 282 | # The name for this set of Sphinx documents. If None, it defaults to |
| 283 | # "<project> v<release> documentation". |
| 284 | html_title = f"pyglet v{pyglet.version}" |
| 285 | |
| 286 | # A shorter title for the navigation bar. Default is the same as html_title. |
| 287 | html_short_title = f"pyglet v{pyglet.version} documentation " |
| 288 | |
| 289 | # The name of an image file (relative to this directory) to place at the top |
| 290 | # of the sidebar. |
| 291 | html_logo = "_static/logo.png" |
| 292 | |
| 293 | # The name of an image file (within the static path) to use as favicon of the |
| 294 | # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 |
| 295 | # pixels large. |
| 296 | html_favicon = "_static/favicon.ico" |
| 297 | |
| 298 | # Add any paths that contain custom static files (such as style sheets) here, |
| 299 | # relative to this directory. They are copied after the builtin static files, |
| 300 | # so a file named "default.css" will overwrite the builtin "default.css". |
| 301 | html_static_path = ["_static"] |
| 302 | |
| 303 | # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, |
| 304 | # using the given strftime format. |
| 305 | # html_last_updated_fmt = '%b %d, %Y' |
| 306 | |
| 307 | # If true, SmartyPants will be used to convert quotes and dashes to |
| 308 | # typographically correct entities. |
| 309 | # html_use_smartypants = True |
| 310 | |
| 311 | # Custom sidebar templates, maps document names to template names. |
| 312 | # html_sidebars = {} |
| 313 | |
| 314 | # Additional templates that should be rendered to pages, maps page names to |
| 315 | # template names. |
| 316 | # html_additional_pages = {} |
| 317 | |
| 318 | # If false, no module index is generated. |
| 319 | html_domain_indices = True |
| 320 | |
| 321 | # If false, no index is generated. |
| 322 | html_use_index = True |
| 323 | |
| 324 | # If true, the index is split into individual pages for each letter. |
| 325 | html_split_index = True |
| 326 | |
| 327 | # If true, links to the reST sources are added to the pages. |
| 328 | html_show_sourcelink = False |
| 329 | |
| 330 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. |
| 331 | # html_show_sphinx = True |
| 332 | |
| 333 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. |
| 334 | # html_show_copyright = True |
| 335 | |
| 336 | # If true, an OpenSearch description file will be output, and all pages will |
| 337 | # contain a <link> tag referring to it. The value of this option must be the |
| 338 | # base URL from which the finished HTML is served. |
| 339 | # html_use_opensearch = '' |
| 340 | |
| 341 | # This is the file name suffix for HTML files (e.g. ".xhtml"). |
| 342 | # html_file_suffix = None |
| 343 | |
| 344 | # Output file base name for HTML help builder. |
| 345 | htmlhelp_basename = "pygletdoc" |
| 346 | |
| 347 | # -- Options for LaTeX output -------------------------------------------------- |
| 348 | |
| 349 | latex_elements = { |
| 350 | # The paper size ('letterpaper' or 'a4paper'). |
| 351 | # 'papersize': 'letterpaper', |
| 352 | |
| 353 | # The font size ('10pt', '11pt' or '12pt'). |
| 354 | # 'pointsize': '10pt', |
| 355 | |
| 356 | # Additional stuff for the LaTeX preamble. |
| 357 | # 'preamble': '', |
| 358 | } |
| 359 | |
| 360 | # Grouping the document tree into LaTeX files. List of tuples |
| 361 | # (source start file, target name, title, author, documentclass [howto/manual]). |
| 362 | latex_documents = [ |
| 363 | ("index", "pyglet.tex", "pyglet Documentation", |
| 364 | "Alex Holkner", "manual"), |
| 365 | ] |
| 366 | |
| 367 | # The name of an image file (relative to this directory) to place at the top of |
| 368 | # the title page. |
| 369 | # latex_logo = None |
| 370 | |
| 371 | # For "manual" documents, if this is true, then toplevel headings are parts, |
| 372 | # not chapters. |
| 373 | # latex_use_parts = False |
| 374 | |
| 375 | # If true, show page references after internal links. |
| 376 | # latex_show_pagerefs = False |
| 377 | |
| 378 | # If true, show URL addresses after external links. |
| 379 | # latex_show_urls = False |
| 380 | |
| 381 | # Documents to append as an appendix to all manuals. |
| 382 | # latex_appendices = [] |
| 383 | |
| 384 | # If false, no module index is generated. |
| 385 | # latex_domain_indices = True |
| 386 | |
| 387 | |
| 388 | # -- Options for manual page output -------------------------------------------- |
| 389 | |
| 390 | # One entry per manual page. List of tuples |
| 391 | # (source start file, name, description, authors, manual section). |
| 392 | man_pages = [ |
| 393 | ("index", "pyglet", "pyglet Documentation", |
| 394 | ["Alex Holkner"], 1), |
| 395 | ] |
| 396 | |
| 397 | # If true, show URL addresses after external links. |
| 398 | # man_show_urls = False |
| 399 | |
| 400 | |
| 401 | # -- Options for Texinfo output ------------------------------------------------ |
| 402 | |
| 403 | # Grouping the document tree into Texinfo files. List of tuples |
| 404 | # (source start file, target name, title, author, |
| 405 | # dir menu entry, description, category) |
| 406 | texinfo_documents = [ |
| 407 | ("index", "pyglet", "pyglet Documentation", |
| 408 | "Alex Holkner", "pyglet", "One line description of project.", |
| 409 | "Miscellaneous"), |
| 410 | ] |
| 411 | |
| 412 | # Documents to append as an appendix to all manuals. |
| 413 | # texinfo_appendices = [] |
| 414 | |
| 415 | # If false, no module index is generated. |
| 416 | # texinfo_domain_indices = True |
| 417 | |
| 418 | # How to display URL addresses: 'footnote', 'no', or 'inline'. |
| 419 | # texinfo_show_urls = 'footnote' |
| 420 | |
| 421 | python_maximum_signature_line_length = 85 |
| 422 | |
| 423 | nitpicky = True |
| 424 | |
| 425 | |
| 426 | def custom_formatter(annotation, config): |
| 427 | # Fixes TypeVar bounds, where the bound reference is forward referenced and class cannot be determined. |
| 428 | # Defaults to a class style. Seems good enough for now since most bound things are classes. |
| 429 | if isinstance(annotation, TypeVar): |
| 430 | try: |
| 431 | module = sphinx_autodoc_typehints.get_annotation_module(annotation) |
| 432 | class_name = sphinx_autodoc_typehints.get_annotation_class_name(annotation, module) |
| 433 | args = sphinx_autodoc_typehints.get_annotation_args(annotation, module, class_name) |
| 434 | except ValueError: |
| 435 | return str(annotation).strip("'") |
| 436 | params = {k: getattr(annotation, f"__{k}__") for k in ("bound", "covariant", "contravariant")} |
| 437 | params = {k: v for k, v in params.items() if v} |
| 438 | if "bound" in params: |
| 439 | bound_param = params["bound"] |
| 440 | if isinstance(bound_param, typing.ForwardRef): |
| 441 | # May be wrong but |
| 442 | params["bound"] = f":py:class:`{bound_param.__forward_arg__}`" |
| 443 | else: |
| 444 | params["bound"] = f"{sphinx_autodoc_typehints.format_annotation(bound_param, config)}" |
| 445 | args_format = f"\\(``{annotation.__name__}``{', {}' if args else ''}" |
| 446 | if params: |
| 447 | args_format += "".join(f", *{k} =* {v}" for k, v in params.items()) |
| 448 | args_format += ")" |
| 449 | formatted_args = None if args else args_format |
| 450 | return f":py:class:`TypeVar` {formatted_args}" |
| 451 | return None |
| 452 | |
| 453 | |
| 454 | typehints_formatter = custom_formatter |