Commits

Anonymous committed 5cc94cb

Add option parsing to setuptools integration

Comments (0)

Files changed (3)

 - Marc Labbé
 - Bruno Miguel Custódio
 - Florent Xicluna
+- Austin Morton
 
 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')
     # 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:
         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):
     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)
     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')