From 17fed2a15231872b8c47082dada0931d4b72aad9 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 13:55:32 +0200 Subject: [PATCH 1/3] Split the filtering logic out of parse_whitelist_file. This allows parse_whitelist_file to focus more on parsing than filtering. --- lint/lint.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index f3978f3f11efba..9cd8d4d97c085e 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -70,20 +70,30 @@ def parse_whitelist_file(filename): data[file_match][error_type].add(line_number) def inner(path, errors): - whitelisted = [False for item in xrange(len(errors))] + return filter_whitelist_errors(data, path, errors) - for file_match, whitelist_errors in data.iteritems(): - if fnmatch.fnmatch(path, file_match): - for i, (error_type, msg, line) in enumerate(errors): - if "*" in whitelist_errors: + return inner + + +def filter_whitelist_errors(data, path, errors): + """ + Filter out those errors that are whitelisted in `data`. + """ + + whitelisted = [False for item in xrange(len(errors))] + + for file_match, whitelist_errors in data.iteritems(): + if fnmatch.fnmatch(path, file_match): + for i, (error_type, msg, line) in enumerate(errors): + if "*" in whitelist_errors: + whitelisted[i] = True + elif error_type in whitelist_errors: + allowed_lines = whitelist_errors[error_type] + if None in allowed_lines or line in allowed_lines: whitelisted[i] = True - elif error_type in whitelist_errors: - allowed_lines = whitelist_errors[error_type] - if None in allowed_lines or line in allowed_lines: - whitelisted[i] = True - return [item for i, item in enumerate(errors) if not whitelisted[i]] - return inner + return [item for i, item in enumerate(errors) if not whitelisted[i]] + _whitelist_fn = None def whitelist_errors(path, errors): From 8095bbdf6f9039f7b96d37fafabd74d87961940f Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 13:59:15 +0200 Subject: [PATCH 2/3] Return the data from parse_whitelist_file. This allows parse_whitelist_file to focus exclusively on parsing the file. --- lint/lint.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index 9cd8d4d97c085e..b014c64c05ec22 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -53,6 +53,10 @@ def set_type(error_type, errors): return [(error_type,) + error for error in errors] def parse_whitelist_file(filename): + """ + Parse the whitelist file at `filename`, and return the parsed structure. + """ + data = defaultdict(lambda:defaultdict(set)) with open(filename) as f: @@ -69,10 +73,7 @@ def parse_whitelist_file(filename): error_type, file_match, line_number = parts data[file_match][error_type].add(line_number) - def inner(path, errors): - return filter_whitelist_errors(data, path, errors) - - return inner + return data def filter_whitelist_errors(data, path, errors): @@ -100,7 +101,12 @@ def whitelist_errors(path, errors): global _whitelist_fn if _whitelist_fn is None: - _whitelist_fn = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist")) + data = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist")) + + def inner(path, errors): + return filter_whitelist_errors(data, path, errors) + + _whitelist_fn = inner return _whitelist_fn(path, errors) class Regexp(object): From 9a8cbfcb431a2a5a868f9fb8a1a8f142aa487439 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Sun, 8 May 2016 14:08:16 +0200 Subject: [PATCH 3/3] Remove whitelist_errors. This simplifies the code flow, gets rid of a global variable. It might even be slightly faster, as it doesn't need to check the global variable, and call through another closure. --- lint/lint.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/lint/lint.py b/lint/lint.py index b014c64c05ec22..61f900cb980302 100644 --- a/lint/lint.py +++ b/lint/lint.py @@ -95,20 +95,6 @@ def filter_whitelist_errors(data, path, errors): return [item for i, item in enumerate(errors) if not whitelisted[i]] - -_whitelist_fn = None -def whitelist_errors(path, errors): - global _whitelist_fn - - if _whitelist_fn is None: - data = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist")) - - def inner(path, errors): - return filter_whitelist_errors(data, path, errors) - - _whitelist_fn = inner - return _whitelist_fn(path, errors) - class Regexp(object): pattern = None file_extensions = None @@ -281,8 +267,10 @@ def lint(paths): error_count = defaultdict(int) last = None + whitelist = parse_whitelist_file(os.path.join(repo_root, "lint.whitelist")) + def run_lint(path, fn, last, *args): - errors = whitelist_errors(path, fn(path, *args)) + errors = filter_whitelist_errors(whitelist, path, fn(path, *args)) if errors: last = (errors[-1][0], path)