Skip to content

Commit

Permalink
Prevent v2 imports from happening on systems with v3
Browse files Browse the repository at this point in the history
  • Loading branch information
dhood committed Jan 11, 2017
1 parent 3b888fc commit 036f3c2
Showing 1 changed file with 51 additions and 47 deletions.
98 changes: 51 additions & 47 deletions ament_flake8/ament_flake8/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,66 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import flake8.engine
from flake8.reporter import QueueReport
from distutils.version import LooseVersion

import flake8

class CustomReport(QueueReport):
# Ensure that importing this module doesn't fail when imported on systems with flake8>=3.0.
# This can happen during coverage tests on systems with flake8>=3.0, for example.
if LooseVersion(flake8.__version__) < '3.0':
import flake8.engine
from flake8.reporter import QueueReport

errors = []
files = []
class CustomReport(QueueReport):

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
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):
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 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

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)

# 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

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

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

return wrapperStyleGuide.check_files(paths)
return wrapperStyleGuide.check_files(paths)

0 comments on commit 036f3c2

Please sign in to comment.