def parse_docstring()

in build/gen_py_api_docs.py [0:0]


def parse_docstring(f):
    """
    Parse the docstring of `f` and return a list of parameters
    """
    doc = inspect.cleandoc(f.__doc__)

    lines = doc.splitlines()
    params = []
    currentparam = None
    shortdoc = ""

    def flush_param_if_necessary():
        if currentparam:
            params.append(currentparam)

    # Loop through all the lines of the docstring
    for i, line in enumerate(lines):
        match = re.match(r':param\s+(\w+):\s+(.*)', line)
        if match:
            # This is a new param
            flush_param_if_necessary()

            currentparam = Parameter(name=match.group(1))
            currentparam.add_doc_line(match.group(2))
        elif i == 0:
            # If this is the first line and it's not a new parameter,
            # this is the shortdoc
            shortdoc = line
        elif currentparam is not None:
            # This is part of the documentation of the current parameter
            currentparam.add_doc_line(line)

    # Make sure we get the final parameter
    flush_param_if_necessary()

    # Check for default values
    params_required = []
    params_defaults = []
    for param in params:
        if param.name in f.neuropod_default_args:
            param.set_default(f.neuropod_default_args[param.name])
            params_defaults.append(param)
        else:
            params_required.append(param)

    return Function(name=f.__name__, shortdoc=shortdoc, params=params_required + params_defaults)