<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import curses <EOL> import time <EOL> import threading <EOL> import heapq <EOL> import logging <EOL> import codecs <EOL> import locale <EOL> import click <EOL> from anytop import ( <EOL> common , <EOL> accumulate <EOL> ) <EOL> locale . setlocale ( locale . LC_ALL , '<STR_LIT>' ) <EOL> def anytop ( win , istream = sys . stdin , n = None ) : <EOL> "<STR_LIT>" <EOL> if sys . version_info . major == <NUM_LIT:2> : <EOL> istream = codecs . getreader ( '<STR_LIT:utf8>' ) ( istream ) <EOL> common . init_win ( win ) <EOL> if n : <EOL> accumulator = accumulate . WindowAccumulator ( n ) <EOL> else : <EOL> accumulator = accumulate . Accumulator ( ) <EOL> lock = threading . Lock ( ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> ui = AnyTopUI ( win , lock , accumulator ) <EOL> ui . start ( ) <EOL> try : <EOL> for line in common . robust_line_iter ( istream ) : <EOL> if ui . error : <EOL> return ui . error <EOL> logging . debug ( '<STR_LIT>' ) <EOL> lock . acquire ( ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> accumulator . consume ( line ) <EOL> lock . release ( ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> while True : <EOL> time . sleep ( <NUM_LIT> ) <EOL> except KeyboardInterrupt : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> ui . stop ( ) <EOL> ui . join ( ) <EOL> finally : <EOL> ui . stop ( ) <EOL> ui . join ( ) <EOL> return ui . error <EOL> class AnyTopUI ( threading . Thread ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , win , lock , accumulator ) : <EOL> self . win = win <EOL> self . acc = accumulator <EOL> self . lock = lock <EOL> self . error = None <EOL> self . _stop_event = threading . Event ( ) <EOL> super ( AnyTopUI , self ) . __init__ ( ) <EOL> def stop ( self ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . _stop_event . set ( ) <EOL> def stopped ( self ) : <EOL> return self . _stop_event . is_set ( ) <EOL> def run ( self ) : <EOL> try : <EOL> while not self . stopped ( ) : <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . lock . acquire ( ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> dist = self . acc . to_dist ( ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . lock . release ( ) <EOL> self . refresh_display ( dist ) <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> except Exception as e : <EOL> self . lock . release ( ) <EOL> self . error = e <EOL> return <EOL> def refresh_display ( self , d ) : <EOL> "<STR_LIT>" <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . win . erase ( ) <EOL> height , width = self . win . getmaxyx ( ) <EOL> logging . debug ( '<STR_LIT>' % ( width , height ) ) <EOL> n = len ( d ) <EOL> s = sum ( d . values ( ) ) <EOL> largest_keys = heapq . nlargest ( min ( n , height - <NUM_LIT:2> ) , d , key = d . __getitem__ ) <EOL> largest = [ ( d [ l ] , l ) for l in largest_keys ] <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . win . redrawwin ( ) <EOL> self . win . addstr ( <NUM_LIT:0> , <NUM_LIT:0> , '<STR_LIT>' % ( n , s ) ) <EOL> if largest : <EOL> w = max ( <NUM_LIT:6> , len ( str ( max ( c for ( c , k ) in largest ) ) ) ) <EOL> k_len = width - w - <NUM_LIT:4> <EOL> template = u'<STR_LIT>' % w <EOL> for i , ( c , k ) in enumerate ( largest ) : <EOL> line = ( template % ( c , k [ : k_len ] ) ) [ : width ] <EOL> try : <EOL> self . win . addstr ( i + <NUM_LIT:2> , <NUM_LIT:0> , line . encode ( '<STR_LIT:utf8>' ) ) <EOL> except : <EOL> raise Exception ( "<STR_LIT>" <EOL> % line . encode ( '<STR_LIT:utf8>' ) ) <EOL> logging . debug ( '<STR_LIT>' ) <EOL> self . win . refresh ( ) <EOL> @ click . command ( ) <EOL> @ click . option ( '<STR_LIT>' , '<STR_LIT>' , type = int , default = None , <EOL> help = '<STR_LIT>' ) <EOL> @ click . option ( '<STR_LIT>' , is_flag = True , help = '<STR_LIT>' ) <EOL> def main ( window = None , debug = False ) : <EOL> """<STR_LIT>""" <EOL> if debug : <EOL> if os . path . exists ( '<STR_LIT>' ) : <EOL> os . remove ( '<STR_LIT>' ) <EOL> logging . basicConfig ( filename = '<STR_LIT>' , level = logging . DEBUG ) <EOL> if window : <EOL> err = curses . wrapper ( anytop , n = window ) <EOL> else : <EOL> err = curses . wrapper ( anytop ) <EOL> if err : <EOL> raise err </s>
