class Pronto::Flake8

Flake8 Pronto Runner. Entry point is run

Constants

CONFIG_FILE
CONFIG_KEYS
PYTHON_FILE_EXTENSION

Public Class Methods

new(patches, commit = nil) click to toggle source
Calls superclass method
# File lib/pronto/flake8.rb, line 11
def initialize(patches, commit = nil)
  super(patches, commit)
  read_config
end

Public Instance Methods

files() click to toggle source
# File lib/pronto/flake8.rb, line 20
def files
  return [] if @patches.nil?

  @files ||= begin
   @patches
     .select { |patch| patch.additions > 0 }
     .map(&:new_file_full_path)
     .compact
 end
end
filter_python_files(all_files) click to toggle source
# File lib/pronto/flake8.rb, line 74
def filter_python_files(all_files)
  all_files.select { |file_path| file_path.to_s.end_with? PYTHON_FILE_EXTENSION}
           .map { |py_file| py_file.to_s.shellescape }
end
flake8_executable() click to toggle source
# File lib/pronto/flake8.rb, line 16
def flake8_executable
  @flake8_executable || 'flake8'.freeze
end
git_repo_path() click to toggle source
# File lib/pronto/flake8.rb, line 123
def git_repo_path
  @git_repo_path ||= Rugged::Repository.discover(File.expand_path(Dir.pwd)).workdir
end
messages(json_list) click to toggle source
# File lib/pronto/flake8.rb, line 107
def messages(json_list)
  json_list.map do |error|
    patch_line = patch_line_for_offence(error[:file_path],
                                        error[:line_number])
    next if patch_line.nil?
    description = error[:message]
    path = patch_line.patch.delta.new_file[:path]
    Message.new(path,
                patch_line,
                error[:level].to_sym,
                description,
                nil,
                self.class)
  end
end
parse_output(executable_output) click to toggle source
# File lib/pronto/flake8.rb, line 79
def parse_output(executable_output)
  lines = executable_output.split("\n")
  lines.map { |line| parse_output_line(line) }
end
parse_output_line(line) click to toggle source
# File lib/pronto/flake8.rb, line 84
def parse_output_line(line)
  splits = line.strip.split(':')
  message = splits[3].strip
  {
    file_path: splits[0],
    line_number: splits[1].to_i,
    column_number: splits[2].to_i,
    message: message,
    level: violation_level(message)
  }
end
patch_line_for_offence(path, lineno) click to toggle source
# File lib/pronto/flake8.rb, line 31
def patch_line_for_offence(path, lineno)
  patch_node = @patches.find do |patch|
    patch.new_file_full_path.to_s == path
  end

  return if patch_node.nil?

  patch_node.added_lines.find do |patch_line|
    patch_line.new_lineno == lineno
  end
end
read_config() click to toggle source
# File lib/pronto/flake8.rb, line 43
def read_config
  config_file = File.join(git_repo_path, CONFIG_FILE)
  return unless File.exist?(config_file)
  config = YAML.load_file(config_file)

  CONFIG_KEYS.each do |config_key|
    next unless config[config_key]
    instance_variable_set("@#{config_key}", config[config_key])
  end
end
run() click to toggle source
# File lib/pronto/flake8.rb, line 54
def run
  if files.any?
    messages(run_flake8)
  else
    []
  end
end
run_flake8() click to toggle source
# File lib/pronto/flake8.rb, line 62
def run_flake8
  Dir.chdir(git_repo_path) do
    python_files = filter_python_files(files)
    file_paths_str = python_files.join(' ')
    if !file_paths_str.empty?
      parse_output `#{flake8_executable} --format=default #{file_paths_str}`
    else
      []
    end
  end
end
violation_level(flake8_message) click to toggle source
# File lib/pronto/flake8.rb, line 96
def violation_level(flake8_message)
  message = flake8_message.strip
  first_letter = message[0]
  # W is warning, C is for McCabe Complexity
  if %w[W C].include? first_letter
    'warning'
  else
    'error'
  end
end