<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import sys <EOL> class Stats : <EOL> def __init__ ( self ) : <EOL> self . stats = { } <EOL> def statargs ( self , args ) : <EOL> for arg in args : <EOL> if os . path . isdir ( arg ) : <EOL> self . statdir ( arg ) <EOL> elif os . path . isfile ( arg ) : <EOL> self . statfile ( arg ) <EOL> else : <EOL> sys . stderr . write ( "<STR_LIT>" % arg ) <EOL> self . addstats ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> def statdir ( self , dir ) : <EOL> self . addstats ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> try : <EOL> names = sorted ( os . listdir ( dir ) ) <EOL> except os . error as err : <EOL> sys . stderr . write ( "<STR_LIT>" % ( dir , err ) ) <EOL> self . addstats ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return <EOL> for name in names : <EOL> if name . startswith ( "<STR_LIT>" ) : <EOL> continue <EOL> if name . endswith ( "<STR_LIT>" ) : <EOL> continue <EOL> full = os . path . join ( dir , name ) <EOL> if os . path . islink ( full ) : <EOL> self . addstats ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> elif os . path . isdir ( full ) : <EOL> self . statdir ( full ) <EOL> else : <EOL> self . statfile ( full ) <EOL> def statfile ( self , filename ) : <EOL> head , ext = os . path . splitext ( filename ) <EOL> head , base = os . path . split ( filename ) <EOL> if ext == base : <EOL> ext = "<STR_LIT>" <EOL> ext = os . path . normcase ( ext ) <EOL> if not ext : <EOL> ext = "<STR_LIT>" <EOL> self . addstats ( ext , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> try : <EOL> f = open ( filename , "<STR_LIT:rb>" ) <EOL> except IOError as err : <EOL> sys . stderr . write ( "<STR_LIT>" % ( filename , err ) ) <EOL> self . addstats ( ext , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> self . addstats ( ext , "<STR_LIT>" , len ( data ) ) <EOL> if b'<STR_LIT>' in data : <EOL> self . addstats ( ext , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> return <EOL> if not data : <EOL> self . addstats ( ext , "<STR_LIT>" , <NUM_LIT:1> ) <EOL> lines = data . splitlines ( ) <EOL> self . addstats ( ext , "<STR_LIT>" , len ( lines ) ) <EOL> del lines <EOL> words = data . split ( ) <EOL> self . addstats ( ext , "<STR_LIT>" , len ( words ) ) <EOL> def addstats ( self , ext , key , n ) : <EOL> d = self . stats . setdefault ( ext , { } ) <EOL> d [ key ] = d . get ( key , <NUM_LIT:0> ) + n <EOL> def report ( self ) : <EOL> exts = sorted ( self . stats . keys ( ) ) <EOL> columns = { } <EOL> for ext in exts : <EOL> columns . update ( self . stats [ ext ] ) <EOL> cols = sorted ( columns . keys ( ) ) <EOL> colwidth = { } <EOL> colwidth [ "<STR_LIT>" ] = max ( [ len ( ext ) for ext in exts ] ) <EOL> minwidth = <NUM_LIT:6> <EOL> self . stats [ "<STR_LIT>" ] = { } <EOL> for col in cols : <EOL> total = <NUM_LIT:0> <EOL> cw = max ( minwidth , len ( col ) ) <EOL> for ext in exts : <EOL> value = self . stats [ ext ] . get ( col ) <EOL> if value is None : <EOL> w = <NUM_LIT:0> <EOL> else : <EOL> w = len ( "<STR_LIT>" % value ) <EOL> total += value <EOL> cw = max ( cw , w ) <EOL> cw = max ( cw , len ( str ( total ) ) ) <EOL> colwidth [ col ] = cw <EOL> self . stats [ "<STR_LIT>" ] [ col ] = total <EOL> exts . append ( "<STR_LIT>" ) <EOL> for ext in exts : <EOL> self . stats [ ext ] [ "<STR_LIT>" ] = ext <EOL> cols . insert ( <NUM_LIT:0> , "<STR_LIT>" ) <EOL> def printheader ( ) : <EOL> for col in cols : <EOL> print ( "<STR_LIT>" % ( colwidth [ col ] , col ) , end = "<STR_LIT:U+0020>" ) <EOL> print ( ) <EOL> printheader ( ) <EOL> for ext in exts : <EOL> for col in cols : <EOL> value = self . stats [ ext ] . get ( col , "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" % ( colwidth [ col ] , value ) , end = "<STR_LIT:U+0020>" ) <EOL> print ( ) <EOL> printheader ( ) <EOL> def main ( ) : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> if not args : <EOL> args = [ os . curdir ] <EOL> s = Stats ( ) <EOL> s . statargs ( args ) <EOL> s . report ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
