|
12 | 12 | # All configuration values have a default; values that are commented out |
13 | 13 | # serve to show the default. |
14 | 14 |
|
15 | | -import m2r |
16 | 15 | import os |
17 | 16 | import re |
18 | | -import semver |
19 | 17 | import sys |
20 | 18 |
|
21 | | -from CPAC import __version__ |
22 | 19 | from dateutil import parser as dparser |
| 20 | +from CPAC import __version__ |
| 21 | +from CPAC.utils.monitoring import custom_logging |
23 | 22 | from github import Github |
24 | 23 | from github.GithubException import RateLimitExceededException, \ |
25 | 24 | UnknownObjectException |
| 25 | +import m2r |
| 26 | +import semver |
26 | 27 | from pybtex.plugin import register_plugin |
27 | 28 |
|
28 | 29 | sys.path.append(os.path.dirname(__file__)) |
@@ -571,45 +572,59 @@ def _unireplace(release_note, unireplace): |
571 | 572 | ) if len(gh_tags) >= 5 else "" |
572 | 573 |
|
573 | 574 |
|
574 | | -def format_node_block_docstrings(app, what, name, obj, options, lines): |
575 | | - _ = (app, name, obj, options) |
576 | | - if what in ["function"]: |
577 | | - indent = 0 |
578 | | - insert_at = None |
579 | | - first_to_del = None |
580 | | - nevermore = False |
581 | | - for i, line in enumerate(lines): |
582 | | - if nevermore and not line.strip(): |
583 | | - first_to_del = i + 1 |
584 | | - nevermore = False |
585 | | - if line.lstrip().startswith("Node Block:"): |
586 | | - insert_at = i + 1 |
587 | | - indent = 3 |
588 | | - elif indent == 0 and re.match(r"\s*{['\"]name['\"]:", line): |
| 575 | +def autodoc_process_docstring(app, what, name, obj, options, lines) -> None: |
| 576 | + """Modify docstrings before parsing RST""" |
| 577 | + # pylint: disable=too-many-arguments,unused-argument |
| 578 | + initialize_factory() |
| 579 | + if what == "function": |
| 580 | + format_node_block_docstrings(lines) |
| 581 | + |
| 582 | + |
| 583 | +def format_node_block_docstrings(lines: list) -> None: |
| 584 | + """Format Node Block docstring dictionaries as Python code blocks |
| 585 | +
|
| 586 | + Parameters |
| 587 | + ---------- |
| 588 | + lines : list |
| 589 | + modified in-place |
| 590 | + """ |
| 591 | + indent = 0 |
| 592 | + insert_at = None |
| 593 | + first_to_del = None |
| 594 | + nevermore = False |
| 595 | + for i, line in enumerate(lines): |
| 596 | + if nevermore and not line.strip(): |
| 597 | + first_to_del = i + 1 |
| 598 | + nevermore = False |
| 599 | + if line.lstrip().startswith("Node Block:"): |
| 600 | + insert_at = i + 1 |
| 601 | + indent = 3 |
| 602 | + else: |
| 603 | + if indent == 0 and re.match(r"\s*{['\"]name['\"]:", line): |
589 | 604 | insert_at = i |
590 | 605 | indent = 3 |
591 | | - else: |
592 | | - lines[i] = f'{" " * indent}{line}' |
593 | | - if re.match(r"\s*{['\"]outputs['\"]:", line): |
594 | | - nevermore = True |
595 | | - if first_to_del is not None: |
596 | | - del lines[first_to_del:] |
597 | | - if insert_at is not None: |
598 | | - lines.insert(insert_at, '') |
599 | | - lines.insert(insert_at, ".. code-block:: Python") |
600 | | - print('\n'.join([f'|{line}|' for line in lines])) |
601 | | - |
602 | | - |
603 | | -def setup(app): |
604 | | - from CPAC.utils.monitoring import custom_logging |
605 | | - |
606 | | - # initilaize class to make factory functions available to Sphinx |
607 | | - ml = custom_logging.MockLogger('test', 'test.log', 0, '/tmp') |
| 606 | + lines[i] = f'{" " * indent}{line}' |
| 607 | + if re.match(r"\s*{['\"]outputs['\"]:", line): |
| 608 | + nevermore = True |
| 609 | + if first_to_del is not None: |
| 610 | + del lines[first_to_del:] |
| 611 | + if insert_at is not None: |
| 612 | + lines.insert(insert_at, '') |
| 613 | + lines.insert(insert_at, ".. code-block:: Python") |
| 614 | + |
| 615 | + |
| 616 | +def initialize_factory() -> None: |
| 617 | + """Initilaize class to make factory functions available to Sphinx""" |
| 618 | + mocklogger = custom_logging.MockLogger('test', 'test.log', 0, '/tmp') |
608 | 619 | for method in [ |
609 | 620 | method for method in |
610 | | - set(dir(ml)) - set(dir(custom_logging.MockLogger)) if |
| 621 | + set(dir(mocklogger)) - set(dir(custom_logging.MockLogger)) if |
611 | 622 | method not in ['name', 'handlers'] |
612 | 623 | ]: |
613 | | - setattr(custom_logging.MockLogger, method, getattr(ml, method)) |
| 624 | + setattr(custom_logging.MockLogger, method, |
| 625 | + getattr(mocklogger, method)) |
| 626 | + |
614 | 627 |
|
615 | | - app.connect('autodoc-process-docstring', format_node_block_docstrings) |
| 628 | +def setup(app) -> None: |
| 629 | + """Extend Sphinx""" |
| 630 | + app.connect('autodoc-process-docstring', autodoc_process_docstring) |
0 commit comments