<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import division <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> import bisect <EOL> import codecs <EOL> import collections <EOL> import copy <EOL> import difflib <EOL> import fnmatch <EOL> import inspect <EOL> import io <EOL> import itertools <EOL> import keyword <EOL> import locale <EOL> import os <EOL> import re <EOL> import signal <EOL> import sys <EOL> import token <EOL> import tokenize <EOL> import pep8 <EOL> try : <EOL> unicode <EOL> except NameError : <EOL> unicode = str <EOL> __version__ = '<STR_LIT>' <EOL> CR = '<STR_LIT:\r>' <EOL> LF = '<STR_LIT:\n>' <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> PYTHON_SHEBANG_REGEX = re . compile ( r'<STR_LIT>' ) <EOL> SHORTEN_OPERATOR_GROUPS = frozenset ( [ <EOL> frozenset ( [ '<STR_LIT:U+002C>' ] ) , <EOL> frozenset ( [ '<STR_LIT:%>' ] ) , <EOL> frozenset ( [ '<STR_LIT:U+002C>' , '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' ] ) , <EOL> frozenset ( [ '<STR_LIT:%>' , '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' ] ) , <EOL> frozenset ( [ '<STR_LIT:U+002C>' , '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' , '<STR_LIT:%>' , '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:*>' , '<STR_LIT:/>' , '<STR_LIT>' ] ) , <EOL> frozenset ( [ '<STR_LIT:%>' , '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:*>' , '<STR_LIT:/>' , '<STR_LIT>' ] ) , <EOL> ] ) <EOL> DEFAULT_IGNORE = '<STR_LIT>' <EOL> DEFAULT_INDENT_SIZE = <NUM_LIT:4> <EOL> CODE_TO_2TO3 = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] } <EOL> def open_with_encoding ( filename , encoding = None , mode = '<STR_LIT:r>' ) : <EOL> """<STR_LIT>""" <EOL> if not encoding : <EOL> encoding = detect_encoding ( filename ) <EOL> return io . open ( filename , mode = mode , encoding = encoding , <EOL> newline = '<STR_LIT>' ) <EOL> def detect_encoding ( filename ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT:utf-8>' <EOL> def readlines_from_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> with open_with_encoding ( filename ) as input_file : <EOL> return input_file . readlines ( ) <EOL> def extended_blank_lines ( logical_line , <EOL> blank_lines , <EOL> indent_level , <EOL> previous_logical ) : <EOL> """<STR_LIT>""" <EOL> if previous_logical . startswith ( '<STR_LIT>' ) : <EOL> if ( <EOL> logical_line . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:@>' ) ) or <EOL> pep8 . DOCSTRING_REGEX . match ( logical_line ) <EOL> ) : <EOL> if indent_level and not blank_lines : <EOL> yield ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> elif previous_logical . startswith ( '<STR_LIT>' ) : <EOL> if blank_lines and pep8 . DOCSTRING_REGEX . match ( logical_line ) : <EOL> yield ( <NUM_LIT:0> , '<STR_LIT>' . format ( blank_lines ) ) <EOL> elif pep8 . DOCSTRING_REGEX . match ( previous_logical ) : <EOL> if ( <EOL> indent_level and <EOL> not blank_lines and <EOL> logical_line . startswith ( ( '<STR_LIT>' ) ) and <EOL> '<STR_LIT>' in logical_line <EOL> ) : <EOL> yield ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> pep8 . register_check ( extended_blank_lines ) <EOL> def continued_indentation ( logical_line , tokens , indent_level , indent_char , <EOL> noqa ) : <EOL> """<STR_LIT>""" <EOL> first_row = tokens [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> nrows = <NUM_LIT:1> + tokens [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] - first_row <EOL> if noqa or nrows == <NUM_LIT:1> : <EOL> return <EOL> indent_next = logical_line . endswith ( '<STR_LIT::>' ) <EOL> row = depth = <NUM_LIT:0> <EOL> valid_hangs = ( <EOL> ( DEFAULT_INDENT_SIZE , ) <EOL> if indent_char != '<STR_LIT:\t>' else ( DEFAULT_INDENT_SIZE , <EOL> <NUM_LIT:2> * DEFAULT_INDENT_SIZE ) <EOL> ) <EOL> parens = [ <NUM_LIT:0> ] * nrows <EOL> rel_indent = [ <NUM_LIT:0> ] * nrows <EOL> open_rows = [ [ <NUM_LIT:0> ] ] <EOL> hangs = [ None ] <EOL> indent_chances = { } <EOL> last_indent = tokens [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> indent = [ last_indent [ <NUM_LIT:1> ] ] <EOL> last_token_multiline = None <EOL> line = None <EOL> last_line = '<STR_LIT>' <EOL> last_line_begins_with_multiline = False <EOL> for token_type , text , start , end , line in tokens : <EOL> newline = row < start [ <NUM_LIT:0> ] - first_row <EOL> if newline : <EOL> row = start [ <NUM_LIT:0> ] - first_row <EOL> newline = ( not last_token_multiline and <EOL> token_type not in ( tokenize . NL , tokenize . NEWLINE ) ) <EOL> last_line_begins_with_multiline = last_token_multiline <EOL> if newline : <EOL> last_indent = start <EOL> rel_indent [ row ] = pep8 . expand_indent ( line ) - indent_level <EOL> close_bracket = ( token_type == tokenize . OP and text in '<STR_LIT>' ) <EOL> for open_row in reversed ( open_rows [ depth ] ) : <EOL> hang = rel_indent [ row ] - rel_indent [ open_row ] <EOL> hanging_indent = hang in valid_hangs <EOL> if hanging_indent : <EOL> break <EOL> if hangs [ depth ] : <EOL> hanging_indent = ( hang == hangs [ depth ] ) <EOL> visual_indent = ( not close_bracket and hang > <NUM_LIT:0> and <EOL> indent_chances . get ( start [ <NUM_LIT:1> ] ) ) <EOL> if close_bracket and indent [ depth ] : <EOL> if start [ <NUM_LIT:1> ] != indent [ depth ] : <EOL> yield ( start , '<STR_LIT>' . format ( indent [ depth ] ) ) <EOL> elif close_bracket and not hang : <EOL> pass <EOL> elif indent [ depth ] and start [ <NUM_LIT:1> ] < indent [ depth ] : <EOL> yield ( start , '<STR_LIT>' . format ( indent [ depth ] ) ) <EOL> elif ( hanging_indent or <EOL> ( indent_next and <EOL> rel_indent [ row ] == <NUM_LIT:2> * DEFAULT_INDENT_SIZE ) ) : <EOL> if close_bracket : <EOL> yield ( start , '<STR_LIT>' . format ( indent_level + <EOL> rel_indent [ open_row ] ) ) <EOL> hangs [ depth ] = hang <EOL> elif visual_indent is True : <EOL> indent [ depth ] = start [ <NUM_LIT:1> ] <EOL> elif visual_indent in ( text , unicode ) : <EOL> pass <EOL> else : <EOL> one_indented = ( indent_level + rel_indent [ open_row ] + <EOL> DEFAULT_INDENT_SIZE ) <EOL> if hang <= <NUM_LIT:0> : <EOL> error = ( '<STR_LIT>' , one_indented ) <EOL> elif indent [ depth ] : <EOL> error = ( '<STR_LIT>' , indent [ depth ] ) <EOL> elif hang > DEFAULT_INDENT_SIZE : <EOL> error = ( '<STR_LIT>' , one_indented ) <EOL> else : <EOL> hangs [ depth ] = hang <EOL> error = ( '<STR_LIT>' , one_indented ) <EOL> yield ( start , '<STR_LIT>' . format ( * error ) ) <EOL> if ( parens [ row ] and token_type not in ( tokenize . NL , tokenize . COMMENT ) <EOL> and not indent [ depth ] ) : <EOL> indent [ depth ] = start [ <NUM_LIT:1> ] <EOL> indent_chances [ start [ <NUM_LIT:1> ] ] = True <EOL> elif ( token_type in ( tokenize . STRING , tokenize . COMMENT ) or <EOL> text in ( '<STR_LIT:u>' , '<STR_LIT>' , '<STR_LIT:b>' , '<STR_LIT>' ) ) : <EOL> indent_chances [ start [ <NUM_LIT:1> ] ] = unicode <EOL> elif not indent_chances and not row and not depth and text == '<STR_LIT>' : <EOL> indent_chances [ end [ <NUM_LIT:1> ] + <NUM_LIT:1> ] = True <EOL> elif text == '<STR_LIT::>' and line [ end [ <NUM_LIT:1> ] : ] . isspace ( ) : <EOL> open_rows [ depth ] . append ( row ) <EOL> if token_type == tokenize . OP : <EOL> if text in '<STR_LIT>' : <EOL> depth += <NUM_LIT:1> <EOL> indent . append ( <NUM_LIT:0> ) <EOL> hangs . append ( None ) <EOL> if len ( open_rows ) == depth : <EOL> open_rows . append ( [ ] ) <EOL> open_rows [ depth ] . append ( row ) <EOL> parens [ row ] += <NUM_LIT:1> <EOL> elif text in '<STR_LIT>' and depth > <NUM_LIT:0> : <EOL> prev_indent = indent . pop ( ) or last_indent [ <NUM_LIT:1> ] <EOL> hangs . pop ( ) <EOL> for d in range ( depth ) : <EOL> if indent [ d ] > prev_indent : <EOL> indent [ d ] = <NUM_LIT:0> <EOL> for ind in list ( indent_chances ) : <EOL> if ind >= prev_indent : <EOL> del indent_chances [ ind ] <EOL> del open_rows [ depth + <NUM_LIT:1> : ] <EOL> depth -= <NUM_LIT:1> <EOL> if depth : <EOL> indent_chances [ indent [ depth ] ] = True <EOL> for idx in range ( row , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if parens [ idx ] : <EOL> parens [ idx ] -= <NUM_LIT:1> <EOL> break <EOL> assert len ( indent ) == depth + <NUM_LIT:1> <EOL> if ( <EOL> start [ <NUM_LIT:1> ] not in indent_chances and <EOL> not last_line . rstrip ( ) . endswith ( '<STR_LIT:U+002C>' ) <EOL> ) : <EOL> indent_chances [ start [ <NUM_LIT:1> ] ] = text <EOL> last_token_multiline = ( start [ <NUM_LIT:0> ] != end [ <NUM_LIT:0> ] ) <EOL> if last_token_multiline : <EOL> rel_indent [ end [ <NUM_LIT:0> ] - first_row ] = rel_indent [ row ] <EOL> last_line = line <EOL> if ( <EOL> indent_next and <EOL> not last_line_begins_with_multiline and <EOL> pep8 . expand_indent ( line ) == indent_level + DEFAULT_INDENT_SIZE <EOL> ) : <EOL> pos = ( start [ <NUM_LIT:0> ] , indent [ <NUM_LIT:0> ] + <NUM_LIT:4> ) <EOL> yield ( pos , '<STR_LIT>' . format ( indent_level + <EOL> <NUM_LIT:2> * DEFAULT_INDENT_SIZE ) ) <EOL> del pep8 . _checks [ '<STR_LIT>' ] [ pep8 . continued_indentation ] <EOL> pep8 . register_check ( continued_indentation ) <EOL> class FixPEP8 ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , <EOL> options , <EOL> contents = None , <EOL> long_line_ignore_cache = None ) : <EOL> self . filename = filename <EOL> if contents is None : <EOL> self . source = readlines_from_file ( filename ) <EOL> else : <EOL> sio = io . StringIO ( contents ) <EOL> self . source = sio . readlines ( ) <EOL> self . options = options <EOL> self . indent_word = _get_indentword ( '<STR_LIT>' . join ( self . source ) ) <EOL> self . long_line_ignore_cache = ( <EOL> set ( ) if long_line_ignore_cache is None <EOL> else long_line_ignore_cache ) <EOL> self . fix_e121 = self . _fix_reindent <EOL> self . fix_e122 = self . _fix_reindent <EOL> self . fix_e123 = self . _fix_reindent <EOL> self . fix_e124 = self . _fix_reindent <EOL> self . fix_e126 = self . _fix_reindent <EOL> self . fix_e127 = self . _fix_reindent <EOL> self . fix_e128 = self . _fix_reindent <EOL> self . fix_e129 = self . _fix_reindent <EOL> self . fix_e202 = self . fix_e201 <EOL> self . fix_e203 = self . fix_e201 <EOL> self . fix_e211 = self . fix_e201 <EOL> self . fix_e221 = self . fix_e271 <EOL> self . fix_e222 = self . fix_e271 <EOL> self . fix_e223 = self . fix_e271 <EOL> self . fix_e226 = self . fix_e225 <EOL> self . fix_e227 = self . fix_e225 <EOL> self . fix_e228 = self . fix_e225 <EOL> self . fix_e241 = self . fix_e271 <EOL> self . fix_e242 = self . fix_e224 <EOL> self . fix_e261 = self . fix_e262 <EOL> self . fix_e272 = self . fix_e271 <EOL> self . fix_e273 = self . fix_e271 <EOL> self . fix_e274 = self . fix_e271 <EOL> self . fix_e309 = self . fix_e301 <EOL> self . fix_e501 = ( <EOL> self . fix_long_line_logically if <EOL> options and ( options . aggressive >= <NUM_LIT:2> or options . experimental ) else <EOL> self . fix_long_line_physically ) <EOL> self . fix_e703 = self . fix_e702 <EOL> self . _ws_comma_done = False <EOL> def _fix_source ( self , results ) : <EOL> try : <EOL> ( logical_start , logical_end ) = _find_logical ( self . source ) <EOL> logical_support = True <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> logical_support = False <EOL> completed_lines = set ( ) <EOL> for result in sorted ( results , key = _priority_key ) : <EOL> if result [ '<STR_LIT>' ] in completed_lines : <EOL> continue <EOL> fixed_methodname = '<STR_LIT>' + result [ '<STR_LIT:id>' ] . lower ( ) <EOL> if hasattr ( self , fixed_methodname ) : <EOL> fix = getattr ( self , fixed_methodname ) <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> original_line = self . source [ line_index ] <EOL> is_logical_fix = len ( inspect . getargspec ( fix ) . args ) > <NUM_LIT:2> <EOL> if is_logical_fix : <EOL> logical = None <EOL> if logical_support : <EOL> logical = _get_logical ( self . source , <EOL> result , <EOL> logical_start , <EOL> logical_end ) <EOL> if logical and set ( range ( <EOL> logical [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] + <NUM_LIT:1> , <EOL> logical [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] + <NUM_LIT:1> ) ) . intersection ( <EOL> completed_lines ) : <EOL> continue <EOL> modified_lines = fix ( result , logical ) <EOL> else : <EOL> modified_lines = fix ( result ) <EOL> if modified_lines is None : <EOL> assert not is_logical_fix <EOL> if self . source [ line_index ] == original_line : <EOL> modified_lines = [ ] <EOL> if modified_lines : <EOL> completed_lines . update ( modified_lines ) <EOL> elif modified_lines == [ ] : <EOL> if self . options . verbose >= <NUM_LIT:2> : <EOL> print ( <EOL> '<STR_LIT>' . format ( <EOL> f = result [ '<STR_LIT:id>' ] , l = result [ '<STR_LIT>' ] ) , <EOL> file = sys . stderr ) <EOL> else : <EOL> completed_lines . add ( result [ '<STR_LIT>' ] ) <EOL> else : <EOL> if self . options . verbose >= <NUM_LIT:3> : <EOL> print ( <EOL> "<STR_LIT>" . format ( fixed_methodname ) , <EOL> file = sys . stderr ) <EOL> info = result [ '<STR_LIT:info>' ] . strip ( ) <EOL> print ( '<STR_LIT>' . format ( self . filename , <EOL> result [ '<STR_LIT>' ] , <EOL> result [ '<STR_LIT>' ] , <EOL> info ) , <EOL> file = sys . stderr ) <EOL> def fix ( self ) : <EOL> """<STR_LIT>""" <EOL> pep8_options = { <EOL> '<STR_LIT:ignore>' : self . options . ignore , <EOL> '<STR_LIT>' : self . options . select , <EOL> '<STR_LIT>' : self . options . max_line_length , <EOL> } <EOL> results = _execute_pep8 ( pep8_options , self . source ) <EOL> if self . options . verbose : <EOL> progress = { } <EOL> for r in results : <EOL> if r [ '<STR_LIT:id>' ] not in progress : <EOL> progress [ r [ '<STR_LIT:id>' ] ] = set ( ) <EOL> progress [ r [ '<STR_LIT:id>' ] ] . add ( r [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' . format ( <EOL> n = len ( results ) , progress = progress ) , file = sys . stderr ) <EOL> if self . options . line_range : <EOL> start , end = self . options . line_range <EOL> results = [ r for r in results <EOL> if start <= r [ '<STR_LIT>' ] <= end ] <EOL> self . _fix_source ( filter_results ( source = '<STR_LIT>' . join ( self . source ) , <EOL> results = results , <EOL> aggressive = self . options . aggressive ) ) <EOL> if self . options . line_range : <EOL> count = sum ( sline . count ( '<STR_LIT:\n>' ) <EOL> for sline in self . source [ start - <NUM_LIT:1> : end ] ) <EOL> self . options . line_range [ <NUM_LIT:1> ] = start + count - <NUM_LIT:1> <EOL> return '<STR_LIT>' . join ( self . source ) <EOL> def _fix_reindent ( self , result ) : <EOL> """<STR_LIT>""" <EOL> num_indent_spaces = int ( result [ '<STR_LIT:info>' ] . split ( ) [ <NUM_LIT:1> ] ) <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> self . source [ line_index ] = '<STR_LIT:U+0020>' * num_indent_spaces + target . lstrip ( ) <EOL> def fix_e125 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> num_indent_spaces = int ( result [ '<STR_LIT:info>' ] . split ( ) [ <NUM_LIT:1> ] ) <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> spaces_to_add = num_indent_spaces - len ( _get_indentation ( target ) ) <EOL> indent = len ( _get_indentation ( target ) ) <EOL> modified_lines = [ ] <EOL> while len ( _get_indentation ( self . source [ line_index ] ) ) >= indent : <EOL> self . source [ line_index ] = ( '<STR_LIT:U+0020>' * spaces_to_add + <EOL> self . source [ line_index ] ) <EOL> modified_lines . append ( <NUM_LIT:1> + line_index ) <EOL> line_index -= <NUM_LIT:1> <EOL> return modified_lines <EOL> def fix_e201 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> if offset < <NUM_LIT:0> : <EOL> return [ ] <EOL> if is_probably_part_of_multiline ( target ) : <EOL> return [ ] <EOL> fixed = fix_whitespace ( target , <EOL> offset = offset , <EOL> replacement = '<STR_LIT>' ) <EOL> self . source [ line_index ] = fixed <EOL> def fix_e224 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> target = self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> fixed = target [ : offset ] + target [ offset : ] . replace ( '<STR_LIT:\t>' , '<STR_LIT:U+0020>' ) <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed <EOL> def fix_e225 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> target = self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> fixed = target [ : offset ] + '<STR_LIT:U+0020>' + target [ offset : ] <EOL> if ( <EOL> fixed . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) == target . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) and <EOL> _get_indentation ( fixed ) == _get_indentation ( target ) <EOL> ) : <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed <EOL> else : <EOL> return [ ] <EOL> def fix_e231 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:U+002C>' in result [ '<STR_LIT:info>' ] and not self . _ws_comma_done : <EOL> self . _ws_comma_done = True <EOL> original = '<STR_LIT>' . join ( self . source ) <EOL> new = refactor ( original , [ '<STR_LIT>' ] ) <EOL> if original . strip ( ) != new . strip ( ) : <EOL> self . source = [ new ] <EOL> return range ( <NUM_LIT:1> , <NUM_LIT:1> + len ( original ) ) <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] <EOL> fixed = target [ : offset ] + '<STR_LIT:U+0020>' + target [ offset : ] <EOL> self . source [ line_index ] = fixed <EOL> def fix_e251 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> c = min ( result [ '<STR_LIT>' ] - <NUM_LIT:1> , <EOL> len ( target ) - <NUM_LIT:1> ) <EOL> if target [ c ] . strip ( ) : <EOL> fixed = target <EOL> else : <EOL> fixed = target [ : c ] . rstrip ( ) + target [ c : ] . lstrip ( ) <EOL> if fixed . endswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> self . source [ line_index ] = fixed . rstrip ( '<STR_LIT>' ) <EOL> self . source [ line_index + <NUM_LIT:1> ] = self . source [ line_index + <NUM_LIT:1> ] . lstrip ( ) <EOL> return [ line_index + <NUM_LIT:1> , line_index + <NUM_LIT:2> ] <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed <EOL> def fix_e262 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> target = self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] <EOL> offset = result [ '<STR_LIT>' ] <EOL> code = target [ : offset ] . rstrip ( '<STR_LIT>' ) <EOL> comment = target [ offset : ] . lstrip ( '<STR_LIT>' ) <EOL> fixed = code + ( '<STR_LIT>' + comment if comment . strip ( ) else '<STR_LIT:\n>' ) <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed <EOL> def fix_e271 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> if is_probably_part_of_multiline ( target ) : <EOL> return [ ] <EOL> fixed = fix_whitespace ( target , <EOL> offset = offset , <EOL> replacement = '<STR_LIT:U+0020>' ) <EOL> if fixed == target : <EOL> return [ ] <EOL> else : <EOL> self . source [ line_index ] = fixed <EOL> def fix_e301 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> cr = '<STR_LIT:\n>' <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = cr + self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] <EOL> def fix_e302 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> add_linenum = <NUM_LIT:2> - int ( result [ '<STR_LIT:info>' ] . split ( ) [ - <NUM_LIT:1> ] ) <EOL> cr = '<STR_LIT:\n>' * add_linenum <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = cr + self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] <EOL> def fix_e303 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> delete_linenum = int ( result [ '<STR_LIT:info>' ] . split ( '<STR_LIT:(>' ) [ <NUM_LIT:1> ] . split ( '<STR_LIT:)>' ) [ <NUM_LIT:0> ] ) - <NUM_LIT:2> <EOL> delete_linenum = max ( <NUM_LIT:1> , delete_linenum ) <EOL> cnt = <NUM_LIT:0> <EOL> line = result [ '<STR_LIT>' ] - <NUM_LIT:2> <EOL> modified_lines = [ ] <EOL> while cnt < delete_linenum and line >= <NUM_LIT:0> : <EOL> if not self . source [ line ] . strip ( ) : <EOL> self . source [ line ] = '<STR_LIT>' <EOL> modified_lines . append ( <NUM_LIT:1> + line ) <EOL> cnt += <NUM_LIT:1> <EOL> line -= <NUM_LIT:1> <EOL> return modified_lines <EOL> def fix_e304 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line = result [ '<STR_LIT>' ] - <NUM_LIT:2> <EOL> if not self . source [ line ] . strip ( ) : <EOL> self . source [ line ] = '<STR_LIT>' <EOL> def fix_e401 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> if not target . lstrip ( ) . startswith ( '<STR_LIT>' ) : <EOL> return [ ] <EOL> indentation = re . split ( pattern = r'<STR_LIT>' , <EOL> string = target , maxsplit = <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> fixed = ( target [ : offset ] . rstrip ( '<STR_LIT>' ) + '<STR_LIT:\n>' + <EOL> indentation + '<STR_LIT>' + target [ offset : ] . lstrip ( '<STR_LIT>' ) ) <EOL> self . source [ line_index ] = fixed <EOL> def fix_long_line_logically ( self , result , logical ) : <EOL> """<STR_LIT>""" <EOL> if ( <EOL> not logical or <EOL> len ( logical [ <NUM_LIT:2> ] ) == <NUM_LIT:1> or <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] . lstrip ( ) . startswith ( '<STR_LIT:#>' ) <EOL> ) : <EOL> return self . fix_long_line_physically ( result ) <EOL> start_line_index = logical [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> end_line_index = logical [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> logical_lines = logical [ <NUM_LIT:2> ] <EOL> previous_line = get_item ( self . source , start_line_index - <NUM_LIT:1> , default = '<STR_LIT>' ) <EOL> next_line = get_item ( self . source , end_line_index + <NUM_LIT:1> , default = '<STR_LIT>' ) <EOL> single_line = join_logical_line ( '<STR_LIT>' . join ( logical_lines ) ) <EOL> try : <EOL> fixed = self . fix_long_line ( <EOL> target = single_line , <EOL> previous_line = previous_line , <EOL> next_line = next_line , <EOL> original = '<STR_LIT>' . join ( logical_lines ) ) <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> return self . fix_long_line_physically ( result ) <EOL> if fixed : <EOL> for line_index in range ( start_line_index , end_line_index + <NUM_LIT:1> ) : <EOL> self . source [ line_index ] = '<STR_LIT>' <EOL> self . source [ start_line_index ] = fixed <EOL> return range ( start_line_index + <NUM_LIT:1> , end_line_index + <NUM_LIT:1> ) <EOL> else : <EOL> return [ ] <EOL> def fix_long_line_physically ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> previous_line = get_item ( self . source , line_index - <NUM_LIT:1> , default = '<STR_LIT>' ) <EOL> next_line = get_item ( self . source , line_index + <NUM_LIT:1> , default = '<STR_LIT>' ) <EOL> try : <EOL> fixed = self . fix_long_line ( <EOL> target = target , <EOL> previous_line = previous_line , <EOL> next_line = next_line , <EOL> original = target ) <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> return [ ] <EOL> if fixed : <EOL> self . source [ line_index ] = fixed <EOL> return [ line_index + <NUM_LIT:1> ] <EOL> else : <EOL> return [ ] <EOL> def fix_long_line ( self , target , previous_line , <EOL> next_line , original ) : <EOL> cache_entry = ( target , previous_line , next_line ) <EOL> if cache_entry in self . long_line_ignore_cache : <EOL> return [ ] <EOL> if target . lstrip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> return shorten_comment ( <EOL> line = target , <EOL> max_line_length = self . options . max_line_length , <EOL> last_comment = not next_line . lstrip ( ) . startswith ( '<STR_LIT:#>' ) ) <EOL> fixed = get_fixed_long_line ( <EOL> target = target , <EOL> previous_line = previous_line , <EOL> original = original , <EOL> indent_word = self . indent_word , <EOL> max_line_length = self . options . max_line_length , <EOL> aggressive = self . options . aggressive , <EOL> experimental = self . options . experimental , <EOL> verbose = self . options . verbose ) <EOL> if fixed and not code_almost_equal ( original , fixed ) : <EOL> return fixed <EOL> else : <EOL> self . long_line_ignore_cache . add ( cache_entry ) <EOL> return None <EOL> def fix_e502 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> self . source [ line_index ] = target . rstrip ( '<STR_LIT>' ) + '<STR_LIT:\n>' <EOL> def fix_e701 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> c = result [ '<STR_LIT>' ] <EOL> if line_index > <NUM_LIT:0> and '<STR_LIT:\\>' in self . source [ line_index - <NUM_LIT:1> ] : <EOL> return [ ] <EOL> fixed_source = ( target [ : c ] + '<STR_LIT:\n>' + <EOL> _get_indentation ( target ) + self . indent_word + <EOL> target [ c : ] . lstrip ( '<STR_LIT>' ) ) <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed_source <EOL> return [ result [ '<STR_LIT>' ] , result [ '<STR_LIT>' ] + <NUM_LIT:1> ] <EOL> def fix_e702 ( self , result , logical ) : <EOL> """<STR_LIT>""" <EOL> if not logical : <EOL> return [ ] <EOL> logical_lines = logical [ <NUM_LIT:2> ] <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> if target . rstrip ( ) . endswith ( '<STR_LIT:\\>' ) : <EOL> self . source [ line_index ] = target . rstrip ( '<STR_LIT>' ) <EOL> self . source [ line_index + <NUM_LIT:1> ] = self . source [ line_index + <NUM_LIT:1> ] . lstrip ( ) <EOL> return [ line_index + <NUM_LIT:1> , line_index + <NUM_LIT:2> ] <EOL> if target . rstrip ( ) . endswith ( '<STR_LIT:;>' ) : <EOL> self . source [ line_index ] = target . rstrip ( '<STR_LIT>' ) + '<STR_LIT:\n>' <EOL> return [ line_index + <NUM_LIT:1> ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> first = target [ : offset ] . rstrip ( '<STR_LIT:;>' ) . rstrip ( ) <EOL> second = ( _get_indentation ( logical_lines [ <NUM_LIT:0> ] ) + <EOL> target [ offset : ] . lstrip ( '<STR_LIT:;>' ) . lstrip ( ) ) <EOL> self . source [ line_index ] = first + '<STR_LIT:\n>' + second <EOL> return [ line_index + <NUM_LIT:1> ] <EOL> def fix_e711 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> right_offset = offset + <NUM_LIT:2> <EOL> if right_offset >= len ( target ) : <EOL> return [ ] <EOL> left = target [ : offset ] . rstrip ( ) <EOL> center = target [ offset : right_offset ] <EOL> right = target [ right_offset : ] . lstrip ( ) <EOL> if not right . startswith ( '<STR_LIT:None>' ) : <EOL> return [ ] <EOL> if center . strip ( ) == '<STR_LIT>' : <EOL> new_center = '<STR_LIT>' <EOL> elif center . strip ( ) == '<STR_LIT>' : <EOL> new_center = '<STR_LIT>' <EOL> else : <EOL> return [ ] <EOL> self . source [ line_index ] = '<STR_LIT:U+0020>' . join ( [ left , new_center , right ] ) <EOL> def fix_e712 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> line_index = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> target = self . source [ line_index ] <EOL> offset = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> if re . match ( r'<STR_LIT>' , target ) : <EOL> self . source [ line_index ] = re . sub ( r'<STR_LIT>' , <EOL> r'<STR_LIT>' , target , count = <NUM_LIT:1> ) <EOL> elif re . match ( r'<STR_LIT>' , target ) : <EOL> self . source [ line_index ] = re . sub ( r'<STR_LIT>' , <EOL> r'<STR_LIT>' , target , count = <NUM_LIT:1> ) <EOL> else : <EOL> right_offset = offset + <NUM_LIT:2> <EOL> if right_offset >= len ( target ) : <EOL> return [ ] <EOL> left = target [ : offset ] . rstrip ( ) <EOL> center = target [ offset : right_offset ] <EOL> right = target [ right_offset : ] . lstrip ( ) <EOL> new_right = None <EOL> if center . strip ( ) == '<STR_LIT>' : <EOL> if re . match ( r'<STR_LIT>' , right ) : <EOL> new_right = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , right , count = <NUM_LIT:1> ) <EOL> elif center . strip ( ) == '<STR_LIT>' : <EOL> if re . match ( r'<STR_LIT>' , right ) : <EOL> new_right = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , right , count = <NUM_LIT:1> ) <EOL> if new_right is None : <EOL> return [ ] <EOL> if new_right [ <NUM_LIT:0> ] . isalnum ( ) : <EOL> new_right = '<STR_LIT:U+0020>' + new_right <EOL> self . source [ line_index ] = left + new_right <EOL> def fix_w291 ( self , result ) : <EOL> """<STR_LIT>""" <EOL> fixed_line = self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] . rstrip ( ) <EOL> self . source [ result [ '<STR_LIT>' ] - <NUM_LIT:1> ] = fixed_line + '<STR_LIT:\n>' <EOL> def get_fixed_long_line ( target , previous_line , original , <EOL> indent_word = '<STR_LIT:U+0020>' , max_line_length = <NUM_LIT> , <EOL> aggressive = False , experimental = False , verbose = False ) : <EOL> """<STR_LIT>""" <EOL> indent = _get_indentation ( target ) <EOL> source = target [ len ( indent ) : ] <EOL> assert source . lstrip ( ) == source <EOL> tokens = list ( generate_tokens ( source ) ) <EOL> candidates = shorten_line ( <EOL> tokens , source , indent , <EOL> indent_word , <EOL> max_line_length , <EOL> aggressive = aggressive , <EOL> experimental = experimental , <EOL> previous_line = previous_line ) <EOL> candidates = sorted ( <EOL> sorted ( set ( candidates ) . union ( [ target , original ] ) ) , <EOL> key = lambda x : line_shortening_rank ( x , <EOL> indent_word , <EOL> max_line_length ) ) <EOL> if verbose >= <NUM_LIT:4> : <EOL> print ( ( '<STR_LIT:->' * <NUM_LIT> + '<STR_LIT:\n>' ) . join ( [ '<STR_LIT>' ] + candidates + [ '<STR_LIT>' ] ) , <EOL> file = codecs . getwriter ( '<STR_LIT:utf-8>' ) ( sys . stderr . buffer <EOL> if hasattr ( sys . stderr , <EOL> '<STR_LIT>' ) <EOL> else sys . stderr ) ) <EOL> if candidates : <EOL> return candidates [ <NUM_LIT:0> ] <EOL> def join_logical_line ( logical_line ) : <EOL> """<STR_LIT>""" <EOL> indentation = _get_indentation ( logical_line ) <EOL> return indentation + untokenize_without_newlines ( <EOL> generate_tokens ( logical_line . lstrip ( ) ) ) + '<STR_LIT:\n>' <EOL> def untokenize_without_newlines ( tokens ) : <EOL> """<STR_LIT>""" <EOL> text = '<STR_LIT>' <EOL> last_row = <NUM_LIT:0> <EOL> last_column = - <NUM_LIT:1> <EOL> for t in tokens : <EOL> token_string = t [ <NUM_LIT:1> ] <EOL> ( start_row , start_column ) = t [ <NUM_LIT:2> ] <EOL> ( end_row , end_column ) = t [ <NUM_LIT:3> ] <EOL> if start_row > last_row : <EOL> last_column = <NUM_LIT:0> <EOL> if ( <EOL> ( start_column > last_column or token_string == '<STR_LIT:\n>' ) and <EOL> not text . endswith ( '<STR_LIT:U+0020>' ) <EOL> ) : <EOL> text += '<STR_LIT:U+0020>' <EOL> if token_string != '<STR_LIT:\n>' : <EOL> text += token_string <EOL> last_row = end_row <EOL> last_column = end_column <EOL> return text <EOL> def _find_logical ( source_lines ) : <EOL> logical_start = [ ] <EOL> logical_end = [ ] <EOL> last_newline = True <EOL> parens = <NUM_LIT:0> <EOL> for t in generate_tokens ( '<STR_LIT>' . join ( source_lines ) ) : <EOL> if t [ <NUM_LIT:0> ] in [ tokenize . COMMENT , tokenize . DEDENT , <EOL> tokenize . INDENT , tokenize . NL , <EOL> tokenize . ENDMARKER ] : <EOL> continue <EOL> if not parens and t [ <NUM_LIT:0> ] in [ tokenize . NEWLINE , tokenize . SEMI ] : <EOL> last_newline = True <EOL> logical_end . append ( ( t [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] - <NUM_LIT:1> , t [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) ) <EOL> continue <EOL> if last_newline and not parens : <EOL> logical_start . append ( ( t [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] - <NUM_LIT:1> , t [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) ) <EOL> last_newline = False <EOL> if t [ <NUM_LIT:0> ] == tokenize . OP : <EOL> if t [ <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> parens += <NUM_LIT:1> <EOL> elif t [ <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> parens -= <NUM_LIT:1> <EOL> return ( logical_start , logical_end ) <EOL> def _get_logical ( source_lines , result , logical_start , logical_end ) : <EOL> """<STR_LIT>""" <EOL> row = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> col = result [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> ls = None <EOL> le = None <EOL> for i in range ( <NUM_LIT:0> , len ( logical_start ) , <NUM_LIT:1> ) : <EOL> assert logical_end <EOL> x = logical_end [ i ] <EOL> if x [ <NUM_LIT:0> ] > row or ( x [ <NUM_LIT:0> ] == row and x [ <NUM_LIT:1> ] > col ) : <EOL> le = x <EOL> ls = logical_start [ i ] <EOL> break <EOL> if ls is None : <EOL> return None <EOL> original = source_lines [ ls [ <NUM_LIT:0> ] : le [ <NUM_LIT:0> ] + <NUM_LIT:1> ] <EOL> return ls , le , original <EOL> def get_item ( items , index , default = None ) : <EOL> if <NUM_LIT:0> <= index < len ( items ) : <EOL> return items [ index ] <EOL> else : <EOL> return default <EOL> def reindent ( source , indent_size ) : <EOL> """<STR_LIT>""" <EOL> reindenter = Reindenter ( source ) <EOL> return reindenter . run ( indent_size ) <EOL> def code_almost_equal ( a , b ) : <EOL> """<STR_LIT>""" <EOL> split_a = split_and_strip_non_empty_lines ( a ) <EOL> split_b = split_and_strip_non_empty_lines ( b ) <EOL> if len ( split_a ) != len ( split_b ) : <EOL> return False <EOL> for index in range ( len ( split_a ) ) : <EOL> if '<STR_LIT>' . join ( split_a [ index ] . split ( ) ) != '<STR_LIT>' . join ( split_b [ index ] . split ( ) ) : <EOL> return False <EOL> return True <EOL> def split_and_strip_non_empty_lines ( text ) : <EOL> """<STR_LIT>""" <EOL> return [ line . strip ( ) for line in text . splitlines ( ) if line . strip ( ) ] <EOL> def fix_e265 ( source , aggressive = False ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:#>' not in source : <EOL> return source <EOL> ignored_line_numbers = multiline_string_lines ( <EOL> source , <EOL> include_docstrings = True ) | set ( commented_out_code_lines ( source ) ) <EOL> fixed_lines = [ ] <EOL> sio = io . StringIO ( source ) <EOL> for ( index , line ) in enumerate ( sio . readlines ( ) ) : <EOL> line_number = index + <NUM_LIT:1> <EOL> if ( <EOL> line . lstrip ( ) . startswith ( '<STR_LIT:#>' ) and <EOL> line_number not in ignored_line_numbers <EOL> ) : <EOL> indentation = _get_indentation ( line ) <EOL> line = line . lstrip ( ) <EOL> if len ( line ) > <NUM_LIT:1> : <EOL> if ( <EOL> ( line . count ( '<STR_LIT:#>' ) > <NUM_LIT:1> or line [ <NUM_LIT:1> ] . isalnum ( ) ) <EOL> and not line . rstrip ( ) . endswith ( '<STR_LIT:#>' ) <EOL> ) : <EOL> line = '<STR_LIT>' + line . lstrip ( '<STR_LIT>' ) <EOL> fixed_lines . append ( indentation + line ) <EOL> else : <EOL> fixed_lines . append ( line ) <EOL> return '<STR_LIT>' . join ( fixed_lines ) <EOL> def refactor ( source , fixer_names , ignore = None ) : <EOL> """<STR_LIT>""" <EOL> return source <EOL> def code_to_2to3 ( select , ignore ) : <EOL> fixes = set ( ) <EOL> for code , fix in CODE_TO_2TO3 . items ( ) : <EOL> if code_match ( code , select = select , ignore = ignore ) : <EOL> fixes |= set ( fix ) <EOL> return fixes <EOL> def fix_2to3 ( source , aggressive = True , select = None , ignore = None ) : <EOL> """<STR_LIT>""" <EOL> if not aggressive : <EOL> return source <EOL> select = select or [ ] <EOL> ignore = ignore or [ ] <EOL> return refactor ( source , <EOL> code_to_2to3 ( select = select , <EOL> ignore = ignore ) ) <EOL> def fix_w602 ( source , aggressive = True ) : <EOL> """<STR_LIT>""" <EOL> if not aggressive : <EOL> return source <EOL> return refactor ( source , [ '<STR_LIT>' ] , <EOL> ignore = '<STR_LIT>' ) <EOL> def find_newline ( source ) : <EOL> """<STR_LIT>""" <EOL> assert not isinstance ( source , unicode ) <EOL> counter = collections . defaultdict ( int ) <EOL> for line in source : <EOL> if line . endswith ( CRLF ) : <EOL> counter [ CRLF ] += <NUM_LIT:1> <EOL> elif line . endswith ( CR ) : <EOL> counter [ CR ] += <NUM_LIT:1> <EOL> elif line . endswith ( LF ) : <EOL> counter [ LF ] += <NUM_LIT:1> <EOL> return ( sorted ( counter , key = counter . get , reverse = True ) or [ LF ] ) [ <NUM_LIT:0> ] <EOL> def _get_indentword ( source ) : <EOL> """<STR_LIT>""" <EOL> indent_word = '<STR_LIT:U+0020>' <EOL> try : <EOL> for t in generate_tokens ( source ) : <EOL> if t [ <NUM_LIT:0> ] == token . INDENT : <EOL> indent_word = t [ <NUM_LIT:1> ] <EOL> break <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> pass <EOL> return indent_word <EOL> def _get_indentation ( line ) : <EOL> """<STR_LIT>""" <EOL> if line . strip ( ) : <EOL> non_whitespace_index = len ( line ) - len ( line . lstrip ( ) ) <EOL> return line [ : non_whitespace_index ] <EOL> else : <EOL> return '<STR_LIT>' <EOL> def get_diff_text ( old , new , filename ) : <EOL> """<STR_LIT>""" <EOL> newline = '<STR_LIT:\n>' <EOL> diff = difflib . unified_diff ( <EOL> old , new , <EOL> '<STR_LIT>' + filename , <EOL> '<STR_LIT>' + filename , <EOL> lineterm = newline ) <EOL> text = '<STR_LIT>' <EOL> for line in diff : <EOL> text += line <EOL> if text and not line . endswith ( newline ) : <EOL> text += newline + r'<STR_LIT>' + newline <EOL> return text <EOL> def _priority_key ( pep8_result ) : <EOL> """<STR_LIT>""" <EOL> priority = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> middle_index = <NUM_LIT> <EOL> lowest_priority = [ <EOL> '<STR_LIT>' <EOL> ] <EOL> key = pep8_result [ '<STR_LIT:id>' ] . lower ( ) <EOL> try : <EOL> return priority . index ( key ) <EOL> except ValueError : <EOL> try : <EOL> return middle_index + lowest_priority . index ( key ) + <NUM_LIT:1> <EOL> except ValueError : <EOL> return middle_index <EOL> def shorten_line ( tokens , source , indentation , indent_word , max_line_length , <EOL> aggressive = False , experimental = False , previous_line = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> for candidate in _shorten_line ( tokens = tokens , <EOL> source = source , <EOL> indentation = indentation , <EOL> indent_word = indent_word , <EOL> aggressive = aggressive , <EOL> previous_line = previous_line ) : <EOL> yield candidate <EOL> if aggressive : <EOL> for key_token_strings in SHORTEN_OPERATOR_GROUPS : <EOL> shortened = _shorten_line_at_tokens ( <EOL> tokens = tokens , <EOL> source = source , <EOL> indentation = indentation , <EOL> indent_word = indent_word , <EOL> key_token_strings = key_token_strings , <EOL> aggressive = aggressive ) <EOL> if shortened is not None and shortened != source : <EOL> yield shortened <EOL> if experimental : <EOL> for shortened in _shorten_line_at_tokens_new ( <EOL> tokens = tokens , <EOL> source = source , <EOL> indentation = indentation , <EOL> max_line_length = max_line_length ) : <EOL> yield shortened <EOL> def _shorten_line ( tokens , source , indentation , indent_word , <EOL> aggressive = False , previous_line = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> for ( token_type , <EOL> token_string , <EOL> start_offset , <EOL> end_offset ) in token_offsets ( tokens ) : <EOL> if ( <EOL> token_type == tokenize . COMMENT and <EOL> not is_probably_part_of_multiline ( previous_line ) and <EOL> not is_probably_part_of_multiline ( source ) and <EOL> not source [ start_offset + <NUM_LIT:1> : ] . strip ( ) . lower ( ) . startswith ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> ) : <EOL> first = source [ : start_offset ] <EOL> second = source [ start_offset : ] <EOL> yield ( indentation + second . strip ( ) + '<STR_LIT:\n>' + <EOL> indentation + first . strip ( ) + '<STR_LIT:\n>' ) <EOL> elif token_type == token . OP and token_string != '<STR_LIT:=>' : <EOL> assert token_type != token . INDENT <EOL> first = source [ : end_offset ] <EOL> second_indent = indentation <EOL> if first . rstrip ( ) . endswith ( '<STR_LIT:(>' ) : <EOL> second_indent += indent_word <EOL> elif '<STR_LIT:(>' in first : <EOL> second_indent += '<STR_LIT:U+0020>' * ( <NUM_LIT:1> + first . find ( '<STR_LIT:(>' ) ) <EOL> else : <EOL> second_indent += indent_word <EOL> second = ( second_indent + source [ end_offset : ] . lstrip ( ) ) <EOL> if ( <EOL> not second . strip ( ) or <EOL> second . lstrip ( ) . startswith ( '<STR_LIT:#>' ) <EOL> ) : <EOL> continue <EOL> if second . lstrip ( ) . startswith ( '<STR_LIT:U+002C>' ) : <EOL> continue <EOL> if first . rstrip ( ) . endswith ( '<STR_LIT:.>' ) : <EOL> continue <EOL> if token_string in '<STR_LIT>' : <EOL> fixed = first + '<STR_LIT>' + '<STR_LIT:\n>' + second <EOL> else : <EOL> fixed = first + '<STR_LIT:\n>' + second <EOL> if check_syntax ( normalize_multiline ( fixed ) <EOL> if aggressive else fixed ) : <EOL> yield indentation + fixed <EOL> Token = collections . namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> class ReformattedLines ( object ) : <EOL> """<STR_LIT>""" <EOL> class _Indent ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , indent_amt ) : <EOL> self . _indent_amt = indent_amt <EOL> def emit ( self ) : <EOL> return '<STR_LIT:U+0020>' * self . _indent_amt <EOL> @ property <EOL> def size ( self ) : <EOL> return self . _indent_amt <EOL> class _Space ( object ) : <EOL> """<STR_LIT>""" <EOL> def emit ( self ) : <EOL> return '<STR_LIT:U+0020>' <EOL> @ property <EOL> def size ( self ) : <EOL> return <NUM_LIT:1> <EOL> class _LineBreak ( object ) : <EOL> """<STR_LIT>""" <EOL> def emit ( self ) : <EOL> return '<STR_LIT:\n>' <EOL> @ property <EOL> def size ( self ) : <EOL> return <NUM_LIT:0> <EOL> def __init__ ( self , max_line_length ) : <EOL> self . _max_line_length = max_line_length <EOL> self . _lines = [ ] <EOL> self . _bracket_depth = <NUM_LIT:0> <EOL> self . _prev_item = None <EOL> self . _prev_prev_item = None <EOL> def __repr__ ( self ) : <EOL> return self . emit ( ) <EOL> def add ( self , obj , indent_amt , break_after_open_bracket ) : <EOL> if isinstance ( obj , Atom ) : <EOL> self . _add_item ( obj , indent_amt ) <EOL> return <EOL> self . _add_container ( obj , indent_amt , break_after_open_bracket ) <EOL> def add_comment ( self , item ) : <EOL> self . _lines . append ( self . _Space ( ) ) <EOL> self . _lines . append ( self . _Space ( ) ) <EOL> self . _lines . append ( item ) <EOL> def add_indent ( self , indent_amt ) : <EOL> self . _lines . append ( self . _Indent ( indent_amt ) ) <EOL> def add_line_break ( self , indent ) : <EOL> self . _lines . append ( self . _LineBreak ( ) ) <EOL> self . add_indent ( len ( indent ) ) <EOL> def add_line_break_at ( self , index , indent_amt ) : <EOL> self . _lines . insert ( index , self . _LineBreak ( ) ) <EOL> self . _lines . insert ( index + <NUM_LIT:1> , self . _Indent ( indent_amt ) ) <EOL> def add_space_if_needed ( self , curr_text , equal = False ) : <EOL> if ( <EOL> not self . _lines or isinstance ( <EOL> self . _lines [ - <NUM_LIT:1> ] , ( self . _LineBreak , self . _Indent , self . _Space ) ) <EOL> ) : <EOL> return <EOL> prev_text = unicode ( self . _prev_item ) <EOL> prev_prev_text = ( <EOL> unicode ( self . _prev_prev_item ) if self . _prev_prev_item else '<STR_LIT>' ) <EOL> if ( <EOL> ( ( self . _prev_item . is_keyword or self . _prev_item . is_string or <EOL> self . _prev_item . is_name or self . _prev_item . is_number ) and <EOL> ( curr_text [ <NUM_LIT:0> ] not in '<STR_LIT>' or <EOL> ( curr_text [ <NUM_LIT:0> ] == '<STR_LIT:=>' and equal ) ) ) or <EOL> ( ( prev_prev_text != '<STR_LIT>' and prev_text [ - <NUM_LIT:1> ] != '<STR_LIT:.>' and <EOL> curr_text != '<STR_LIT>' ) and <EOL> curr_text [ <NUM_LIT:0> ] != '<STR_LIT::>' and <EOL> ( ( prev_text [ - <NUM_LIT:1> ] in '<STR_LIT>' and curr_text [ <NUM_LIT:0> ] not in '<STR_LIT>' ) or <EOL> prev_text [ - <NUM_LIT:1> ] in '<STR_LIT>' or <EOL> ( equal and prev_text == '<STR_LIT:=>' ) or <EOL> ( ( self . _prev_prev_item and <EOL> ( prev_text not in '<STR_LIT>' and <EOL> ( self . _prev_prev_item . is_name or <EOL> self . _prev_prev_item . is_number or <EOL> self . _prev_prev_item . is_string ) ) and <EOL> prev_text in ( '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:%>' , '<STR_LIT:*>' , '<STR_LIT:/>' , '<STR_LIT>' , '<STR_LIT>' ) ) ) ) ) <EOL> ) : <EOL> self . _lines . append ( self . _Space ( ) ) <EOL> def previous_item ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _prev_item <EOL> def fits_on_current_line ( self , item_extent ) : <EOL> return self . current_size ( ) + item_extent <= self . _max_line_length <EOL> def current_size ( self ) : <EOL> """<STR_LIT>""" <EOL> size = <NUM_LIT:0> <EOL> for item in reversed ( self . _lines ) : <EOL> size += item . size <EOL> if isinstance ( item , self . _LineBreak ) : <EOL> break <EOL> return size <EOL> def line_empty ( self ) : <EOL> return ( self . _lines and <EOL> isinstance ( self . _lines [ - <NUM_LIT:1> ] , <EOL> ( self . _LineBreak , self . _Indent ) ) ) <EOL> def emit ( self ) : <EOL> string = '<STR_LIT>' <EOL> for item in self . _lines : <EOL> if isinstance ( item , self . _LineBreak ) : <EOL> string = string . rstrip ( ) <EOL> string += item . emit ( ) <EOL> return string . rstrip ( ) + '<STR_LIT:\n>' <EOL> def _add_item ( self , item , indent_amt ) : <EOL> """<STR_LIT>""" <EOL> if self . _prev_item and self . _prev_item . is_string and item . is_string : <EOL> self . _lines . append ( self . _LineBreak ( ) ) <EOL> self . _lines . append ( self . _Indent ( indent_amt ) ) <EOL> item_text = unicode ( item ) <EOL> if self . _lines and self . _bracket_depth : <EOL> self . _prevent_default_initializer_splitting ( item , indent_amt ) <EOL> if item_text in '<STR_LIT>' : <EOL> self . _split_after_delimiter ( item , indent_amt ) <EOL> elif self . _lines and not self . line_empty ( ) : <EOL> if self . fits_on_current_line ( len ( item_text ) ) : <EOL> self . _enforce_space ( item ) <EOL> else : <EOL> self . _lines . append ( self . _LineBreak ( ) ) <EOL> self . _lines . append ( self . _Indent ( indent_amt ) ) <EOL> self . _lines . append ( item ) <EOL> self . _prev_item , self . _prev_prev_item = item , self . _prev_item <EOL> if item_text in '<STR_LIT>' : <EOL> self . _bracket_depth += <NUM_LIT:1> <EOL> elif item_text in '<STR_LIT>' : <EOL> self . _bracket_depth -= <NUM_LIT:1> <EOL> assert self . _bracket_depth >= <NUM_LIT:0> <EOL> def _add_container ( self , container , indent_amt , break_after_open_bracket ) : <EOL> actual_indent = indent_amt + <NUM_LIT:1> <EOL> if ( <EOL> unicode ( self . _prev_item ) != '<STR_LIT:=>' and <EOL> not self . line_empty ( ) and <EOL> not self . fits_on_current_line ( <EOL> container . size + self . _bracket_depth + <NUM_LIT:2> ) <EOL> ) : <EOL> if unicode ( container ) [ <NUM_LIT:0> ] == '<STR_LIT:(>' and self . _prev_item . is_name : <EOL> break_after_open_bracket = True <EOL> actual_indent = indent_amt + <NUM_LIT:4> <EOL> elif ( <EOL> break_after_open_bracket or <EOL> unicode ( self . _prev_item ) not in '<STR_LIT>' <EOL> ) : <EOL> self . _lines . append ( self . _LineBreak ( ) ) <EOL> self . _lines . append ( self . _Indent ( indent_amt ) ) <EOL> break_after_open_bracket = False <EOL> else : <EOL> actual_indent = self . current_size ( ) + <NUM_LIT:1> <EOL> break_after_open_bracket = False <EOL> if isinstance ( container , ( ListComprehension , IfExpression ) ) : <EOL> actual_indent = indent_amt <EOL> container . reflow ( self , '<STR_LIT:U+0020>' * actual_indent , <EOL> break_after_open_bracket = break_after_open_bracket ) <EOL> def _prevent_default_initializer_splitting ( self , item , indent_amt ) : <EOL> """<STR_LIT>""" <EOL> if unicode ( item ) == '<STR_LIT:=>' : <EOL> self . _delete_whitespace ( ) <EOL> return <EOL> if ( not self . _prev_item or not self . _prev_prev_item or <EOL> unicode ( self . _prev_item ) != '<STR_LIT:=>' ) : <EOL> return <EOL> self . _delete_whitespace ( ) <EOL> prev_prev_index = self . _lines . index ( self . _prev_prev_item ) <EOL> if ( <EOL> isinstance ( self . _lines [ prev_prev_index - <NUM_LIT:1> ] , self . _Indent ) or <EOL> self . fits_on_current_line ( item . size + <NUM_LIT:1> ) <EOL> ) : <EOL> return <EOL> if isinstance ( self . _lines [ prev_prev_index - <NUM_LIT:1> ] , self . _Space ) : <EOL> del self . _lines [ prev_prev_index - <NUM_LIT:1> ] <EOL> self . add_line_break_at ( self . _lines . index ( self . _prev_prev_item ) , <EOL> indent_amt ) <EOL> def _split_after_delimiter ( self , item , indent_amt ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_whitespace ( ) <EOL> if self . fits_on_current_line ( item . size ) : <EOL> return <EOL> last_space = None <EOL> for item in reversed ( self . _lines ) : <EOL> if ( <EOL> last_space and <EOL> ( not isinstance ( item , Atom ) or not item . is_colon ) <EOL> ) : <EOL> break <EOL> else : <EOL> last_space = None <EOL> if isinstance ( item , self . _Space ) : <EOL> last_space = item <EOL> if isinstance ( item , ( self . _LineBreak , self . _Indent ) ) : <EOL> return <EOL> if not last_space : <EOL> return <EOL> self . add_line_break_at ( self . _lines . index ( last_space ) , indent_amt ) <EOL> def _enforce_space ( self , item ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . _lines [ - <NUM_LIT:1> ] , <EOL> ( self . _Space , self . _LineBreak , self . _Indent ) ) : <EOL> return <EOL> if not self . _prev_item : <EOL> return <EOL> item_text = unicode ( item ) <EOL> prev_text = unicode ( self . _prev_item ) <EOL> if ( <EOL> ( item_text == '<STR_LIT:.>' and prev_text == '<STR_LIT>' ) or <EOL> ( item_text == '<STR_LIT>' and prev_text == '<STR_LIT:.>' ) or <EOL> ( item_text == '<STR_LIT:(>' and prev_text == '<STR_LIT>' ) <EOL> ) : <EOL> self . _lines . append ( self . _Space ( ) ) <EOL> def _delete_whitespace ( self ) : <EOL> """<STR_LIT>""" <EOL> while isinstance ( self . _lines [ - <NUM_LIT:1> ] , ( self . _Space , self . _LineBreak , <EOL> self . _Indent ) ) : <EOL> del self . _lines [ - <NUM_LIT:1> ] <EOL> class Atom ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , atom ) : <EOL> self . _atom = atom <EOL> def __repr__ ( self ) : <EOL> return self . _atom . token_string <EOL> def __len__ ( self ) : <EOL> return self . size <EOL> def reflow ( <EOL> self , reflowed_lines , continued_indent , extent , <EOL> break_after_open_bracket = False , <EOL> is_list_comp_or_if_expr = False , <EOL> next_is_dot = False <EOL> ) : <EOL> if self . _atom . token_type == tokenize . COMMENT : <EOL> reflowed_lines . add_comment ( self ) <EOL> return <EOL> total_size = extent if extent else self . size <EOL> if self . _atom . token_string not in '<STR_LIT>' : <EOL> total_size += <NUM_LIT:1> <EOL> prev_item = reflowed_lines . previous_item ( ) <EOL> if ( <EOL> not is_list_comp_or_if_expr and <EOL> not reflowed_lines . fits_on_current_line ( total_size ) and <EOL> not ( next_is_dot and <EOL> reflowed_lines . fits_on_current_line ( self . size + <NUM_LIT:1> ) ) and <EOL> not reflowed_lines . line_empty ( ) and <EOL> not self . is_colon and <EOL> not ( prev_item and prev_item . is_name and <EOL> unicode ( self ) == '<STR_LIT:(>' ) <EOL> ) : <EOL> reflowed_lines . add_line_break ( continued_indent ) <EOL> else : <EOL> reflowed_lines . add_space_if_needed ( unicode ( self ) ) <EOL> reflowed_lines . add ( self , len ( continued_indent ) , <EOL> break_after_open_bracket ) <EOL> def emit ( self ) : <EOL> return self . __repr__ ( ) <EOL> @ property <EOL> def is_keyword ( self ) : <EOL> return keyword . iskeyword ( self . _atom . token_string ) <EOL> @ property <EOL> def is_string ( self ) : <EOL> return self . _atom . token_type == tokenize . STRING <EOL> @ property <EOL> def is_name ( self ) : <EOL> return self . _atom . token_type == tokenize . NAME <EOL> @ property <EOL> def is_number ( self ) : <EOL> return self . _atom . token_type == tokenize . NUMBER <EOL> @ property <EOL> def is_comma ( self ) : <EOL> return self . _atom . token_string == '<STR_LIT:U+002C>' <EOL> @ property <EOL> def is_colon ( self ) : <EOL> return self . _atom . token_string == '<STR_LIT::>' <EOL> @ property <EOL> def size ( self ) : <EOL> return len ( self . _atom . token_string ) <EOL> class Container ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , items ) : <EOL> self . _items = items <EOL> def __repr__ ( self ) : <EOL> string = '<STR_LIT>' <EOL> last_was_keyword = False <EOL> for item in self . _items : <EOL> if item . is_comma : <EOL> string += '<STR_LIT:U+002CU+0020>' <EOL> elif item . is_colon : <EOL> string += '<STR_LIT>' <EOL> else : <EOL> item_string = unicode ( item ) <EOL> if ( <EOL> string and <EOL> ( last_was_keyword or <EOL> ( not string . endswith ( tuple ( '<STR_LIT>' ) ) and <EOL> not item_string . startswith ( tuple ( '<STR_LIT>' ) ) ) ) <EOL> ) : <EOL> string += '<STR_LIT:U+0020>' <EOL> string += item_string <EOL> last_was_keyword = item . is_keyword <EOL> return string <EOL> def __iter__ ( self ) : <EOL> for element in self . _items : <EOL> yield element <EOL> def __getitem__ ( self , idx ) : <EOL> return self . _items [ idx ] <EOL> def reflow ( self , reflowed_lines , continued_indent , <EOL> break_after_open_bracket = False ) : <EOL> last_was_container = False <EOL> for ( index , item ) in enumerate ( self . _items ) : <EOL> next_item = get_item ( self . _items , index + <NUM_LIT:1> ) <EOL> if isinstance ( item , Atom ) : <EOL> is_list_comp_or_if_expr = ( <EOL> isinstance ( self , ( ListComprehension , IfExpression ) ) ) <EOL> item . reflow ( reflowed_lines , continued_indent , <EOL> self . _get_extent ( index ) , <EOL> is_list_comp_or_if_expr = is_list_comp_or_if_expr , <EOL> next_is_dot = ( next_item and <EOL> unicode ( next_item ) == '<STR_LIT:.>' ) ) <EOL> if last_was_container and item . is_comma : <EOL> reflowed_lines . add_line_break ( continued_indent ) <EOL> last_was_container = False <EOL> else : <EOL> reflowed_lines . add ( item , len ( continued_indent ) , <EOL> break_after_open_bracket ) <EOL> last_was_container = not isinstance ( item , ( ListComprehension , <EOL> IfExpression ) ) <EOL> if ( <EOL> break_after_open_bracket and index == <NUM_LIT:0> and <EOL> unicode ( item ) == self . open_bracket and <EOL> ( not next_item or unicode ( next_item ) != self . close_bracket ) and <EOL> ( len ( self . _items ) != <NUM_LIT:3> or not isinstance ( next_item , Atom ) ) <EOL> ) : <EOL> reflowed_lines . add_line_break ( continued_indent ) <EOL> break_after_open_bracket = False <EOL> else : <EOL> next_next_item = get_item ( self . _items , index + <NUM_LIT:2> ) <EOL> if ( <EOL> unicode ( item ) not in [ '<STR_LIT:.>' , '<STR_LIT:%>' , '<STR_LIT>' ] and <EOL> next_item and not isinstance ( next_item , Container ) and <EOL> unicode ( next_item ) != '<STR_LIT::>' and <EOL> next_next_item and ( not isinstance ( next_next_item , Atom ) or <EOL> unicode ( next_item ) == '<STR_LIT>' ) and <EOL> not reflowed_lines . line_empty ( ) and <EOL> not reflowed_lines . fits_on_current_line ( <EOL> self . _get_extent ( index + <NUM_LIT:1> ) + <NUM_LIT:2> ) <EOL> ) : <EOL> reflowed_lines . add_line_break ( continued_indent ) <EOL> def _get_extent ( self , index ) : <EOL> """<STR_LIT>""" <EOL> extent = <NUM_LIT:0> <EOL> prev_item = get_item ( self . _items , index - <NUM_LIT:1> ) <EOL> seen_dot = prev_item and unicode ( prev_item ) == '<STR_LIT:.>' <EOL> while index < len ( self . _items ) : <EOL> item = get_item ( self . _items , index ) <EOL> index += <NUM_LIT:1> <EOL> if isinstance ( item , ( ListComprehension , IfExpression ) ) : <EOL> break <EOL> if isinstance ( item , Container ) : <EOL> if prev_item and prev_item . is_name : <EOL> if seen_dot : <EOL> extent += <NUM_LIT:1> <EOL> else : <EOL> extent += item . size <EOL> prev_item = item <EOL> continue <EOL> elif ( unicode ( item ) not in [ '<STR_LIT:.>' , '<STR_LIT:=>' , '<STR_LIT::>' , '<STR_LIT>' ] and <EOL> not item . is_name and not item . is_string ) : <EOL> break <EOL> if unicode ( item ) == '<STR_LIT:.>' : <EOL> seen_dot = True <EOL> extent += item . size <EOL> prev_item = item <EOL> return extent <EOL> @ property <EOL> def is_string ( self ) : <EOL> return False <EOL> @ property <EOL> def size ( self ) : <EOL> return len ( self . __repr__ ( ) ) <EOL> @ property <EOL> def is_keyword ( self ) : <EOL> return False <EOL> @ property <EOL> def is_name ( self ) : <EOL> return False <EOL> @ property <EOL> def is_comma ( self ) : <EOL> return False <EOL> @ property <EOL> def is_colon ( self ) : <EOL> return False <EOL> @ property <EOL> def open_bracket ( self ) : <EOL> return None <EOL> @ property <EOL> def close_bracket ( self ) : <EOL> return None <EOL> class Tuple ( Container ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def open_bracket ( self ) : <EOL> return '<STR_LIT:(>' <EOL> @ property <EOL> def close_bracket ( self ) : <EOL> return '<STR_LIT:)>' <EOL> class List ( Container ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def open_bracket ( self ) : <EOL> return '<STR_LIT:[>' <EOL> @ property <EOL> def close_bracket ( self ) : <EOL> return '<STR_LIT:]>' <EOL> class DictOrSet ( Container ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def open_bracket ( self ) : <EOL> return '<STR_LIT:{>' <EOL> @ property <EOL> def close_bracket ( self ) : <EOL> return '<STR_LIT:}>' <EOL> class ListComprehension ( Container ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def size ( self ) : <EOL> length = <NUM_LIT:0> <EOL> for item in self . _items : <EOL> if isinstance ( item , IfExpression ) : <EOL> break <EOL> length += item . size <EOL> return length <EOL> class IfExpression ( Container ) : <EOL> """<STR_LIT>""" <EOL> def _parse_container ( tokens , index , for_or_if = None ) : <EOL> """<STR_LIT>""" <EOL> items = [ Atom ( Token ( * tokens [ index ] ) ) ] <EOL> index += <NUM_LIT:1> <EOL> num_tokens = len ( tokens ) <EOL> while index < num_tokens : <EOL> tok = Token ( * tokens [ index ] ) <EOL> if tok . token_string in '<STR_LIT>' : <EOL> if for_or_if == '<STR_LIT>' : <EOL> return ( ListComprehension ( items ) , index - <NUM_LIT:1> ) <EOL> elif for_or_if == '<STR_LIT>' : <EOL> return ( IfExpression ( items ) , index - <NUM_LIT:1> ) <EOL> items . append ( Atom ( tok ) ) <EOL> if tok . token_string == '<STR_LIT:)>' : <EOL> return ( Tuple ( items ) , index ) <EOL> elif tok . token_string == '<STR_LIT:]>' : <EOL> return ( List ( items ) , index ) <EOL> elif tok . token_string == '<STR_LIT:}>' : <EOL> return ( DictOrSet ( items ) , index ) <EOL> elif tok . token_string in '<STR_LIT>' : <EOL> ( container , index ) = _parse_container ( tokens , index ) <EOL> items . append ( container ) <EOL> elif tok . token_string == '<STR_LIT>' : <EOL> ( container , index ) = _parse_container ( tokens , index , '<STR_LIT>' ) <EOL> items . append ( container ) <EOL> elif tok . token_string == '<STR_LIT>' : <EOL> ( container , index ) = _parse_container ( tokens , index , '<STR_LIT>' ) <EOL> items . append ( container ) <EOL> else : <EOL> items . append ( Atom ( tok ) ) <EOL> index += <NUM_LIT:1> <EOL> return ( None , None ) <EOL> def _parse_tokens ( tokens ) : <EOL> """<STR_LIT>""" <EOL> index = <NUM_LIT:0> <EOL> parsed_tokens = [ ] <EOL> num_tokens = len ( tokens ) <EOL> while index < num_tokens : <EOL> tok = Token ( * tokens [ index ] ) <EOL> assert tok . token_type != token . INDENT <EOL> if tok . token_type == tokenize . NEWLINE : <EOL> break <EOL> if tok . token_string in '<STR_LIT>' : <EOL> ( container , index ) = _parse_container ( tokens , index ) <EOL> if not container : <EOL> return None <EOL> parsed_tokens . append ( container ) <EOL> else : <EOL> parsed_tokens . append ( Atom ( tok ) ) <EOL> index += <NUM_LIT:1> <EOL> return parsed_tokens <EOL> def _reflow_lines ( parsed_tokens , indentation , max_line_length , <EOL> start_on_prefix_line ) : <EOL> """<STR_LIT>""" <EOL> if unicode ( parsed_tokens [ <NUM_LIT:0> ] ) == '<STR_LIT>' : <EOL> continued_indent = indentation + '<STR_LIT:U+0020>' * <NUM_LIT:2> * DEFAULT_INDENT_SIZE <EOL> else : <EOL> continued_indent = indentation + '<STR_LIT:U+0020>' * DEFAULT_INDENT_SIZE <EOL> break_after_open_bracket = not start_on_prefix_line <EOL> lines = ReformattedLines ( max_line_length ) <EOL> lines . add_indent ( len ( indentation . lstrip ( '<STR_LIT:\r\n>' ) ) ) <EOL> if not start_on_prefix_line : <EOL> first_token = get_item ( parsed_tokens , <NUM_LIT:0> ) <EOL> second_token = get_item ( parsed_tokens , <NUM_LIT:1> ) <EOL> if ( <EOL> first_token and second_token and <EOL> unicode ( second_token ) [ <NUM_LIT:0> ] == '<STR_LIT:(>' and <EOL> len ( indentation ) + len ( first_token ) + <NUM_LIT:1> == len ( continued_indent ) <EOL> ) : <EOL> return None <EOL> for item in parsed_tokens : <EOL> lines . add_space_if_needed ( unicode ( item ) , equal = True ) <EOL> save_continued_indent = continued_indent <EOL> if start_on_prefix_line and isinstance ( item , Container ) : <EOL> start_on_prefix_line = False <EOL> continued_indent = '<STR_LIT:U+0020>' * ( lines . current_size ( ) + <NUM_LIT:1> ) <EOL> item . reflow ( lines , continued_indent , break_after_open_bracket ) <EOL> continued_indent = save_continued_indent <EOL> return lines . emit ( ) <EOL> def _shorten_line_at_tokens_new ( tokens , source , indentation , <EOL> max_line_length ) : <EOL> """<STR_LIT>""" <EOL> yield indentation + source <EOL> parsed_tokens = _parse_tokens ( tokens ) <EOL> if parsed_tokens : <EOL> fixed = _reflow_lines ( parsed_tokens , indentation , max_line_length , <EOL> start_on_prefix_line = True ) <EOL> if fixed and check_syntax ( normalize_multiline ( fixed . lstrip ( ) ) ) : <EOL> yield fixed <EOL> fixed = _reflow_lines ( parsed_tokens , indentation , max_line_length , <EOL> start_on_prefix_line = False ) <EOL> if fixed and check_syntax ( normalize_multiline ( fixed . lstrip ( ) ) ) : <EOL> yield fixed <EOL> def _shorten_line_at_tokens ( tokens , source , indentation , indent_word , <EOL> key_token_strings , aggressive ) : <EOL> """<STR_LIT>""" <EOL> offsets = [ ] <EOL> for ( index , _t ) in enumerate ( token_offsets ( tokens ) ) : <EOL> ( token_type , <EOL> token_string , <EOL> start_offset , <EOL> end_offset ) = _t <EOL> assert token_type != token . INDENT <EOL> if token_string in key_token_strings : <EOL> unwanted_next_token = { <EOL> '<STR_LIT:(>' : '<STR_LIT:)>' , <EOL> '<STR_LIT:[>' : '<STR_LIT:]>' , <EOL> '<STR_LIT:{>' : '<STR_LIT:}>' } . get ( token_string ) <EOL> if unwanted_next_token : <EOL> if ( <EOL> get_item ( tokens , <EOL> index + <NUM_LIT:1> , <EOL> default = [ None , None ] ) [ <NUM_LIT:1> ] == unwanted_next_token or <EOL> get_item ( tokens , <EOL> index + <NUM_LIT:2> , <EOL> default = [ None , None ] ) [ <NUM_LIT:1> ] == unwanted_next_token <EOL> ) : <EOL> continue <EOL> if ( <EOL> index > <NUM_LIT:2> and token_string == '<STR_LIT:(>' and <EOL> tokens [ index - <NUM_LIT:1> ] [ <NUM_LIT:1> ] in '<STR_LIT>' <EOL> ) : <EOL> continue <EOL> if end_offset < len ( source ) - <NUM_LIT:1> : <EOL> offsets . append ( end_offset ) <EOL> else : <EOL> previous_token = get_item ( tokens , index - <NUM_LIT:1> ) <EOL> if ( <EOL> token_type == tokenize . STRING and <EOL> previous_token and previous_token [ <NUM_LIT:0> ] == tokenize . STRING <EOL> ) : <EOL> offsets . append ( start_offset ) <EOL> current_indent = None <EOL> fixed = None <EOL> for line in split_at_offsets ( source , offsets ) : <EOL> if fixed : <EOL> fixed += '<STR_LIT:\n>' + current_indent + line <EOL> for symbol in '<STR_LIT>' : <EOL> if line . endswith ( symbol ) : <EOL> current_indent += indent_word <EOL> else : <EOL> fixed = line <EOL> assert not current_indent <EOL> current_indent = indent_word <EOL> assert fixed is not None <EOL> if check_syntax ( normalize_multiline ( fixed ) <EOL> if aggressive > <NUM_LIT:1> else fixed ) : <EOL> return indentation + fixed <EOL> else : <EOL> return None <EOL> def token_offsets ( tokens ) : <EOL> """<STR_LIT>""" <EOL> end_offset = <NUM_LIT:0> <EOL> previous_end_row = <NUM_LIT:0> <EOL> previous_end_column = <NUM_LIT:0> <EOL> for t in tokens : <EOL> token_type = t [ <NUM_LIT:0> ] <EOL> token_string = t [ <NUM_LIT:1> ] <EOL> ( start_row , start_column ) = t [ <NUM_LIT:2> ] <EOL> ( end_row , end_column ) = t [ <NUM_LIT:3> ] <EOL> end_offset += start_column <EOL> if previous_end_row == start_row : <EOL> end_offset -= previous_end_column <EOL> start_offset = end_offset <EOL> end_offset += len ( token_string ) <EOL> yield ( token_type , <EOL> token_string , <EOL> start_offset , <EOL> end_offset ) <EOL> previous_end_row = end_row <EOL> previous_end_column = end_column <EOL> def normalize_multiline ( line ) : <EOL> """<STR_LIT>""" <EOL> if line . startswith ( '<STR_LIT>' ) and line . rstrip ( ) . endswith ( '<STR_LIT::>' ) : <EOL> return line + '<STR_LIT>' <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> return '<STR_LIT>' + line <EOL> elif line . startswith ( '<STR_LIT:@>' ) : <EOL> return line + '<STR_LIT>' <EOL> elif line . startswith ( '<STR_LIT>' ) : <EOL> return line + '<STR_LIT>' <EOL> else : <EOL> return line <EOL> def fix_whitespace ( line , offset , replacement ) : <EOL> """<STR_LIT>""" <EOL> left = line [ : offset ] . rstrip ( '<STR_LIT>' ) <EOL> right = line [ offset : ] . lstrip ( '<STR_LIT>' ) <EOL> if right . startswith ( '<STR_LIT:#>' ) : <EOL> return line <EOL> else : <EOL> return left + replacement + right <EOL> def _execute_pep8 ( pep8_options , source ) : <EOL> """<STR_LIT>""" <EOL> class QuietReport ( pep8 . BaseReport ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , options ) : <EOL> super ( QuietReport , self ) . __init__ ( options ) <EOL> self . __full_error_results = [ ] <EOL> def error ( self , line_number , offset , text , _ ) : <EOL> """<STR_LIT>""" <EOL> code = super ( QuietReport , self ) . error ( line_number , offset , text , _ ) <EOL> if code : <EOL> self . __full_error_results . append ( <EOL> { '<STR_LIT:id>' : code , <EOL> '<STR_LIT>' : line_number , <EOL> '<STR_LIT>' : offset + <NUM_LIT:1> , <EOL> '<STR_LIT:info>' : text } ) <EOL> def full_error_results ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __full_error_results <EOL> checker = pep8 . Checker ( '<STR_LIT>' , lines = source , <EOL> reporter = QuietReport , ** pep8_options ) <EOL> checker . check_all ( ) <EOL> return checker . report . full_error_results ( ) <EOL> def _remove_leading_and_normalize ( line ) : <EOL> return line . lstrip ( ) . rstrip ( CR + LF ) + '<STR_LIT:\n>' <EOL> class Reindenter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , input_text ) : <EOL> sio = io . StringIO ( input_text ) <EOL> source_lines = sio . readlines ( ) <EOL> self . string_content_line_numbers = multiline_string_lines ( input_text ) <EOL> self . lines = [ ] <EOL> for index , line in enumerate ( source_lines ) : <EOL> line_number = index + <NUM_LIT:1> <EOL> if line_number in self . string_content_line_numbers : <EOL> self . lines . append ( line ) <EOL> else : <EOL> self . lines . append ( _get_indentation ( line ) . expandtabs ( ) + <EOL> _remove_leading_and_normalize ( line ) ) <EOL> self . lines . insert ( <NUM_LIT:0> , None ) <EOL> self . index = <NUM_LIT:1> <EOL> self . input_text = input_text <EOL> def run ( self , indent_size = DEFAULT_INDENT_SIZE ) : <EOL> """<STR_LIT>""" <EOL> if indent_size < <NUM_LIT:1> : <EOL> return self . input_text <EOL> try : <EOL> stats = _reindent_stats ( tokenize . generate_tokens ( self . getline ) ) <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> return self . input_text <EOL> lines = self . lines <EOL> while lines and lines [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' : <EOL> lines . pop ( ) <EOL> stats . append ( ( len ( lines ) , <NUM_LIT:0> ) ) <EOL> have2want = { } <EOL> after = [ ] <EOL> i = stats [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> after . extend ( lines [ <NUM_LIT:1> : i ] ) <EOL> for i in range ( len ( stats ) - <NUM_LIT:1> ) : <EOL> thisstmt , thislevel = stats [ i ] <EOL> nextstmt = stats [ i + <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> have = _leading_space_count ( lines [ thisstmt ] ) <EOL> want = thislevel * indent_size <EOL> if want < <NUM_LIT:0> : <EOL> if have : <EOL> want = have2want . get ( have , - <NUM_LIT:1> ) <EOL> if want < <NUM_LIT:0> : <EOL> for j in range ( i + <NUM_LIT:1> , len ( stats ) - <NUM_LIT:1> ) : <EOL> jline , jlevel = stats [ j ] <EOL> if jlevel >= <NUM_LIT:0> : <EOL> if have == _leading_space_count ( lines [ jline ] ) : <EOL> want = jlevel * indent_size <EOL> break <EOL> if want < <NUM_LIT:0> : <EOL> for j in range ( i - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> jline , jlevel = stats [ j ] <EOL> if jlevel >= <NUM_LIT:0> : <EOL> want = ( have + _leading_space_count ( <EOL> after [ jline - <NUM_LIT:1> ] ) - <EOL> _leading_space_count ( lines [ jline ] ) ) <EOL> break <EOL> if want < <NUM_LIT:0> : <EOL> want = have <EOL> else : <EOL> want = <NUM_LIT:0> <EOL> assert want >= <NUM_LIT:0> <EOL> have2want [ have ] = want <EOL> diff = want - have <EOL> if diff == <NUM_LIT:0> or have == <NUM_LIT:0> : <EOL> after . extend ( lines [ thisstmt : nextstmt ] ) <EOL> else : <EOL> for index , line in enumerate ( lines [ thisstmt : nextstmt ] ) : <EOL> line_number = index + thisstmt <EOL> if line_number in self . string_content_line_numbers : <EOL> after . append ( line ) <EOL> elif diff > <NUM_LIT:0> : <EOL> if line == '<STR_LIT:\n>' : <EOL> after . append ( line ) <EOL> else : <EOL> after . append ( '<STR_LIT:U+0020>' * diff + line ) <EOL> else : <EOL> remove = min ( _leading_space_count ( line ) , - diff ) <EOL> after . append ( line [ remove : ] ) <EOL> return '<STR_LIT>' . join ( after ) <EOL> def getline ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . index >= len ( self . lines ) : <EOL> line = '<STR_LIT>' <EOL> else : <EOL> line = self . lines [ self . index ] <EOL> self . index += <NUM_LIT:1> <EOL> return line <EOL> def _reindent_stats ( tokens ) : <EOL> """<STR_LIT>""" <EOL> find_stmt = <NUM_LIT:1> <EOL> level = <NUM_LIT:0> <EOL> stats = [ ] <EOL> for t in tokens : <EOL> token_type = t [ <NUM_LIT:0> ] <EOL> sline = t [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> line = t [ <NUM_LIT:4> ] <EOL> if token_type == tokenize . NEWLINE : <EOL> find_stmt = <NUM_LIT:1> <EOL> elif token_type == tokenize . INDENT : <EOL> find_stmt = <NUM_LIT:1> <EOL> level += <NUM_LIT:1> <EOL> elif token_type == tokenize . DEDENT : <EOL> find_stmt = <NUM_LIT:1> <EOL> level -= <NUM_LIT:1> <EOL> elif token_type == tokenize . COMMENT : <EOL> if find_stmt : <EOL> stats . append ( ( sline , - <NUM_LIT:1> ) ) <EOL> elif token_type == tokenize . NL : <EOL> pass <EOL> elif find_stmt : <EOL> find_stmt = <NUM_LIT:0> <EOL> if line : <EOL> stats . append ( ( sline , level ) ) <EOL> return stats <EOL> def _leading_space_count ( line ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:0> <EOL> while i < len ( line ) and line [ i ] == '<STR_LIT:U+0020>' : <EOL> i += <NUM_LIT:1> <EOL> return i <EOL> def refactor_with_2to3 ( source_text , fixer_names ) : <EOL> """<STR_LIT>""" <EOL> from lib2to3 . refactor import RefactoringTool <EOL> fixers = [ '<STR_LIT>' + name for name in fixer_names ] <EOL> tool = RefactoringTool ( fixer_names = fixers , explicit = fixers ) <EOL> from lib2to3 . pgen2 import tokenize as lib2to3_tokenize <EOL> try : <EOL> return unicode ( tool . refactor_string ( source_text , name = '<STR_LIT>' ) ) <EOL> except lib2to3_tokenize . TokenError : <EOL> return source_text <EOL> def check_syntax ( code ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return compile ( code , '<STR_LIT>' , '<STR_LIT>' ) <EOL> except ( SyntaxError , TypeError , UnicodeDecodeError ) : <EOL> return False <EOL> def filter_results ( source , results , aggressive ) : <EOL> """<STR_LIT>""" <EOL> non_docstring_string_line_numbers = multiline_string_lines ( <EOL> source , include_docstrings = False ) <EOL> all_string_line_numbers = multiline_string_lines ( <EOL> source , include_docstrings = True ) <EOL> commented_out_code_line_numbers = commented_out_code_lines ( source ) <EOL> for r in results : <EOL> issue_id = r [ '<STR_LIT:id>' ] . lower ( ) <EOL> if r [ '<STR_LIT>' ] in non_docstring_string_line_numbers : <EOL> if issue_id . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> if r [ '<STR_LIT>' ] in all_string_line_numbers : <EOL> if issue_id in [ '<STR_LIT>' ] : <EOL> continue <EOL> if not aggressive and ( r [ '<STR_LIT>' ] + <NUM_LIT:1> ) in all_string_line_numbers : <EOL> if issue_id . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> if aggressive <= <NUM_LIT:0> : <EOL> if issue_id . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> if aggressive <= <NUM_LIT:1> : <EOL> if issue_id . startswith ( ( '<STR_LIT>' , ) ) : <EOL> continue <EOL> if r [ '<STR_LIT>' ] in commented_out_code_line_numbers : <EOL> if issue_id . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> yield r <EOL> def multiline_string_lines ( source , include_docstrings = False ) : <EOL> """<STR_LIT>""" <EOL> line_numbers = set ( ) <EOL> previous_token_type = '<STR_LIT>' <EOL> try : <EOL> for t in generate_tokens ( source ) : <EOL> token_type = t [ <NUM_LIT:0> ] <EOL> start_row = t [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> end_row = t [ <NUM_LIT:3> ] [ <NUM_LIT:0> ] <EOL> if token_type == tokenize . STRING and start_row != end_row : <EOL> if ( <EOL> include_docstrings or <EOL> previous_token_type != tokenize . INDENT <EOL> ) : <EOL> line_numbers |= set ( range ( <NUM_LIT:1> + start_row , <NUM_LIT:1> + end_row ) ) <EOL> previous_token_type = token_type <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> pass <EOL> return line_numbers <EOL> def commented_out_code_lines ( source ) : <EOL> """<STR_LIT>""" <EOL> line_numbers = [ ] <EOL> try : <EOL> for t in generate_tokens ( source ) : <EOL> token_type = t [ <NUM_LIT:0> ] <EOL> token_string = t [ <NUM_LIT:1> ] <EOL> start_row = t [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] <EOL> line = t [ <NUM_LIT:4> ] <EOL> if not line . lstrip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> if token_type == tokenize . COMMENT : <EOL> stripped_line = token_string . lstrip ( '<STR_LIT:#>' ) . strip ( ) <EOL> if ( <EOL> '<STR_LIT:U+0020>' in stripped_line and <EOL> '<STR_LIT:#>' not in stripped_line and <EOL> check_syntax ( stripped_line ) <EOL> ) : <EOL> line_numbers . append ( start_row ) <EOL> except ( SyntaxError , tokenize . TokenError ) : <EOL> pass <EOL> return line_numbers <EOL> def shorten_comment ( line , max_line_length , last_comment = False ) : <EOL> """<STR_LIT>""" <EOL> assert len ( line ) > max_line_length <EOL> line = line . rstrip ( ) <EOL> indentation = _get_indentation ( line ) + '<STR_LIT>' <EOL> max_line_length = min ( max_line_length , <EOL> len ( indentation ) + <NUM_LIT> ) <EOL> MIN_CHARACTER_REPEAT = <NUM_LIT:5> <EOL> if ( <EOL> len ( line ) - len ( line . rstrip ( line [ - <NUM_LIT:1> ] ) ) >= MIN_CHARACTER_REPEAT and <EOL> not line [ - <NUM_LIT:1> ] . isalnum ( ) <EOL> ) : <EOL> return line [ : max_line_length ] + '<STR_LIT:\n>' <EOL> elif last_comment and re . match ( r'<STR_LIT>' , line ) : <EOL> import textwrap <EOL> split_lines = textwrap . wrap ( line . lstrip ( '<STR_LIT>' ) , <EOL> initial_indent = indentation , <EOL> subsequent_indent = indentation , <EOL> width = max_line_length , <EOL> break_long_words = False , <EOL> break_on_hyphens = False ) <EOL> return '<STR_LIT:\n>' . join ( split_lines ) + '<STR_LIT:\n>' <EOL> else : <EOL> return line + '<STR_LIT:\n>' <EOL> def normalize_line_endings ( lines , newline ) : <EOL> """<STR_LIT>""" <EOL> return [ line . rstrip ( '<STR_LIT>' ) + newline for line in lines ] <EOL> def mutual_startswith ( a , b ) : <EOL> return b . startswith ( a ) or a . startswith ( b ) <EOL> def code_match ( code , select , ignore ) : <EOL> if ignore : <EOL> assert not isinstance ( ignore , unicode ) <EOL> for ignored_code in [ c . strip ( ) for c in ignore ] : <EOL> if mutual_startswith ( code . lower ( ) , ignored_code . lower ( ) ) : <EOL> return False <EOL> if select : <EOL> assert not isinstance ( select , unicode ) <EOL> for selected_code in [ c . strip ( ) for c in select ] : <EOL> if mutual_startswith ( code . lower ( ) , selected_code . lower ( ) ) : <EOL> return True <EOL> return False <EOL> return True <EOL> def fix_code ( source , options = None ) : <EOL> """<STR_LIT>""" <EOL> if not options : <EOL> options = parse_args ( [ '<STR_LIT>' ] ) <EOL> if not isinstance ( source , unicode ) : <EOL> source = source . decode ( locale . getpreferredencoding ( False ) ) <EOL> sio = io . StringIO ( source ) <EOL> return fix_lines ( sio . readlines ( ) , options = options ) <EOL> def fix_lines ( source_lines , options , filename = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> original_newline = find_newline ( source_lines ) <EOL> tmp_source = '<STR_LIT>' . join ( normalize_line_endings ( source_lines , '<STR_LIT:\n>' ) ) <EOL> previous_hashes = set ( ) <EOL> if options . line_range : <EOL> fixed_source = apply_local_fixes ( tmp_source , options ) <EOL> else : <EOL> fixed_source = apply_global_fixes ( tmp_source , options ) <EOL> passes = <NUM_LIT:0> <EOL> long_line_ignore_cache = set ( ) <EOL> while hash ( fixed_source ) not in previous_hashes : <EOL> if options . pep8_passes >= <NUM_LIT:0> and passes > options . pep8_passes : <EOL> break <EOL> passes += <NUM_LIT:1> <EOL> previous_hashes . add ( hash ( fixed_source ) ) <EOL> tmp_source = copy . copy ( fixed_source ) <EOL> fix = FixPEP8 ( <EOL> filename , <EOL> options , <EOL> contents = tmp_source , <EOL> long_line_ignore_cache = long_line_ignore_cache ) <EOL> fixed_source = fix . fix ( ) <EOL> sio = io . StringIO ( fixed_source ) <EOL> return '<STR_LIT>' . join ( normalize_line_endings ( sio . readlines ( ) , original_newline ) ) <EOL> def fix_file ( filename , options = None , output = None ) : <EOL> if not options : <EOL> options = parse_args ( [ filename ] ) <EOL> original_source = readlines_from_file ( filename ) <EOL> fixed_source = original_source <EOL> if options . in_place or output : <EOL> encoding = detect_encoding ( filename ) <EOL> if output : <EOL> output = codecs . getwriter ( encoding ) ( output . buffer <EOL> if hasattr ( output , '<STR_LIT>' ) <EOL> else output ) <EOL> output = LineEndingWrapper ( output ) <EOL> fixed_source = fix_lines ( fixed_source , options , filename = filename ) <EOL> if options . diff : <EOL> new = io . StringIO ( fixed_source ) <EOL> new = new . readlines ( ) <EOL> diff = get_diff_text ( original_source , new , filename ) <EOL> if output : <EOL> output . write ( diff ) <EOL> output . flush ( ) <EOL> else : <EOL> return diff <EOL> elif options . in_place : <EOL> fp = open_with_encoding ( filename , encoding = encoding , <EOL> mode = '<STR_LIT:w>' ) <EOL> fp . write ( fixed_source ) <EOL> fp . close ( ) <EOL> else : <EOL> if output : <EOL> output . write ( fixed_source ) <EOL> output . flush ( ) <EOL> else : <EOL> return fixed_source <EOL> def global_fixes ( ) : <EOL> """<STR_LIT>""" <EOL> for function in globals ( ) . values ( ) : <EOL> if inspect . isfunction ( function ) : <EOL> arguments = inspect . getargspec ( function ) [ <NUM_LIT:0> ] <EOL> if arguments [ : <NUM_LIT:1> ] != [ '<STR_LIT:source>' ] : <EOL> continue <EOL> code = extract_code_from_function ( function ) <EOL> if code : <EOL> yield ( code , function ) <EOL> def apply_global_fixes ( source , options , where = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if code_match ( '<STR_LIT>' , select = options . select , ignore = options . ignore ) : <EOL> source = reindent ( source , <EOL> indent_size = options . indent_size ) <EOL> for ( code , function ) in global_fixes ( ) : <EOL> if code_match ( code , select = options . select , ignore = options . ignore ) : <EOL> if options . verbose : <EOL> print ( '<STR_LIT>' . format ( where , <EOL> code . upper ( ) ) , <EOL> file = sys . stderr ) <EOL> source = function ( source , <EOL> aggressive = options . aggressive ) <EOL> source = fix_2to3 ( source , <EOL> aggressive = options . aggressive , <EOL> select = options . select , <EOL> ignore = options . ignore ) <EOL> return source <EOL> def apply_local_fixes ( source , options ) : <EOL> """<STR_LIT>""" <EOL> def find_ge ( a , x ) : <EOL> """<STR_LIT>""" <EOL> i = bisect . bisect_left ( a , x ) <EOL> if i != len ( a ) : <EOL> return i , a [ i ] <EOL> return len ( a ) - <NUM_LIT:1> , a [ - <NUM_LIT:1> ] <EOL> def find_le ( a , x ) : <EOL> """<STR_LIT>""" <EOL> i = bisect . bisect_right ( a , x ) <EOL> if i : <EOL> return i - <NUM_LIT:1> , a [ i - <NUM_LIT:1> ] <EOL> return <NUM_LIT:0> , a [ <NUM_LIT:0> ] <EOL> def local_fix ( source , start_log , end_log , <EOL> start_lines , end_lines , indents , last_line ) : <EOL> """<STR_LIT>""" <EOL> if end_log < start_log : <EOL> return source <EOL> ind = indents [ start_log ] <EOL> indent = _get_indentation ( source [ start_lines [ start_log ] ] ) <EOL> sl = slice ( start_lines [ start_log ] , end_lines [ end_log ] + <NUM_LIT:1> ) <EOL> subsource = source [ sl ] <EOL> if ind : <EOL> for line_no in start_lines [ start_log : end_log + <NUM_LIT:1> ] : <EOL> pos = line_no - start_lines [ start_log ] <EOL> subsource [ pos ] = subsource [ pos ] [ ind : ] <EOL> fixed_subsource = apply_global_fixes ( '<STR_LIT>' . join ( subsource ) , <EOL> options , <EOL> where = '<STR_LIT>' ) <EOL> fixed_subsource = fixed_subsource . splitlines ( True ) <EOL> msl = multiline_string_lines ( '<STR_LIT>' . join ( fixed_subsource ) , <EOL> include_docstrings = False ) <EOL> for i , line in enumerate ( fixed_subsource ) : <EOL> if not i + <NUM_LIT:1> in msl : <EOL> fixed_subsource [ i ] = indent + line if line != '<STR_LIT:\n>' else line <EOL> changed_lines = len ( fixed_subsource ) <EOL> if ( start_lines [ end_log ] != end_lines [ end_log ] <EOL> and end_lines [ end_log ] > last_line ) : <EOL> after_end = end_lines [ end_log ] - last_line <EOL> fixed_subsource = ( fixed_subsource [ : - after_end ] + <EOL> source [ sl ] [ - after_end : ] ) <EOL> changed_lines -= after_end <EOL> options . line_range [ <NUM_LIT:1> ] = ( options . line_range [ <NUM_LIT:0> ] + <EOL> changed_lines - <NUM_LIT:1> ) <EOL> return ( source [ : start_lines [ start_log ] ] + <EOL> fixed_subsource + <EOL> source [ end_lines [ end_log ] + <NUM_LIT:1> : ] ) <EOL> def is_continued_stmt ( line , <EOL> continued_stmts = frozenset ( [ '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] ) ) : <EOL> return re . split ( '<STR_LIT>' , line . strip ( ) , <NUM_LIT:1> ) [ <NUM_LIT:0> ] in continued_stmts <EOL> assert options . line_range <EOL> start , end = options . line_range <EOL> start -= <NUM_LIT:1> <EOL> end -= <NUM_LIT:1> <EOL> last_line = end <EOL> logical = _find_logical ( source ) <EOL> if not logical [ <NUM_LIT:0> ] : <EOL> return apply_global_fixes ( source , options ) <EOL> start_lines , indents = zip ( * logical [ <NUM_LIT:0> ] ) <EOL> end_lines , _ = zip ( * logical [ <NUM_LIT:1> ] ) <EOL> source = source . splitlines ( True ) <EOL> start_log , start = find_ge ( start_lines , start ) <EOL> end_log , end = find_le ( start_lines , end ) <EOL> if ( start_log > <NUM_LIT:0> <EOL> and indents [ start_log - <NUM_LIT:1> ] < indents [ start_log ] <EOL> and not is_continued_stmt ( source [ start_log - <NUM_LIT:1> ] ) ) : <EOL> start_log -= <NUM_LIT:1> <EOL> start = start_lines [ start_log ] <EOL> while start < end : <EOL> if is_continued_stmt ( source [ start ] ) : <EOL> start_log += <NUM_LIT:1> <EOL> start = start_lines [ start_log ] <EOL> continue <EOL> ind = indents [ start_log ] <EOL> for t in itertools . takewhile ( lambda t : t [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] >= ind , <EOL> enumerate ( logical [ <NUM_LIT:0> ] [ start_log : ] ) ) : <EOL> n_log , n = start_log + t [ <NUM_LIT:0> ] , t [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> if n <= end : <EOL> source = local_fix ( source , start_log , n_log , <EOL> start_lines , end_lines , <EOL> indents , last_line ) <EOL> start_log = n_log if n == end else n_log + <NUM_LIT:1> <EOL> start = start_lines [ start_log ] <EOL> continue <EOL> else : <EOL> after_end_log , after_end = find_ge ( start_lines , end + <NUM_LIT:1> ) <EOL> if indents [ after_end_log ] > indents [ start_log ] : <EOL> start_log , start = find_ge ( start_lines , start + <NUM_LIT:1> ) <EOL> continue <EOL> if ( indents [ after_end_log ] == indents [ start_log ] <EOL> and is_continued_stmt ( source [ after_end ] ) ) : <EOL> only_block = True <EOL> for n , n_ind in logical [ <NUM_LIT:0> ] [ start_log : end_log + <NUM_LIT:1> ] [ : : - <NUM_LIT:1> ] : <EOL> if n_ind == ind and not is_continued_stmt ( source [ n ] ) : <EOL> n_log = start_lines . index ( n ) <EOL> source = local_fix ( source , start_log , n_log - <NUM_LIT:1> , <EOL> start_lines , end_lines , <EOL> indents , last_line ) <EOL> start_log = n_log + <NUM_LIT:1> <EOL> start = start_lines [ start_log ] <EOL> only_block = False <EOL> break <EOL> if only_block : <EOL> end_log , end = find_le ( start_lines , end - <NUM_LIT:1> ) <EOL> continue <EOL> source = local_fix ( source , start_log , end_log , <EOL> start_lines , end_lines , <EOL> indents , last_line ) <EOL> break <EOL> return '<STR_LIT>' . join ( source ) <EOL> def extract_code_from_function ( function ) : <EOL> """<STR_LIT>""" <EOL> if not function . __name__ . startswith ( '<STR_LIT>' ) : <EOL> return None <EOL> code = re . sub ( '<STR_LIT>' , '<STR_LIT>' , function . __name__ ) <EOL> if not code : <EOL> return None <EOL> try : <EOL> int ( code [ <NUM_LIT:1> : ] ) <EOL> except ValueError : <EOL> return None <EOL> return code <EOL> def create_parser ( ) : <EOL> """<STR_LIT>""" <EOL> import argparse <EOL> parser = argparse . ArgumentParser ( description = docstring_summary ( __doc__ ) , <EOL> prog = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:version>' , <EOL> version = '<STR_LIT>' + __version__ ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:count>' , dest = '<STR_LIT>' , <EOL> default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = int , metavar = '<STR_LIT:n>' , default = <NUM_LIT:1> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , metavar = '<STR_LIT:n>' , <EOL> default = - <NUM_LIT:1> , type = int , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:count>' , default = <NUM_LIT:0> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' . format ( DEFAULT_IGNORE ) ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , default = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT:n>' , default = <NUM_LIT> , type = int , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , metavar = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = None , type = int , nargs = <NUM_LIT:2> , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = DEFAULT_INDENT_SIZE , <EOL> type = int , metavar = '<STR_LIT:n>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , nargs = '<STR_LIT:*>' , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> def parse_args ( arguments ) : <EOL> """<STR_LIT>""" <EOL> parser = create_parser ( ) <EOL> args = parser . parse_args ( arguments ) <EOL> if not args . files and not args . list_fixes : <EOL> parser . error ( '<STR_LIT>' ) <EOL> args . files = [ decode_filename ( name ) for name in args . files ] <EOL> if '<STR_LIT:->' in args . files : <EOL> if len ( args . files ) > <NUM_LIT:1> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . diff : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . in_place : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . recursive : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if len ( args . files ) > <NUM_LIT:1> and not ( args . in_place or args . diff ) : <EOL> parser . error ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if args . recursive and not ( args . in_place or args . diff ) : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . exclude and not args . recursive : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . in_place and args . diff : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . max_line_length <= <NUM_LIT:0> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> if args . select : <EOL> args . select = args . select . split ( '<STR_LIT:U+002C>' ) <EOL> if args . ignore : <EOL> args . ignore = args . ignore . split ( '<STR_LIT:U+002C>' ) <EOL> elif not args . select : <EOL> if args . aggressive : <EOL> args . select = [ '<STR_LIT:E>' , '<STR_LIT>' ] <EOL> else : <EOL> args . ignore = DEFAULT_IGNORE . split ( '<STR_LIT:U+002C>' ) <EOL> if args . exclude : <EOL> args . exclude = args . exclude . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> args . exclude = [ ] <EOL> if args . jobs < <NUM_LIT:1> : <EOL> import multiprocessing <EOL> args . jobs = multiprocessing . cpu_count ( ) <EOL> if args . jobs > <NUM_LIT:1> and not args . in_place : <EOL> parser . error ( '<STR_LIT>' ) <EOL> return args <EOL> def decode_filename ( filename ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filename , unicode ) : <EOL> return filename <EOL> else : <EOL> return filename . decode ( sys . getfilesystemencoding ( ) ) <EOL> def supported_fixes ( ) : <EOL> """<STR_LIT>""" <EOL> yield ( '<STR_LIT>' , docstring_summary ( reindent . __doc__ ) ) <EOL> instance = FixPEP8 ( filename = None , options = None , contents = '<STR_LIT>' ) <EOL> for attribute in dir ( instance ) : <EOL> code = re . match ( '<STR_LIT>' , attribute ) <EOL> if code : <EOL> yield ( <EOL> code . group ( <NUM_LIT:1> ) . upper ( ) , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , <EOL> docstring_summary ( getattr ( instance , attribute ) . __doc__ ) ) <EOL> ) <EOL> for ( code , function ) in sorted ( global_fixes ( ) ) : <EOL> yield ( code . upper ( ) + ( <NUM_LIT:4> - len ( code ) ) * '<STR_LIT:U+0020>' , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , docstring_summary ( function . __doc__ ) ) ) <EOL> for code in sorted ( CODE_TO_2TO3 ) : <EOL> yield ( code . upper ( ) + ( <NUM_LIT:4> - len ( code ) ) * '<STR_LIT:U+0020>' , <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , docstring_summary ( fix_2to3 . __doc__ ) ) ) <EOL> def docstring_summary ( docstring ) : <EOL> """<STR_LIT>""" <EOL> return docstring . split ( '<STR_LIT:\n>' ) [ <NUM_LIT:0> ] <EOL> def line_shortening_rank ( candidate , indent_word , max_line_length ) : <EOL> """<STR_LIT>""" <EOL> if not candidate . strip ( ) : <EOL> return <NUM_LIT:0> <EOL> rank = <NUM_LIT:0> <EOL> lines = candidate . split ( '<STR_LIT:\n>' ) <EOL> offset = <NUM_LIT:0> <EOL> if ( <EOL> not lines [ <NUM_LIT:0> ] . lstrip ( ) . startswith ( '<STR_LIT:#>' ) and <EOL> lines [ <NUM_LIT:0> ] . rstrip ( ) [ - <NUM_LIT:1> ] not in '<STR_LIT>' <EOL> ) : <EOL> for ( opening , closing ) in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:{}>' ) : <EOL> opening_loc = lines [ <NUM_LIT:0> ] . find ( opening ) <EOL> closing_loc = lines [ <NUM_LIT:0> ] . find ( closing ) <EOL> if opening_loc >= <NUM_LIT:0> : <EOL> if closing_loc < <NUM_LIT:0> or closing_loc != opening_loc + <NUM_LIT:1> : <EOL> offset = max ( offset , <NUM_LIT:1> + opening_loc ) <EOL> current_longest = max ( offset + len ( x . strip ( ) ) for x in lines ) <EOL> rank += <NUM_LIT:4> * max ( <NUM_LIT:0> , current_longest - max_line_length ) <EOL> rank += len ( lines ) <EOL> rank += <NUM_LIT:2> * standard_deviation ( len ( line ) for line in lines ) <EOL> bad_staring_symbol = { <EOL> '<STR_LIT:(>' : '<STR_LIT:)>' , <EOL> '<STR_LIT:[>' : '<STR_LIT:]>' , <EOL> '<STR_LIT:{>' : '<STR_LIT:}>' } . get ( lines [ <NUM_LIT:0> ] [ - <NUM_LIT:1> ] ) <EOL> if len ( lines ) > <NUM_LIT:1> : <EOL> if ( <EOL> bad_staring_symbol and <EOL> lines [ <NUM_LIT:1> ] . lstrip ( ) . startswith ( bad_staring_symbol ) <EOL> ) : <EOL> rank += <NUM_LIT:20> <EOL> for lineno , current_line in enumerate ( lines ) : <EOL> current_line = current_line . strip ( ) <EOL> if current_line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> for bad_start in [ '<STR_LIT:.>' , '<STR_LIT:%>' , '<STR_LIT:+>' , '<STR_LIT:->' , '<STR_LIT:/>' ] : <EOL> if current_line . startswith ( bad_start ) : <EOL> rank += <NUM_LIT:100> <EOL> if current_line == bad_start : <EOL> rank += <NUM_LIT:1000> <EOL> if current_line . endswith ( ( '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' , '<STR_LIT:.>' ) ) : <EOL> if len ( current_line ) <= len ( indent_word ) : <EOL> rank += <NUM_LIT:100> <EOL> if ( <EOL> current_line . endswith ( '<STR_LIT:(>' ) and <EOL> current_line [ : - <NUM_LIT:1> ] . rstrip ( ) . endswith ( '<STR_LIT:U+002C>' ) <EOL> ) : <EOL> rank += <NUM_LIT:100> <EOL> if current_line . endswith ( '<STR_LIT:.>' ) : <EOL> rank += <NUM_LIT:100> <EOL> if has_arithmetic_operator ( current_line ) : <EOL> rank += <NUM_LIT:100> <EOL> if current_line . endswith ( ( '<STR_LIT:%>' , '<STR_LIT:(>' , '<STR_LIT:[>' , '<STR_LIT:{>' ) ) : <EOL> rank -= <NUM_LIT:20> <EOL> if current_line . startswith ( '<STR_LIT>' ) : <EOL> rank -= <NUM_LIT:50> <EOL> if current_line . endswith ( '<STR_LIT:\\>' ) : <EOL> total_len = len ( current_line ) <EOL> lineno += <NUM_LIT:1> <EOL> while lineno < len ( lines ) : <EOL> total_len += len ( lines [ lineno ] ) <EOL> if lines [ lineno ] . lstrip ( ) . startswith ( '<STR_LIT:#>' ) : <EOL> total_len = max_line_length <EOL> break <EOL> if not lines [ lineno ] . endswith ( '<STR_LIT:\\>' ) : <EOL> break <EOL> lineno += <NUM_LIT:1> <EOL> if total_len < max_line_length : <EOL> rank += <NUM_LIT:10> <EOL> else : <EOL> rank += <NUM_LIT:1> <EOL> if '<STR_LIT:U+002C>' in current_line and current_line . endswith ( '<STR_LIT::>' ) : <EOL> rank += <NUM_LIT:10> <EOL> rank += <NUM_LIT:10> * count_unbalanced_brackets ( current_line ) <EOL> return max ( <NUM_LIT:0> , rank ) <EOL> def standard_deviation ( numbers ) : <EOL> """<STR_LIT>""" <EOL> numbers = list ( numbers ) <EOL> if not numbers : <EOL> return <NUM_LIT:0> <EOL> mean = sum ( numbers ) / len ( numbers ) <EOL> return ( sum ( ( n - mean ) ** <NUM_LIT:2> for n in numbers ) / <EOL> len ( numbers ) ) ** <NUM_LIT> <EOL> def has_arithmetic_operator ( line ) : <EOL> """<STR_LIT>""" <EOL> for operator in pep8 . ARITHMETIC_OP : <EOL> if operator in line : <EOL> return True <EOL> return False <EOL> def count_unbalanced_brackets ( line ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for opening , closing in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:{}>' ] : <EOL> count += abs ( line . count ( opening ) - line . count ( closing ) ) <EOL> return count <EOL> def split_at_offsets ( line , offsets ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> previous_offset = <NUM_LIT:0> <EOL> current_offset = <NUM_LIT:0> <EOL> for current_offset in sorted ( offsets ) : <EOL> if current_offset < len ( line ) and previous_offset != current_offset : <EOL> result . append ( line [ previous_offset : current_offset ] . strip ( ) ) <EOL> previous_offset = current_offset <EOL> result . append ( line [ current_offset : ] ) <EOL> return result <EOL> class LineEndingWrapper ( object ) : <EOL> r"""<STR_LIT>""" <EOL> def __init__ ( self , output ) : <EOL> self . __output = output <EOL> def write ( self , s ) : <EOL> self . __output . write ( s . replace ( '<STR_LIT:\r\n>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT:\r>' , '<STR_LIT:\n>' ) ) <EOL> def flush ( self ) : <EOL> self . __output . flush ( ) <EOL> def match_file ( filename , exclude ) : <EOL> """<STR_LIT>""" <EOL> base_name = os . path . basename ( filename ) <EOL> if base_name . startswith ( '<STR_LIT:.>' ) : <EOL> return False <EOL> for pattern in exclude : <EOL> if fnmatch . fnmatch ( base_name , pattern ) : <EOL> return False <EOL> if not os . path . isdir ( filename ) and not is_python_file ( filename ) : <EOL> return False <EOL> return True <EOL> def find_files ( filenames , recursive , exclude ) : <EOL> """<STR_LIT>""" <EOL> while filenames : <EOL> name = filenames . pop ( <NUM_LIT:0> ) <EOL> if recursive and os . path . isdir ( name ) : <EOL> for root , directories , children in os . walk ( name ) : <EOL> filenames += [ os . path . join ( root , f ) for f in children <EOL> if match_file ( os . path . join ( root , f ) , <EOL> exclude ) ] <EOL> directories [ : ] = [ d for d in directories <EOL> if match_file ( os . path . join ( root , d ) , <EOL> exclude ) ] <EOL> else : <EOL> yield name <EOL> def _fix_file ( parameters ) : <EOL> """<STR_LIT>""" <EOL> if parameters [ <NUM_LIT:1> ] . verbose : <EOL> print ( '<STR_LIT>' . format ( parameters [ <NUM_LIT:0> ] ) , file = sys . stderr ) <EOL> try : <EOL> fix_file ( * parameters ) <EOL> except IOError as error : <EOL> print ( unicode ( error ) , file = sys . stderr ) <EOL> def fix_multiple_files ( filenames , options , output = None ) : <EOL> """<STR_LIT>""" <EOL> filenames = find_files ( filenames , options . recursive , options . exclude ) <EOL> if options . jobs > <NUM_LIT:1> : <EOL> import multiprocessing <EOL> pool = multiprocessing . Pool ( options . jobs ) <EOL> pool . map ( _fix_file , <EOL> [ ( name , options ) for name in filenames ] ) <EOL> else : <EOL> for name in filenames : <EOL> _fix_file ( ( name , options , output ) ) <EOL> def is_python_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> return True <EOL> try : <EOL> with open_with_encoding ( filename ) as f : <EOL> first_line = f . readlines ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> except ( IOError , IndexError ) : <EOL> return False <EOL> if not PYTHON_SHEBANG_REGEX . match ( first_line ) : <EOL> return False <EOL> return True <EOL> def is_probably_part_of_multiline ( line ) : <EOL> """<STR_LIT>""" <EOL> return ( <EOL> '<STR_LIT>' in line or <EOL> "<STR_LIT>" in line or <EOL> line . rstrip ( ) . endswith ( '<STR_LIT:\\>' ) <EOL> ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> signal . signal ( signal . SIGPIPE , signal . SIG_DFL ) <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> args = parse_args ( sys . argv [ <NUM_LIT:1> : ] ) <EOL> if args . list_fixes : <EOL> for code , description in sorted ( supported_fixes ( ) ) : <EOL> print ( '<STR_LIT>' . format ( <EOL> code = code , description = description ) ) <EOL> return <NUM_LIT:0> <EOL> if args . files == [ '<STR_LIT:->' ] : <EOL> assert not args . in_place <EOL> sys . stdout . write ( fix_code ( sys . stdin . read ( ) , args ) ) <EOL> else : <EOL> if args . in_place or args . diff : <EOL> args . files = list ( set ( args . files ) ) <EOL> else : <EOL> assert len ( args . files ) == <NUM_LIT:1> <EOL> assert not args . recursive <EOL> fix_multiple_files ( args . files , args , sys . stdout ) <EOL> except KeyboardInterrupt : <EOL> return <NUM_LIT:1> <EOL> class CachedTokenizer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . last_text = None <EOL> self . last_tokens = None <EOL> def generate_tokens ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if text != self . last_text : <EOL> string_io = io . StringIO ( text ) <EOL> self . last_tokens = list ( <EOL> tokenize . generate_tokens ( string_io . readline ) <EOL> ) <EOL> self . last_text = text <EOL> return self . last_tokens <EOL> _cached_tokenizer = CachedTokenizer ( ) <EOL> generate_tokens = _cached_tokenizer . generate_tokens <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
