Example #1
0
 def run(self):
     """Run analysis on a single file."""
     pep257.Error.explain = self.options['explain']
     filename, source = load_file(self.filename)
     for error in pep257.PEP257Checker().check_source(source, filename):
         if not hasattr(error, 'code') or ignore(error.code):
             continue
         lineno = error.line
         offset = 0  # Column number starting from 0.
         explanation = error.explanation if pep257.Error.explain else ''
         text = '{0} {1}{2}'.format(error.code, error.message.split(': ', 1)[1], explanation)
         yield lineno, offset, text, Main
Example #2
0
def check_pep257(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:: `GreenSteam/pep257 <https://github.com/GreenSteam/pep257/>`_

    """
    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 = pep257.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 pep257.AllError as e:
            errors.append(str(e))

    return errors
    def execute(self):
        """Check the code with pep257 to find errors
        """

        errors = []
        try:
            for error in pep257.PEP257Checker().check_source(
                    self.code, self.filename):
                error_code = getattr(error, 'code', None)
                if error_code is not None and error_code not in self.ignore:
                    errors.append(self._convert(error))
        except Exception as error:
            print(error)

        return errors