|
32 | 32 | "numpydoc", |
33 | 33 | ] |
34 | 34 |
|
| 35 | +napoleon_google_docstring = False |
| 36 | +napoleon_numpy_docstring = True |
| 37 | +napoleon_include_init_with_doc = False |
| 38 | +napoleon_use_rtype = False |
| 39 | +napoleon_custom_sections = [("Params", "Parameters")] |
35 | 40 |
|
36 | 41 | # this is needed for some reason... |
37 | 42 | # see https://github.com/numpy/numpydoc/issues/69 |
|
63 | 68 | # The master toctree document. |
64 | 69 | master_doc = "index" |
65 | 70 |
|
66 | | -project = u"scikit-dimension" |
67 | | -copyright = u"2020, Jonathan Bac" |
| 71 | +project = "scikit-dimension" |
| 72 | +copyright = "2020, Jonathan Bac" |
68 | 73 |
|
69 | 74 | # The short X.Y version. |
70 | 75 | version = __version__ |
|
216 | 221 | ( |
217 | 222 | "index", |
218 | 223 | "scikit-dimension.tex", |
219 | | - u"scikit-dimension Documentation", |
220 | | - u"Jonathan Bac", |
| 224 | + "scikit-dimension Documentation", |
| 225 | + "Jonathan Bac", |
221 | 226 | "manual", |
222 | 227 | ), |
223 | 228 | ] |
|
313 | 318 | def setup(app): |
314 | 319 | # a copy button to copy snippet of code from the documentation |
315 | 320 | app.add_stylesheet("css/custom.css") |
| 321 | + |
| 322 | + |
| 323 | +# -- Options for other output ------------------------------------------ |
| 324 | +import inspect |
| 325 | +from pathlib import Path |
| 326 | +from typing import Optional, Union, Mapping |
| 327 | +import logging |
| 328 | +from sphinx.application import Sphinx |
| 329 | +from sphinx.ext import autosummary |
| 330 | + |
| 331 | +logger = logging.getLogger(__name__) |
| 332 | +author = "Jonathan Bac" |
| 333 | +title = "scikit_dimension" |
| 334 | +title_doc = f"{project} documentation" |
| 335 | + |
| 336 | +latex_documents = [(master_doc, f"{project}.tex", title_doc, author, "manual")] |
| 337 | +man_pages = [(master_doc, project, title_doc, [author], 1)] |
| 338 | +texinfo_documents = [ |
| 339 | + (master_doc, project, title_doc, author, project, title, "Miscellaneous") |
| 340 | +] |
| 341 | + |
| 342 | + |
| 343 | +# -- generate_options override ------------------------------------------ |
| 344 | + |
| 345 | + |
| 346 | +def process_generate_options(app: Sphinx): |
| 347 | + genfiles = app.config.autosummary_generate |
| 348 | + |
| 349 | + if genfiles and not hasattr(genfiles, "__len__"): |
| 350 | + env = app.builder.env |
| 351 | + genfiles = [ |
| 352 | + env.doc2path(x, base=None) |
| 353 | + for x in env.found_docs |
| 354 | + if Path(env.doc2path(x)).is_file() |
| 355 | + ] |
| 356 | + if not genfiles: |
| 357 | + return |
| 358 | + |
| 359 | + from sphinx.ext.autosummary.generate import generate_autosummary_docs |
| 360 | + |
| 361 | + ext = app.config.source_suffix |
| 362 | + genfiles = [ |
| 363 | + genfile + (not genfile.endswith(tuple(ext)) and ext[0] or "") |
| 364 | + for genfile in genfiles |
| 365 | + ] |
| 366 | + |
| 367 | + suffix = autosummary.get_rst_suffix(app) |
| 368 | + if suffix is None: |
| 369 | + return |
| 370 | + |
| 371 | + generate_autosummary_docs( |
| 372 | + genfiles, |
| 373 | + builder=app.builder, |
| 374 | + warn=logger.warning, |
| 375 | + info=logger.info, |
| 376 | + suffix=suffix, |
| 377 | + base_path=app.srcdir, |
| 378 | + imported_members=True, |
| 379 | + app=app, |
| 380 | + ) |
| 381 | + |
| 382 | + |
| 383 | +autosummary.process_generate_options = process_generate_options |
| 384 | + |
| 385 | + |
| 386 | +# -- GitHub URLs for class and method pages ------------------------------------------ |
| 387 | + |
| 388 | + |
| 389 | +def get_obj_module(qualname): |
| 390 | + """Get a module/class/attribute and its original module by qualname""" |
| 391 | + modname = qualname |
| 392 | + classname = None |
| 393 | + attrname = None |
| 394 | + while modname not in sys.modules: |
| 395 | + attrname = classname |
| 396 | + modname, classname = modname.rsplit(".", 1) |
| 397 | + |
| 398 | + # retrieve object and find original module name |
| 399 | + if classname: |
| 400 | + cls = getattr(sys.modules[modname], classname) |
| 401 | + modname = cls.__module__ |
| 402 | + obj = getattr(cls, attrname) if attrname else cls |
| 403 | + else: |
| 404 | + obj = None |
| 405 | + |
| 406 | + return obj, sys.modules[modname] |
| 407 | + |
| 408 | + |
| 409 | +def get_linenos(obj): |
| 410 | + """Get an object’s line numbers""" |
| 411 | + try: |
| 412 | + lines, start = inspect.getsourcelines(obj) |
| 413 | + except TypeError: |
| 414 | + return None, None |
| 415 | + else: |
| 416 | + return start, start + len(lines) - 1 |
| 417 | + |
| 418 | + |
| 419 | +# set project_dir: project/docs/source/conf.py/../../.. → project/ |
| 420 | +project_dir = Path(__file__).parent.parent.parent |
| 421 | +github_url_scvelo = "https://github.com/j-bac/scikit-dimension/tree/master" |
| 422 | +from pathlib import PurePosixPath |
| 423 | + |
| 424 | + |
| 425 | +def modurl(qualname): |
| 426 | + """Get the full GitHub URL for some object’s qualname.""" |
| 427 | + obj, module = get_obj_module(qualname) |
| 428 | + github_url = github_url_scvelo |
| 429 | + # try: |
| 430 | + path = PurePosixPath(Path(module.__file__).resolve().relative_to(project_dir)) |
| 431 | + # except ValueError: |
| 432 | + # trying to document something from another package |
| 433 | + # github_url = ( |
| 434 | + # if "read" in qualname |
| 435 | + # ) |
| 436 | + # path = "/".join(module.__file__.split("/")[-2:]) |
| 437 | + start, end = get_linenos(obj) |
| 438 | + fragment = f"#L{start}-L{end}" if start and end else "" |
| 439 | + return f"{github_url}/{path}{fragment}" |
| 440 | + |
| 441 | + |
| 442 | +def api_image(qualname: str) -> Optional[str]: |
| 443 | + path = Path(__file__).parent / f"{qualname}.png" |
| 444 | + print(path, path.is_file()) |
| 445 | + return ( |
| 446 | + f".. image:: {path.name}\n :width: 200\n :align: right" |
| 447 | + if path.is_file() |
| 448 | + else "" |
| 449 | + ) |
| 450 | + |
| 451 | + |
| 452 | +# modify the default filters |
| 453 | +from jinja2.defaults import DEFAULT_FILTERS |
| 454 | + |
| 455 | +DEFAULT_FILTERS.update(modurl=modurl, api_image=api_image) |
| 456 | + |
| 457 | +# -- Override some classnames in autodoc -------------------------------------------- |
| 458 | + |
| 459 | +import sphinx_autodoc_typehints |
| 460 | + |
| 461 | +qualname_overrides = { |
| 462 | + "anndata.base.AnnData": "anndata.AnnData", |
| 463 | +} |
| 464 | + |
| 465 | +fa_orig = sphinx_autodoc_typehints.format_annotation |
| 466 | + |
| 467 | + |
| 468 | +def format_annotation(annotation): |
| 469 | + if getattr(annotation, "__origin__", None) is Union or hasattr( |
| 470 | + annotation, "__union_params__" |
| 471 | + ): |
| 472 | + params = getattr(annotation, "__union_params__", None) or getattr( |
| 473 | + annotation, "__args__", None |
| 474 | + ) |
| 475 | + return ", ".join(map(format_annotation, params)) |
| 476 | + if getattr(annotation, "__origin__", None) is Mapping: |
| 477 | + return ":class:`~typing.Mapping`" |
| 478 | + if inspect.isclass(annotation): |
| 479 | + full_name = f"{annotation.__module__}.{annotation.__qualname__}" |
| 480 | + override = qualname_overrides.get(full_name) |
| 481 | + if override is not None: |
| 482 | + return f":py:class:`~{qualname_overrides[full_name]}`" |
| 483 | + return fa_orig(annotation) |
| 484 | + |
| 485 | + |
| 486 | +sphinx_autodoc_typehints.format_annotation = format_annotation |
| 487 | + |
| 488 | + |
| 489 | +# -- Prettier Param docs -------------------------------------------- |
| 490 | + |
| 491 | +from typing import Dict, List, Tuple |
| 492 | +from docutils import nodes |
| 493 | +from sphinx import addnodes |
| 494 | +from sphinx.domains.python import PyTypedField, PyObject |
| 495 | +from sphinx.environment import BuildEnvironment |
| 496 | + |
| 497 | + |
| 498 | +class PrettyTypedField(PyTypedField): |
| 499 | + list_type = nodes.definition_list |
| 500 | + |
| 501 | + def make_field( |
| 502 | + self, |
| 503 | + types: Dict[str, List[nodes.Node]], |
| 504 | + domain: str, |
| 505 | + items: Tuple[str, List[nodes.inline]], |
| 506 | + env: BuildEnvironment = None, |
| 507 | + ) -> nodes.field: |
| 508 | + def makerefs(rolename, name, node): |
| 509 | + return self.make_xrefs(rolename, domain, name, node, env=env) |
| 510 | + |
| 511 | + def handle_item( |
| 512 | + fieldarg: str, content: List[nodes.inline] |
| 513 | + ) -> nodes.definition_list_item: |
| 514 | + head = nodes.term() |
| 515 | + head += makerefs(self.rolename, fieldarg, addnodes.literal_strong) |
| 516 | + fieldtype = types.pop(fieldarg, None) |
| 517 | + if fieldtype is not None: |
| 518 | + head += nodes.Text(" : ") |
| 519 | + if len(fieldtype) == 1 and isinstance(fieldtype[0], nodes.Text): |
| 520 | + (text_node,) = fieldtype # type: nodes.Text |
| 521 | + head += makerefs( |
| 522 | + self.typerolename, text_node.astext(), addnodes.literal_emphasis |
| 523 | + ) |
| 524 | + else: |
| 525 | + head += fieldtype |
| 526 | + |
| 527 | + body_content = nodes.paragraph("", "", *content) |
| 528 | + body = nodes.definition("", body_content) |
| 529 | + |
| 530 | + return nodes.definition_list_item("", head, body) |
| 531 | + |
| 532 | + fieldname = nodes.field_name("", self.label) |
| 533 | + if len(items) == 1 and self.can_collapse: |
| 534 | + fieldarg, content = items[0] |
| 535 | + bodynode = handle_item(fieldarg, content) |
| 536 | + else: |
| 537 | + bodynode = self.list_type() |
| 538 | + for fieldarg, content in items: |
| 539 | + bodynode += handle_item(fieldarg, content) |
| 540 | + fieldbody = nodes.field_body("", bodynode) |
| 541 | + return nodes.field("", fieldname, fieldbody) |
| 542 | + |
| 543 | + |
| 544 | +# replace matching field types with ours |
| 545 | +PyObject.doc_field_types = [ |
| 546 | + PrettyTypedField( |
| 547 | + ft.name, |
| 548 | + names=ft.names, |
| 549 | + typenames=ft.typenames, |
| 550 | + label=ft.label, |
| 551 | + rolename=ft.rolename, |
| 552 | + typerolename=ft.typerolename, |
| 553 | + can_collapse=ft.can_collapse, |
| 554 | + ) |
| 555 | + if isinstance(ft, PyTypedField) |
| 556 | + else ft |
| 557 | + for ft in PyObject.doc_field_types |
| 558 | +] |
0 commit comments