Skip to content

Commit cfd32b5

Browse files
committed
🎨 Update Sphinx formatting
1 parent 51bc781 commit cfd32b5

File tree

1 file changed

+53
-38
lines changed

1 file changed

+53
-38
lines changed

docs/_sources/conf.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
# All configuration values have a default; values that are commented out
1313
# serve to show the default.
1414

15-
import m2r
1615
import os
1716
import re
18-
import semver
1917
import sys
2018

21-
from CPAC import __version__
2219
from dateutil import parser as dparser
20+
from CPAC import __version__
21+
from CPAC.utils.monitoring import custom_logging
2322
from github import Github
2423
from github.GithubException import RateLimitExceededException, \
2524
UnknownObjectException
25+
import m2r
26+
import semver
2627
from pybtex.plugin import register_plugin
2728

2829
sys.path.append(os.path.dirname(__file__))
@@ -571,45 +572,59 @@ def _unireplace(release_note, unireplace):
571572
) if len(gh_tags) >= 5 else ""
572573

573574

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):
589604
insert_at = i
590605
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')
608619
for method in [
609620
method for method in
610-
set(dir(ml)) - set(dir(custom_logging.MockLogger)) if
621+
set(dir(mocklogger)) - set(dir(custom_logging.MockLogger)) if
611622
method not in ['name', 'handlers']
612623
]:
613-
setattr(custom_logging.MockLogger, method, getattr(ml, method))
624+
setattr(custom_logging.MockLogger, method,
625+
getattr(mocklogger, method))
626+
614627

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

Comments
 (0)