Skip to content

Commit

Permalink
Slowly working through pep8.Checker.check_logical
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmavirus24 committed Mar 3, 2016
1 parent 0c894cc commit 23c9091
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
51 changes: 50 additions & 1 deletion flake8/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

LOG = logging.getLogger(__name__)

SKIP_TOKENS = frozenset([tokenize.NL, tokenize.NEWLINE, tokenize.INDENT,
tokenize.DEDENT])


class Manager(object):
"""Manage the parallelism and checker instances for each plugin and file.
Expand Down Expand Up @@ -216,8 +219,15 @@ def run_check(self, plugin, **arguments):

def run_logical_checks(self):
"""Run all checks expecting a logical line."""
comments, logical_line, mapping = self.processor.build_logical_line()
if not mapping:
return
self.processor.update_state(mapping)

LOG.debug('Logical line: "%s"', logical_line.rstrip())

for plugin in self.checks.logical_line_plugins:
result = self.run_check(plugin) # , logical_line=logical_line)
result = self.run_check(plugin, logical_line=logical_line)
if result is not None:
column_offset, text = result
self.report(
Expand Down Expand Up @@ -415,6 +425,45 @@ def visited_new_blank_line(self):
"""Note that we visited a new blank line."""
self.blank_lines += 1

def build_logical_line_tokens(self):
"""Build the mapping, comments, and logical line lists."""
logical = []
comments = []
length = 0
previous_row = previous_column = mapping = None
for token_type, text, start, end, line in self.tokens:
if token_type in SKIP_TOKENS:
continue
if not mapping:
mapping = [(0, start)]
if token_type == tokenize.COMMENT:
comments.append(text)
continue
if token_type == tokenize.STRING:
text = utils.mutate_string(text)
if previous_row:
(start_row, start_column) = start
if previous_row != start_row:
row_index = previous_row - 1
column_index = previous_column - 1
previous_text = self.lines[row_index][column_index]
if (previous_text == ',' or
(previous_text not in '{[(' and
text not in '}])')):
text = ' ' + text
elif previous_column != start_column:
text = line[previous_column:start_column] + text
logical.append(text)
length += len(text)
mapping.append((length, end))
(previous_row, previous_column) = end
return comments, logical, mapping

def build_logical_line(self):
"""Build a logical line from the current tokens list."""
comments, logical, mapping_list = self.build_logical_line_tokens()
return ''.join(comments), ''.join(logical), mapping_list

def split_line(self, token):
"""Split a physical line's line based on new-lines.
Expand Down
20 changes: 20 additions & 0 deletions flake8/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,23 @@ def count_parentheses(current_parentheses_count, token_text):
return current_parentheses_count + 1
elif token_text in '}])':
return current_parentheses_count - 1


def mutate_string(text):
"""Replace contents with 'xxx' to prevent syntax matching.
>>> mute_string('"abc"')
'"xxx"'
>>> mute_string("'''abc'''")
"'''xxx'''"
>>> mute_string("r'abc'")
"r'xxx'"
"""
# String modifiers (e.g. u or r)
start = text.index(text[-1]) + 1
end = len(text) - 1
# Triple quotes
if text[-3:] in ('"""', "'''"):
start += 2
end -= 2
return text[:start] + 'x' * (end - start) + text[end:]

0 comments on commit 23c9091

Please sign in to comment.