Add option parsing to setuptools integration

This commit is contained in:
Austin Morton 2013-04-17 10:33:08 -04:00
parent c6623b37d1
commit 98dbd1606a
3 changed files with 49 additions and 7 deletions

View file

@ -19,3 +19,4 @@ Contributors (by order of appearance) :
- Marc Labbé
- Bruno Miguel Custódio
- Florent Xicluna
- Austin Morton

View file

@ -4,7 +4,8 @@ import sys
import setuptools
from flake8.engine import get_style_guide
from flake8.engine import get_parser, get_style_guide
from flake8.util import is_flag, flag_on
if sys.platform.startswith('win'):
DEFAULT_CONFIG = os.path.expanduser(r'~\.flake8')
@ -28,6 +29,11 @@ def main():
# Run the checkers
report = flake8_style.check_files()
exit_code = print_report(report, flake8_style)
raise SystemExit(exit_code > 0)
def print_report(report, flake8_style):
# Print the final report
options = flake8_style.options
if options.statistics:
@ -38,7 +44,8 @@ def main():
if options.count:
sys.stderr.write(str(report.total_errors) + '\n')
if not options.exit_zero:
raise SystemExit(1)
return 1
return 0
def check_file(path, ignore=(), complexity=-1):
@ -75,22 +82,45 @@ class Flake8Command(setuptools.Command):
user_options = []
def initialize_options(self):
pass
self.option_to_cmds = {}
parser = get_parser()[0]
for opt in parser.option_list:
cmd_name = opt._long_opts[0][2:]
option_name = cmd_name.replace('-', '_')
self.option_to_cmds[option_name] = cmd_name
setattr(self, option_name, None)
def finalize_options(self):
pass
self.options_dict = {}
for (option_name, cmd_name) in self.option_to_cmds.items():
if option_name in ['help', 'verbose']:
continue
value = getattr(self, option_name)
if value is None:
continue
if is_flag(value):
value = flag_on(value)
self.options_dict[option_name] = value
def distribution_files(self):
if self.distribution.packages:
package_dirs = self.distribution.package_dir or {}
for package in self.distribution.packages:
yield package.replace(".", os.path.sep)
pkg_dir = package
if package in package_dirs:
pkg_dir = package_dirs[package]
elif '' in package_dirs:
pkg_dir = package_dirs[''] + os.path.sep + pkg_dir
yield pkg_dir.replace('.', os.path.sep)
if self.distribution.py_modules:
for filename in self.distribution.py_modules:
yield "%s.py" % filename
def run(self):
flake8_style = get_style_guide(config_file=DEFAULT_CONFIG)
flake8_style = get_style_guide(config_file=DEFAULT_CONFIG,
**self.options_dict)
paths = self.distribution_files()
report = flake8_style.check_files(paths)
raise SystemExit(report.total_errors > 0)
exit_code = print_report(report, flake8_style)
raise SystemExit(exit_code > 0)

View file

@ -35,3 +35,14 @@ class OrderedSet(list):
def add(self, value):
if value not in self:
self.append(value)
def is_flag(val):
"""Guess if the value could be an on/off toggle"""
val = str(val)
return val.upper() in ('1', '0', 'F', 'T', 'TRUE', 'FALSE', 'ON', 'OFF')
def flag_on(val):
"""Return true if flag is on"""
return str(val).upper() in ('1', 'T', 'TRUE', 'ON')