def check(codeString, filename, reporter=modReporter.Default, settings_path=None, **setting_overrides): """Check the Python source given by codeString for unfrosted flakes.""" if not settings_path and filename: settings_path = os.path.dirname(os.path.abspath(filename)) settings_path = settings_path or os.getcwd() active_settings = settings.from_path(settings_path).copy() for key, value in itemsview(setting_overrides): access_key = key.replace('not_', '').lower() if type(active_settings.get(access_key)) in (list, tuple): if key.startswith('not_'): active_settings[access_key] = list(set(active_settings[access_key]).difference(value)) else: active_settings[access_key] = list(set(active_settings[access_key]).union(value)) else: active_settings[key] = value active_settings.update(setting_overrides) if _should_skip(filename, active_settings.get('skip', [])): if active_settings.get('directly_being_checked', None) == 1: reporter.flake(FileSkipped(filename)) return 1 elif active_settings.get('verbose', False): ignore = active_settings.get('ignore_frosted_errors', []) if(not "W200" in ignore and not "W201" in ignore): reporter.flake(FileSkipped(filename, None, verbose=active_settings.get('verbose'))) return 0 # First, compile into an AST and handle syntax errors. try: tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST) except SyntaxError: value = sys.exc_info()[1] msg = value.args[0] (lineno, offset, text) = value.lineno, value.offset, value.text # If there's an encoding problem with the file, the text is None. if text is None: # Avoid using msg, since for the only known case, it contains a # bogus message that claims the encoding the file declared was # unknown. reporter.unexpected_error(filename, 'problem decoding source') else: reporter.flake(PythonSyntaxError(filename, msg, lineno, offset, text, verbose=active_settings.get('verbose'))) return 1 except Exception: reporter.unexpected_error(filename, 'problem decoding source') return 1 # Okay, it's syntactically valid. Now check it. w = checker.Checker(tree, filename, None, ignore_lines=_noqa_lines(codeString), **active_settings) w.messages.sort(key=lambda m: m.lineno) for warning in w.messages: reporter.flake(warning) return len(w.messages)
def check(codeString, filename, reporter=modReporter.Default, settings_path=None, **setting_overrides): """Check the Python source given by codeString for unfrosted flakes.""" active_settings = settings.from_path(settings_path or os.path.dirname(os.path.abspath(filename))).copy() active_settings.update(setting_overrides) if _should_skip(filename, active_settings.get('skip', [])): sys.stderr.write("WARNING: {0} was skipped as it's listed in 'skip' setting\n".format(filename)) return 0 # First, compile into an AST and handle syntax errors. try: tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST) except SyntaxError: value = sys.exc_info()[1] msg = value.args[0] (lineno, offset, text) = value.lineno, value.offset, value.text # If there's an encoding problem with the file, the text is None. if text is None: # Avoid using msg, since for the only known case, it contains a # bogus message that claims the encoding the file declared was # unknown. reporter.unexpected_error(filename, 'problem decoding source') else: reporter.syntax_error(filename, msg, lineno, offset, text) return 1 except Exception: reporter.unexpected_error(filename, 'problem decoding source') return 1 # Okay, it's syntactically valid. Now check it. w = checker.Checker(tree, filename, None, **active_settings) w.messages.sort(key=lambda m: m.lineno) for warning in w.messages: reporter.flake(warning) return len(w.messages)