Skip to content

Commit

Permalink
autoparse can now split docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucretiel committed Aug 10, 2017
1 parent fe4a839 commit 6f13946
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/autocommand/autoparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with autocommand. If not, see <http://www.gnu.org/licenses/>.

import sys
from re import compile as compile_regex
from inspect import signature, getdoc, Parameter
from argparse import ArgumentParser
from contextlib import contextmanager
Expand All @@ -41,6 +42,18 @@ class KWArgError(AutocommandError):
'''kwarg Error: autocommand can't handle a **kwargs parameter'''


class DocstringError(AutocommandError):
'''Docstring error'''


class TooManySplitsError(DocstringError):
'''
The docstring had too many ---- section splits. Currently we only support
using up to a single split, to split the docstring into description and
epilog parts.
'''


def _get_type_description(annotation):
'''
Given an annotation, return the (type, description) for the parameter.
Expand Down Expand Up @@ -196,6 +209,26 @@ def make_parser(func_sig, description, epilog, add_nos):
return parser


_DOCSTRING_SPLIT = compile_regex(r'\n\s*-{4,}\s*\n')


def parse_docstring(docstring):
'''
Given a docstring, parse it into a description and epilog part
'''
if docstring is None:
return '', ''

parts = _DOCSTRING_SPLIT.split(docstring)

if len(parts) == 1:
return docstring, ''
elif len(parts) == 2:
return parts[0], parts[1]
else:
raise TooManySplitsError()


def autoparse(
func=None, *,
description=None,
Expand Down Expand Up @@ -245,11 +278,13 @@ def autoparse(

func_sig = signature(func)

docstr_description, docstr_epilog = parse_docstring(getdoc(func))

if parser is None:
parser = make_parser(
func_sig,
description or getdoc(func),
epilog,
description or docstr_description,
epilog or docstr_epilog,
add_nos)

@wraps(func)
Expand Down

0 comments on commit 6f13946

Please sign in to comment.