Perform static analysis on the given file docstrings.

    :param filename: path of file to check.
    :type filename: str
    :param ignore: codes to ignore, e.g. ('D400',)
    :type ignore: `list`
    :param match: regex the filename has to match to be checked
    :type match: str
    :param match_dir: regex everydir in path should match to be checked
    :type match_dir: str
    :return: errors
    :rtype: `list`

    .. seealso::
        `PyCQA/pydocstyle <https://github.com/GreenSteam/pydocstyle/>`_

def check_pydocstyle(filename, **kwargs):
    """Perform static analysis on the given file docstrings.

    :param filename: path of file to check.
    :type filename: str
    :param ignore: codes to ignore, e.g. ('D400',)
    :type ignore: `list`
    :param match: regex the filename has to match to be checked
    :type match: str
    :param match_dir: regex everydir in path should match to be checked
    :type match_dir: str
    :return: errors
    :rtype: `list`

    .. seealso::
        `PyCQA/pydocstyle <https://github.com/GreenSteam/pydocstyle/>`_

    """
    ignore = kwargs.get("ignore")
    match = kwargs.get("match", None)
    match_dir = kwargs.get("match_dir", None)

    errors = []

    if match and not re.match(match, os.path.basename(filename)):
        return errors

    if match_dir:
        # FIXME here the full path is checked, be sure, if match_dir doesn't
        # match the path (usually temporary) before the actual application path
        # it may not run the checks when it should have.
        path = os.path.split(os.path.abspath(filename))[0]
        while path != "/":
            path, dirname = os.path.split(path)
            if not re.match(match_dir, dirname):
                return errors

    checker = pydocstyle.PEP257Checker()
    with open(filename) as fp:
        try:
            for error in checker.check_source(fp.read(), filename):
                if ignore is None or error.code not in ignore:
                    # Removing the colon ':' after the error code
                    message = re.sub("(D[0-9]{3}): ?(.*)",
                                     r"\1 \2",
                                     error.message)
                    errors.append("{0}: {1}".format(error.line, message))
        except tokenize.TokenError as e:
            errors.append("{1}:{2} {0}".format(e.args[0], *e.args[1]))
        except pydocstyle.AllError as e:
            errors.append(str(e))

    return errors