<s> from __future__ import print_function <EOL> import os <EOL> import re <EOL> import shutil <EOL> import sys <EOL> from redbaron import RedBaron <EOL> print ( "<STR_LIT>" , sys . argv [ <NUM_LIT:1> ] , end = "<STR_LIT>" ) <EOL> old_code = open ( sys . argv [ <NUM_LIT:1> ] , '<STR_LIT:r>' ) . read ( ) <EOL> red = RedBaron ( old_code . rstrip ( ) + '<STR_LIT:\n>' ) <EOL> def updateCall ( call_node ) : <EOL> max_len = <NUM_LIT:0> <EOL> for argument in call_node : <EOL> if argument . type == "<STR_LIT>" : <EOL> return <EOL> if hasattr ( argument , "<STR_LIT:target>" ) and argument . target is not None : <EOL> key = argument . target . value <EOL> else : <EOL> key = None <EOL> if key is not None : <EOL> max_len = max ( max_len , len ( key ) ) <EOL> if '<STR_LIT:\n>' not in call_node . second_formatting . dumps ( ) : <EOL> del call_node . second_formatting [ : ] <EOL> del call_node . third_formatting [ : ] <EOL> for argument in call_node : <EOL> if hasattr ( argument , "<STR_LIT:target>" ) and argument . target is not None : <EOL> key = argument . target . value <EOL> else : <EOL> key = None <EOL> if key is not None : <EOL> if not argument . second_formatting : <EOL> argument . second_formatting = '<STR_LIT:U+0020>' <EOL> if '<STR_LIT:\n>' in str ( call_node . second_formatting ) : <EOL> if len ( argument . first_formatting ) > <NUM_LIT:0> : <EOL> spacing = argument . first_formatting [ <NUM_LIT:0> ] . value <EOL> else : <EOL> spacing = "<STR_LIT>" <EOL> if len ( key ) + len ( spacing ) != max_len + <NUM_LIT:1> : <EOL> argument . first_formatting = '<STR_LIT:U+0020>' * ( max_len - len ( key ) + <NUM_LIT:1> ) <EOL> else : <EOL> argument . first_formatting = '<STR_LIT:U+0020>' <EOL> else : <EOL> if '<STR_LIT:\n>' not in str ( call_node . second_formatting ) : <EOL> if argument . value . type in ( "<STR_LIT:string>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> argument . value . second_formatting = "<STR_LIT>" <EOL> def updateTuple ( tuple_node ) : <EOL> if '<STR_LIT:\n>' not in str ( tuple_node . dumps ( ) ) : <EOL> tuple_node . second_formatting = "<STR_LIT>" <EOL> tuple_node . third_formatting = "<STR_LIT>" <EOL> if tuple_node . type == "<STR_LIT>" and tuple_node . with_parenthesis : <EOL> if len ( tuple_node . value . node_list ) > <NUM_LIT:0> : <EOL> tuple_node . value . node_list [ - <NUM_LIT:1> ] . second_formatting = "<STR_LIT>" <EOL> for argument in tuple_node . value : <EOL> if argument . type in ( "<STR_LIT:string>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> argument . second_formatting = "<STR_LIT>" <EOL> def updateString ( string_node ) : <EOL> if not hasattr ( node . parent , "<STR_LIT:type>" ) or string_node . parent . type in ( "<STR_LIT:class>" , "<STR_LIT>" , None ) : <EOL> return <EOL> value = string_node . value <EOL> def isQuotedWith ( quote ) : <EOL> return value . startswith ( quote ) and value . endswith ( quote ) <EOL> for quote in "<STR_LIT>" , '<STR_LIT>' , "<STR_LIT:'>" , '<STR_LIT:">' : <EOL> if isQuotedWith ( quote ) : <EOL> break <EOL> else : <EOL> assert False , value <EOL> real_value = value [ len ( quote ) : - len ( quote ) ] <EOL> assert quote + real_value + quote == value <EOL> if '<STR_LIT:\n>' not in real_value : <EOL> if len ( eval ( value ) ) == <NUM_LIT:1> : <EOL> if real_value != "<STR_LIT:'>" : <EOL> string_node . value = "<STR_LIT:'>" + real_value + "<STR_LIT:'>" <EOL> else : <EOL> if '<STR_LIT:">' not in real_value : <EOL> string_node . value = '<STR_LIT:">' + real_value + '<STR_LIT:">' <EOL> def updateDefNode ( def_node ) : <EOL> def_node . first_formatting = '<STR_LIT:U+0020>' <EOL> def_node . third_formatting = "<STR_LIT>" <EOL> def_node . fourth_formatting = "<STR_LIT>" <EOL> for argument_node in def_node . arguments : <EOL> argument_node . first_formatting = '<STR_LIT:U+0020>' <EOL> argument_node . second_formatting = '<STR_LIT:U+0020>' <EOL> def updateCommentNode ( comment_node ) : <EOL> if "<STR_LIT>" in str ( comment_node . value ) : <EOL> def replacer ( part ) : <EOL> return part . group ( <NUM_LIT:1> ) + '<STR_LIT:U+002C>' . join ( sorted ( part . group ( <NUM_LIT:2> ) . split ( '<STR_LIT:U+002C>' ) ) ) <EOL> new_value = re . sub ( r"<STR_LIT>" , replacer , str ( comment_node . value ) , flags = re . M ) <EOL> comment_node . value = new_value <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateCall ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateTuple ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateTuple ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateString ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateDefNode ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> for node in red . find_all ( "<STR_LIT>" ) : <EOL> try : <EOL> updateCommentNode ( node ) <EOL> except Exception : <EOL> print ( "<STR_LIT>" , node ) <EOL> node . help ( deep = True , with_formatting = True ) <EOL> raise <EOL> if False : <EOL> while len ( red ) > <NUM_LIT:2> and red [ <NUM_LIT:0> ] . type == "<STR_LIT:string>" and red [ <NUM_LIT:1> ] . type == "<STR_LIT>" and red [ <NUM_LIT:2> ] . type == "<STR_LIT>" : <EOL> del red [ <NUM_LIT:1> ] <EOL> new_code = red . dumps ( ) <EOL> if new_code != old_code : <EOL> new_name = sys . argv [ <NUM_LIT:1> ] + "<STR_LIT>" <EOL> with open ( new_name , '<STR_LIT:w>' ) as source_code : <EOL> source_code . write ( red . dumps ( ) ) <EOL> old_stat = os . stat ( sys . argv [ <NUM_LIT:1> ] ) <EOL> try : <EOL> os . rename ( new_name , sys . argv [ <NUM_LIT:1> ] ) <EOL> except OSError : <EOL> shutil . copy ( new_name , sys . argv [ <NUM_LIT:1> ] ) <EOL> os . chmod ( sys . argv [ <NUM_LIT:1> ] , old_stat . st_mode ) <EOL> print ( "<STR_LIT>" ) <EOL> else : <EOL> print ( "<STR_LIT>" ) </s>
