<s> from __future__ import print_function <EOL> import sys <EOL> import os <EOL> try : <EOL> from tkinter import * <EOL> except : <EOL> from Tkinter import * <EOL> import argparse <EOL> C_EQUIV_KLOC = { "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT:C>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } <EOL> COMMENT_SYNTAX = { "<STR_LIT>" : ( "<STR_LIT>" , None , None ) , <EOL> "<STR_LIT:C>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:;>" , None , None ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:%>" , None , None ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:C>" , None , None ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:;>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( None , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( None , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:#>" , ( "<STR_LIT>" , "<STR_LIT>" ) , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:#>" , None , None ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:;>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( "<STR_LIT:#>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( None , '<STR_LIT:">' , '<STR_LIT:">' ) <EOL> } <EOL> EXTENSIONS = { "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:C>" , <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>" : "<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>" : "<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>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> SCORE_FILE = os . path . join ( os . getenv ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) <EOL> class LanguageError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> class LevelUpWindow ( Frame ) : <EOL> """<STR_LIT>""" <EOL> def create_widgets ( self , message , button_text ) : <EOL> self . TEXT = Label ( self ) <EOL> self . TEXT [ "<STR_LIT:text>" ] = message <EOL> self . TEXT . pack ( ) <EOL> self . BUTTON = Button ( self ) <EOL> self . BUTTON [ "<STR_LIT:text>" ] = button_text <EOL> self . BUTTON [ "<STR_LIT>" ] = self . quit <EOL> self . BUTTON . pack ( ) <EOL> def __init__ ( self , master , message = "<STR_LIT>" , button_text = "<STR_LIT>" ) : <EOL> Frame . __init__ ( self , master ) <EOL> self . pack ( ) <EOL> self . create_widgets ( message , button_text ) <EOL> def make_level_generator ( ) : <EOL> """<STR_LIT>""" <EOL> xp = <NUM_LIT:0> <EOL> level = <NUM_LIT:1> <EOL> while True : <EOL> yield level , xp <EOL> level += <NUM_LIT:1> <EOL> xp = level * <NUM_LIT:1000> * ( level - <NUM_LIT:1> ) / <NUM_LIT:8> <EOL> def level ( score ) : <EOL> """<STR_LIT>""" <EOL> if score == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> l_gen = make_level_generator ( ) <EOL> while True : <EOL> level , xp = next ( l_gen ) <EOL> if score < xp : <EOL> return level - <NUM_LIT:1> <EOL> def language ( filename ) : <EOL> """<STR_LIT>""" <EOL> name , ext = os . path . splitext ( filename ) <EOL> if ext not in EXTENSIONS : <EOL> raise LanguageError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return EXTENSIONS [ ext ] <EOL> def line_count ( filename ) : <EOL> """<STR_LIT>""" <EOL> lang = language ( filename ) <EOL> comment , begin_block_comment , end_block_comment = COMMENT_SYNTAX [ lang ] <EOL> count = <NUM_LIT:0> <EOL> is_comment = False <EOL> with open ( filename ) as source : <EOL> for line in source : <EOL> line = line . strip ( ) <EOL> if begin_block_comment and line . startswith ( begin_block_comment ) : <EOL> if end_block_comment not in line [ len ( begin_block_comment ) : ] : <EOL> is_comment = True <EOL> elif is_comment and end_block_comment in line : <EOL> is_comment = False <EOL> line = '<STR_LIT>' <EOL> if comment and line . startswith ( comment ) : <EOL> pass <EOL> elif line and not is_comment : <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> def get_line_counts ( root_folder ) : <EOL> """<STR_LIT>""" <EOL> line_counts = { } <EOL> file_list = [ ] <EOL> for root , sub_folders , files in os . walk ( root_folder ) : <EOL> for folder in sub_folders : <EOL> if folder . startswith ( "<STR_LIT:.>" ) : <EOL> sub_folders . remove ( folder ) <EOL> for source_file in files : <EOL> try : <EOL> language ( source_file ) <EOL> except LanguageError : <EOL> continue <EOL> file_path = os . path . join ( root [ len ( root_folder ) : ] , source_file ) <EOL> root_folder_path = os . path . abspath ( root_folder ) <EOL> if os . path . isabs ( file_path ) : <EOL> file_path = file_path [ <NUM_LIT:1> : ] <EOL> file_list . append ( os . path . join ( root_folder_path , file_path ) ) <EOL> for source_file in file_list : <EOL> lang = language ( source_file ) <EOL> line_counts . setdefault ( lang , <NUM_LIT:0> ) <EOL> line_counts [ lang ] += line_count ( source_file ) <EOL> return line_counts <EOL> def get_scores ( root_folder ) : <EOL> """<STR_LIT>""" <EOL> counts = get_line_counts ( root_folder ) <EOL> return { lang : counts [ lang ] / C_EQUIV_KLOC [ lang ] for lang in counts } <EOL> def update_score_file ( line_counts , score_file , head = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> with open ( score_file , '<STR_LIT:w>' ) as sf : <EOL> print ( head , file = sf ) <EOL> for lang in sorted ( line_counts ) : <EOL> print ( lang , line_counts [ lang ] , file = sf ) <EOL> def read_score_file ( score_file ) : <EOL> """<STR_LIT>""" <EOL> line_counts = { } <EOL> with open ( score_file ) as sf : <EOL> root_dir = sf . readline ( ) . strip ( ) <EOL> for line in sf : <EOL> lang , score = line . split ( ) <EOL> score = float ( score ) <EOL> line_counts [ lang ] = score <EOL> return ( root_dir , line_counts ) <EOL> def initialize_score_file ( root_dir , score_file ) : <EOL> """<STR_LIT>""" <EOL> work_dir = os . path . split ( score_file ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( work_dir ) : <EOL> os . makedirs ( work_dir ) <EOL> line_counts = get_line_counts ( root_dir ) <EOL> for lang in line_counts : <EOL> line_counts [ lang ] /= C_EQUIV_KLOC [ lang ] <EOL> update_score_file ( line_counts , score_file , head = os . path . abspath ( root_dir ) ) <EOL> def update_scores ( score_file ) : <EOL> """<STR_LIT>""" <EOL> root_folder , old_scores = read_score_file ( score_file ) <EOL> old_levels = { lang : level ( old_scores [ lang ] ) for lang in old_scores } <EOL> new_scores = get_scores ( root_folder ) <EOL> new_levels = { lang : level ( new_scores [ lang ] ) for lang in new_scores } <EOL> congrats = "<STR_LIT>" <EOL> for lang in sorted ( old_levels ) : <EOL> try : <EOL> if old_levels [ lang ] < new_levels [ lang ] : <EOL> congrats += "<STR_LIT>" . join ( [ "<STR_LIT>" , <EOL> str ( new_levels [ lang ] ) , <EOL> "<STR_LIT>" , lang , "<STR_LIT>" ] ) <EOL> except KeyError : <EOL> pass <EOL> if congrats : <EOL> congrats = "<STR_LIT>" + congrats <EOL> root = Tk ( ) <EOL> window = LevelUpWindow ( root , message = congrats , button_text = "<STR_LIT>" ) <EOL> root . mainloop ( ) <EOL> update_score_file ( new_scores , score_file , head = os . path . abspath ( root_folder ) ) <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , nargs = <NUM_LIT:1> , <EOL> dest = '<STR_LIT>' , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , nargs = <NUM_LIT:1> , <EOL> dest = '<STR_LIT:count>' , <EOL> help = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> if args . count : <EOL> counts = get_line_counts ( args . count [ <NUM_LIT:0> ] ) <EOL> for lang in sorted ( counts ) : <EOL> print ( lang , "<STR_LIT>" , counts [ lang ] ) <EOL> elif args . init : <EOL> initialize_score_file ( args . init [ <NUM_LIT:0> ] , SCORE_FILE ) <EOL> else : <EOL> try : <EOL> root_dir , scores = read_score_file ( SCORE_FILE ) <EOL> except IOError : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> update_scores ( SCORE_FILE ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
