<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import argparse <EOL> import ast <EOL> import cStringIO <EOL> import contextlib <EOL> import re <EOL> import sys <EOL> import textwrap <EOL> import tokenize <EOL> import updatemodcontents <EOL> def main ( ) : <EOL> argparser = argparse . ArgumentParser ( ) <EOL> argparser . add_argument ( <EOL> '<STR_LIT>' , <EOL> type = str , <EOL> nargs = "<STR_LIT:+>" ) <EOL> argparser . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = "<STR_LIT:store_true>" ) <EOL> args = argparser . parse_args ( ) <EOL> block_marker = '<STR_LIT>' + ( '<STR_LIT:=>' * <NUM_LIT> ) <EOL> with contextlib . closing ( cStringIO . StringIO ( ) ) as s : <EOL> print ( block_marker , file = s ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT:U+0020>" * <NUM_LIT> , "<STR_LIT>" , file = s ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT:->" * <NUM_LIT> , file = s ) <EOL> contents_signature = s . getvalue ( ) <EOL> modules = sorted ( args . modules ) <EOL> for m in modules : <EOL> process_module ( m , contents_signature , args , block_marker ) <EOL> def process_module ( m , contents_signature , args , block_marker ) : <EOL> raw = '<STR_LIT>' . join ( open ( m ) ) <EOL> try : <EOL> start_index , end_index = updatemodcontents . find_insertion_point ( <EOL> raw , contents_signature , block_marker , args . force_insert ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" , m ) <EOL> print ( e ) <EOL> raise <EOL> functions = parse_module ( raw ) <EOL> concerns = extract_concerns ( raw ) <EOL> with contextlib . closing ( cStringIO . StringIO ( ) ) as contents : <EOL> contents . write ( contents_signature ) <EOL> print_contents ( contents , functions , concerns ) <EOL> print ( block_marker , file = contents ) <EOL> new_raw = '<STR_LIT>' . join ( [ <EOL> raw [ : start_index ] , <EOL> contents . getvalue ( ) , <EOL> raw [ end_index : ] <EOL> ] ) <EOL> if new_raw != raw : <EOL> with open ( m , '<STR_LIT:w>' ) as f : <EOL> f . write ( new_raw ) <EOL> print ( "<STR_LIT>" , m ) <EOL> return True <EOL> def parse_module ( s ) : <EOL> functions = [ ] <EOL> module = ast . parse ( s ) <EOL> for c in ast . iter_child_nodes ( module ) : <EOL> fields = dict ( ast . iter_fields ( c ) ) <EOL> if isinstance ( c , ast . ClassDef ) : <EOL> name = fields [ "<STR_LIT:name>" ] <EOL> for member in fields [ "<STR_LIT:body>" ] : <EOL> if isinstance ( member , ast . FunctionDef ) : <EOL> member_fields = dict ( ast . iter_fields ( member ) ) <EOL> member_name = member_fields [ '<STR_LIT:name>' ] <EOL> if member_name . startswith ( '<STR_LIT>' ) : <EOL> functions . append ( member_name ) <EOL> elif isinstance ( c , ast . FunctionDef ) : <EOL> name = fields [ "<STR_LIT:name>" ] <EOL> if name . startswith ( '<STR_LIT>' ) : <EOL> functions . append ( name ) <EOL> return functions <EOL> def extract_concerns ( module_text ) : <EOL> concerns = [ ] <EOL> with contextlib . closing ( cStringIO . StringIO ( module_text ) ) as text : <EOL> tokens = tokenize . generate_tokens ( text . readline ) <EOL> function_id = None <EOL> for token in tokens : <EOL> if ( token [ <NUM_LIT:0> ] == tokenize . NAME and <EOL> token [ <NUM_LIT:4> ] . lstrip ( ) . startswith ( '<STR_LIT>' ) and <EOL> token [ <NUM_LIT:1> ] . startswith ( '<STR_LIT>' ) ) : <EOL> function_id = get_function_id ( token [ <NUM_LIT:1> ] ) <EOL> elif function_id and token [ <NUM_LIT:0> ] == tokenize . COMMENT : <EOL> comment = token [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] . lstrip ( ) <EOL> comment_str = None <EOL> if comment . startswith ( '<STR_LIT>' ) : <EOL> comment_str = comment . replace ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:1> ) . lstrip ( ) <EOL> if comment . startswith ( '<STR_LIT:[>' ) : <EOL> comment_str = re . sub ( '<STR_LIT>' , '<STR_LIT>' , comment , <NUM_LIT:1> ) . lstrip ( ) <EOL> if comment_str : <EOL> while True : <EOL> next_token = next ( tokens , None ) <EOL> if next_token and next_token [ <NUM_LIT:0> ] == tokenize . COMMENT : <EOL> next_comment = next_token [ <NUM_LIT:1> ] [ <NUM_LIT:1> : ] . lstrip ( ) <EOL> comment_str += "<STR_LIT:U+0020>" + next_comment <EOL> elif next_token and next_token [ <NUM_LIT:0> ] == tokenize . NL : <EOL> continue <EOL> else : <EOL> break <EOL> concern = "<STR_LIT>" . format ( <EOL> func_id = function_id , comment = comment_str ) <EOL> concerns . append ( concern ) <EOL> return concerns <EOL> def print_contents ( f , functions , concerns ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" , file = f ) <EOL> print ( "<STR_LIT>" , file = f ) <EOL> print ( "<STR_LIT:#>" , file = f ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT>" , file = f ) <EOL> print_concerns ( f , concerns ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT:->" * <NUM_LIT> , file = f ) <EOL> print ( "<STR_LIT:#>" , "<STR_LIT>" , file = f ) <EOL> print_functions ( f , functions ) <EOL> def print_concerns ( f , concerns ) : <EOL> for concern in concerns : <EOL> lines = textwrap . wrap ( concern , <NUM_LIT> ) <EOL> for line in lines : <EOL> print ( "<STR_LIT:#>" , line , file = f ) <EOL> def print_functions ( f , functions ) : <EOL> for function in functions : <EOL> function_id = get_function_id ( function ) <EOL> prefix = generate_bullet_from_id ( function_id ) <EOL> print ( "<STR_LIT:#>" , prefix , function , file = f ) <EOL> def get_function_id ( function ) : <EOL> match = re . search ( '<STR_LIT>' , function ) <EOL> return match . group ( <NUM_LIT:1> ) if match else '<STR_LIT>' <EOL> def generate_bullet_from_id ( id ) : <EOL> return "<STR_LIT>" . format ( id = id ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( ) ) </s>
