class Danger::DangerPep8

Find [PEP 8](www.python.org/dev/peps/pep-0008/) issues in python files.

This is done using the [flake8](pypi.python.org/pypi/flake8) python egg. Results are passed out as a markdown table.

@example Lint files inside the current directory

pep8.lint

@example Lint files inside a given directory

pep8.base_dir = "src"
pep8.lint

@example Warns if number of issues is greater than a given threshold

pep8.threshold = 10
pep8.count_errors

@example Fails if number of issues is greater than a given threshold

pep8.threshold = 10
pep8.count_errors(should_fail: true)

@see loadsmart/danger-pep8 @tags lint, python, pep8, code, style

Constants

MARKDOWN_TEMPLATE

Attributes

base_dir[W]

Root directory from where flake8 will run. Defaults to current directory. @return [String]

config_file[RW]

A custom configuration file to run with flake8 By default, flake will look for setup.cfg, tox.ini, or .flake8 inside the current directory or your top-level user directory @see flake8.pycqa.org/en/latest/user/configuration.html#configuration-locations @return [String]

threshold[W]

Max number of issues allowed. If number of issues is lesser than the threshold, nothing happens. @return [Int]

Public Instance Methods

base_dir() click to toggle source
# File lib/pep8/plugin.rb, line 86
def base_dir
  @base_dir || "."
end
count_errors(should_fail=false) click to toggle source

Triggers a warning/failure if total lint errors found exceedes @threshold @param [Bool] should_fail

A flag to indicate whether it should warn ou fail the build.
It adds an entry on the corresponding warnings/failures table.

@return [void]

# File lib/pep8/plugin.rb, line 76
def count_errors(should_fail=false)
  ensure_flake8_is_installed

  total_errors = run_flake(:count => true).first.to_i
  if total_errors > threshold
    message = "#{total_errors} PEP 8 issues found"
    should_fail ? fail(message) : warn(message)
  end
end
lint(use_inline_comments=false) click to toggle source

Lint all python files inside a given directory. Defaults to “.” @return [void]

# File lib/pep8/plugin.rb, line 57
def lint(use_inline_comments=false)
  ensure_flake8_is_installed

  errors = run_flake
  return if errors.empty? || errors.count <= threshold

  if use_inline_comments
    comment_inline(errors)
  else
    print_markdown_table(errors)
  end
end
threshold() click to toggle source
# File lib/pep8/plugin.rb, line 90
def threshold
  @threshold || 0
end

Private Instance Methods

comment_inline(errors=[]) click to toggle source
# File lib/pep8/plugin.rb, line 121
def comment_inline(errors=[])
  errors.each do |error|
    file, line, column, reason = error.split(":")
    message(reason.strip.gsub("'", "`"), file: file, line: line.to_i)
  end
end
ensure_flake8_is_installed() click to toggle source
# File lib/pep8/plugin.rb, line 104
def ensure_flake8_is_installed
  system "pip install flake8 --upgrade" unless flake8_installed?
end
flake8_installed?() click to toggle source
# File lib/pep8/plugin.rb, line 108
def flake8_installed?
  `which flake8`.strip.empty? == false
end
print_markdown_table(errors=[]) click to toggle source
run_flake(options = {}) click to toggle source
# File lib/pep8/plugin.rb, line 96
def run_flake(options = {})
  command = "flake8 #{base_dir}"
  command << " --config #{config_file}" if config_file
  # We need quiet flag 2 times to return only the count
  command << " --quiet --quiet --count" if options[:count]
  `#{command}`.split("\n")
end