Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process docstring cli testing #215

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/hawkmoth/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import os
import sys

from hawkmoth.ext import javadoc
from hawkmoth.ext import napoleon
from hawkmoth.parser import parse
from hawkmoth.util import doccompat

Expand All @@ -19,7 +21,17 @@ def filename(file):
return file
raise ValueError

def _process_docstring(args, lines):
def _process_docstring(transform, lines):
transformations = {
'napoleon': napoleon.process_docstring,
'javadoc': javadoc.process_docstring,
}

fn = transformations.get(transform)
if fn:
fn(lines)

def _process_docstring_compat(args, lines):
text = '\n'.join(lines)
text = doccompat.convert(text, transform=args.compat)
lines[:] = [line for line in text.splitlines()]
Expand All @@ -45,11 +57,20 @@ def main():
choices=['c', 'cpp'],
default='c',
help='Sphinx domain to be used.')
parser.add_argument('--compat',
choices=['none',
'javadoc-basic',
'javadoc-liberal',
'kernel-doc'],
compat = parser.add_mutually_exclusive_group()
compat.add_argument('--process-docstring',
choices=[
'javadoc',
'napoleon',
],
help='Process docstring.')
compat.add_argument('--compat',
choices=[
'none',
'javadoc-basic',
'javadoc-liberal',
'kernel-doc'
],
help='Compatibility options. See cautodoc_compat.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider raising DeprecationWarning 🤷‍♂️

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: we may also want to institutionalize a deprecation policy so we have a regular, well advertised schedule for dropping things. We have an important user now and everything <.<

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the deprecation has been haphazard so far. If it's release based, I guess it'll have to be major releases. And we're not even at 1.0. 😁

parser.add_argument('--clang', metavar='PARAM', action='append',
help='Argument to pass to Clang. May be specified multiple times. See cautodoc_clang.') # noqa: E501
Expand All @@ -62,7 +83,10 @@ def main():

comments, errors = parse(args.file, domain=args.domain, clang_args=args.clang)

process_docstring = lambda lines: _process_docstring(args, lines)
if args.process_docstring:
process_docstring = lambda lines: _process_docstring(args.process_docstring, lines)
else:
process_docstring = lambda lines: _process_docstring_compat(args, lines)

for comment in comments.walk():
if args.verbose:
Expand Down
6 changes: 6 additions & 0 deletions src/hawkmoth/ext/javadoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ def _process_docstring(app, lines, transform, options):
comment = javadoc_liberal(comment)
lines[:] = comment.splitlines()[:]

def process_docstring(lines):
"""Simple interface for CLI and testing."""
comment = '\n'.join(lines)
comment = javadoc_liberal(comment)
lines[:] = comment.splitlines()[:]

def setup(app):
app.setup_extension('hawkmoth')

Expand Down
11 changes: 9 additions & 2 deletions src/hawkmoth/ext/napoleon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2023, Jani Nikula <[email protected]>
# Licensed under the terms of BSD 2-Clause, see LICENSE for details.

from sphinx.ext.napoleon import _process_docstring
from sphinx.ext import napoleon

def _process_docstring_proxy(app, lines, transform, options):
if transform != app.config.hawkmoth_napoleon_transform:
Expand All @@ -12,7 +12,14 @@ def _process_docstring_proxy(app, lines, transform, options):
# directly, but the alternative is duplicating all it does, which is also
# ugly.

return _process_docstring(app, None, None, None, options, lines)
return napoleon._process_docstring(app, None, None, None, options, lines)

def process_docstring(lines):
"""Simple interface for CLI and testing."""
comment = '\n'.join(lines)
config = napoleon.Config(napoleon_use_rtype=False)
comment = str(napoleon.docstring.GoogleDocstring(comment, config))
lines[:] = comment.splitlines()[:]

def setup(app):
app.setup_extension('sphinx.ext.napoleon')
Expand Down
2 changes: 1 addition & 1 deletion test/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def get_output(self):

transform = directive.options.get('transform')
if transform is not None:
pytest.skip('cli does not support generic transformations')
args += [f'--process-docstring={transform}']

clang_args = directive.get_clang_args()
if clang_args:
Expand Down
32 changes: 11 additions & 21 deletions test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,23 @@

import os

from sphinx.ext import napoleon
import pytest

from hawkmoth import docstring
from hawkmoth.ext import javadoc
from hawkmoth.ext import napoleon
from hawkmoth.parser import parse
from hawkmoth.util import doccompat
from test import testenv

def napoleon_transform(comment):
config = napoleon.Config(napoleon_use_rtype=False)
return str(napoleon.docstring.GoogleDocstring(comment, config))

parser_transformations = {
'napoleon': napoleon_transform,
'javadoc': doccompat.javadoc_liberal,
}

def _process_docstring(transform, lines):
if transform:
text = '\n'.join(lines)
text = transform(text)
lines[:] = [line for line in text.splitlines()]
transformations = {
'napoleon': napoleon.process_docstring,
'javadoc': javadoc.process_docstring,
}

fn = transformations.get(transform)
if fn:
fn(lines)

def _filter_types(directive):
types = {
Expand Down Expand Up @@ -104,12 +99,7 @@ def get_output(self):
if filter_clang_args is not None and root.get_clang_args() not in filter_clang_args:
continue

tropt = directive.options.get('transform')
if tropt is not None:
transform = parser_transformations[tropt]
else:
transform = None

transform = directive.options.get('transform')
process_docstring = lambda lines: _process_docstring(transform, lines)

for docstrings in root.walk(recurse=False, filter_types=_filter_types(directive),
Expand Down
Loading