Skip to content

Commit

Permalink
Support flake8 v2 as well as v3
Browse files Browse the repository at this point in the history
  • Loading branch information
dhood committed Jan 4, 2017
1 parent c8fce1b commit bf07f5b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 11 deletions.
75 changes: 75 additions & 0 deletions ament_flake8/ament_flake8/legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3

# Copyright 2016 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import flake8.engine
from flake8.reporter import QueueReport


class CustomReport(QueueReport):

errors = []
files = []

def error(self, line_number, offset, text, check):
code = super(CustomReport, self).error(
line_number, offset, text, check)
line = self.lines[line_number - 1] \
if line_number <= len(self.lines) else ''
self.errors.append({
'path': self.filename,
'row': self.line_offset + line_number,
'column': offset + 1,
'error_code': code,
'error_message': text,
'source_line': line.splitlines()[0] if line else '',
})
return code


class CustomStyleGuide(flake8.engine.NoQAStyleGuide):

def input_file(self, filename, **kwargs):
self.options.reporter.files.append(filename)
return super(CustomStyleGuide, self).input_file(filename, **kwargs)


def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
kwargs = {
'repeat': True,
'show_source': True,
'verbose': True,
'reporter': CustomReport,
'config_file': config_file,
'jobs': 1,
}
if max_line_length is not None:
kwargs['max_line_length'] = max_line_length

# add options for flake8 plugins
kwargs['parser'], options_hooks = flake8.engine.get_parser()
flake8style = CustomStyleGuide(**kwargs)
options = flake8style.options
for options_hook in options_hooks:
options_hook(options)

if excludes:
flake8style.options.exclude += excludes

# flake8 uses a wrapper StyleGuide to handle some particular OSErrors
kwargs['styleguide'] = flake8style
wrapperStyleGuide = flake8.engine.StyleGuide(**kwargs)

return wrapperStyleGuide.check_files(paths)
19 changes: 8 additions & 11 deletions ament_flake8/ament_flake8/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@
# limitations under the License.

import argparse
from distutils.version import LooseVersion
import os
import sys
import time
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr

import flake8
from distutils.version import LooseVersion


def main(argv=sys.argv[1:]):
if LooseVersion(flake8.__version__) < '3.0':
print(
"Found Flake8 version '%s'; " % flake8.__version__ +
"only version >= 3.0 is supported.", file=sys.stderr)
return 1

global flake8_app, StyleGuide
if LooseVersion(flake8.__version__) >= '3.0':
from flake8.main import application as flake8_app
from flake8.api.legacy import StyleGuide


def main(argv=sys.argv[1:]):
config_file = os.path.join(
os.path.dirname(__file__), 'configuration', 'ament_flake8.ini')

Expand Down Expand Up @@ -137,6 +130,10 @@ def get_flake8_style_guide(argv):


def generate_flake8_report(config_file, paths, excludes, max_line_length=None):
if LooseVersion(flake8.__version__) < '3.0':
from ament_flake8.legacy import generate_flake8_report
return generate_flake8_report(config_file, paths, excludes, max_line_length)

flake8_argv = []
flake8_argv.append('--config={0}'.format(config_file))
flake8_argv.append('--exclude={0}'.format(excludes))
Expand Down

0 comments on commit bf07f5b

Please sign in to comment.