-
-
Notifications
You must be signed in to change notification settings - Fork 168
Unify CLIs #537
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
Unify CLIs #537
Changes from 7 commits
73f9a59
d1d43c1
02f5e5d
007c2ca
f10e786
5c79fe6
3008354
e87a4d1
ec69d5e
6e8491a
66400a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
- id: numpydoc-validation | ||
name: numpydoc-validation | ||
description: This hook validates that docstrings in committed files adhere to numpydoc standards. | ||
entry: validate-docstrings | ||
entry: numpydoc lint | ||
require_serial: true | ||
language: python | ||
types: [python] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"""The CLI for numpydoc.""" | ||
|
||
import argparse | ||
import ast | ||
from typing import List, Sequence, Union | ||
|
||
from .docscrape_sphinx import get_doc_object | ||
from .hooks import validate_docstrings | ||
from .validate import Validator, validate | ||
|
||
|
||
def render_object(import_path: str, config: Union[List[str], None] = None) -> int: | ||
"""Test numpydoc docstring generation for a given object.""" | ||
# TODO: Move Validator._load_obj to a better place than validate | ||
print(get_doc_object(Validator._load_obj(import_path), config=dict(config or []))) | ||
return 0 | ||
|
||
|
||
def validate_object(import_path: str) -> int: | ||
"""Run numpydoc docstring validation for a given object.""" | ||
exit_status = 0 | ||
results = validate(import_path) | ||
for err_code, err_desc in results["errors"]: | ||
exit_status += 1 | ||
print(":".join([import_path, err_code, err_desc])) | ||
return exit_status | ||
|
||
|
||
def main(argv: Union[Sequence[str], None] = None) -> int: | ||
"""CLI for numpydoc.""" | ||
ap = argparse.ArgumentParser(prog="numpydoc", description=__doc__) | ||
subparsers = ap.add_subparsers(title="subcommands") | ||
|
||
def _parse_config(s): | ||
key, _, value = s.partition("=") | ||
value = ast.literal_eval(value) | ||
return key, value | ||
|
||
render = subparsers.add_parser( | ||
"render", | ||
description="Test numpydoc docstring generation for a given object.", | ||
stefmolin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
help="generate the docstring with numpydoc", | ||
) | ||
render.add_argument("import_path", help="e.g. numpy.ndarray") | ||
render.add_argument( | ||
"-c", | ||
"--config", | ||
type=_parse_config, | ||
action="append", | ||
help="key=val where val will be parsed by literal_eval, " | ||
"e.g. -c use_plots=True. Multiple -c can be used.", | ||
) | ||
render.set_defaults(func=render_object) | ||
|
||
validate = subparsers.add_parser( | ||
"validate", | ||
description="Validate the docstring with numpydoc.", | ||
|
||
help="validate the object and report errors", | ||
) | ||
validate.add_argument("import_path", help="e.g. numpy.ndarray") | ||
validate.set_defaults(func=validate_object) | ||
|
||
lint_parser = validate_docstrings.get_parser(parent=subparsers) | ||
stefmolin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
lint_parser.set_defaults(func=validate_docstrings.run_hook) | ||
|
||
args = vars(ap.parse_args(argv)) | ||
func = args.pop("func", render_object) | ||
return func(**args) |
Uh oh!
There was an error while loading. Please reload this page.