<s> from threading import Event <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> class DownloaderFeedback : <EOL> def __init__ ( self , choker , httpdl , add_task , upfunc , downfunc , <EOL> ratemeasure , leftfunc , file_length , finflag , sp , statistics , <EOL> statusfunc = None , interval = None ) : <EOL> self . choker = choker <EOL> self . httpdl = httpdl <EOL> self . add_task = add_task <EOL> self . upfunc = upfunc <EOL> self . downfunc = downfunc <EOL> self . ratemeasure = ratemeasure <EOL> self . leftfunc = leftfunc <EOL> self . file_length = file_length <EOL> self . finflag = finflag <EOL> self . sp = sp <EOL> self . statistics = statistics <EOL> self . lastids = [ ] <EOL> self . spewdata = None <EOL> self . doneprocessing = Event ( ) <EOL> self . doneprocessing . set ( ) <EOL> if statusfunc : <EOL> self . autodisplay ( statusfunc , interval ) <EOL> def _rotate ( self ) : <EOL> cs = self . choker . connections <EOL> for id in self . lastids : <EOL> for i in xrange ( len ( cs ) ) : <EOL> if cs [ i ] . get_id ( ) == id : <EOL> return cs [ i : ] + cs [ : i ] <EOL> return cs <EOL> def spews ( self ) : <EOL> l = [ ] <EOL> cs = self . _rotate ( ) <EOL> self . lastids = [ c . get_id ( ) for c in cs ] <EOL> for c in cs : <EOL> a = { } <EOL> a [ '<STR_LIT:id>' ] = c . get_readable_id ( ) <EOL> a [ '<STR_LIT>' ] = c . get_ip ( ) <EOL> a [ '<STR_LIT>' ] = ( c is self . choker . connections [ <NUM_LIT:0> ] ) <EOL> if c . is_locally_initiated ( ) : <EOL> a [ '<STR_LIT>' ] = '<STR_LIT:L>' <EOL> else : <EOL> a [ '<STR_LIT>' ] = '<STR_LIT:R>' <EOL> u = c . get_upload ( ) <EOL> a [ '<STR_LIT>' ] = int ( u . measure . get_rate ( ) ) <EOL> a [ '<STR_LIT>' ] = u . is_interested ( ) <EOL> a [ '<STR_LIT>' ] = u . is_choked ( ) <EOL> d = c . get_download ( ) <EOL> a [ '<STR_LIT>' ] = int ( d . measure . get_rate ( ) ) <EOL> a [ '<STR_LIT>' ] = d . is_interested ( ) <EOL> a [ '<STR_LIT>' ] = d . is_choked ( ) <EOL> a [ '<STR_LIT>' ] = d . is_snubbed ( ) <EOL> a [ '<STR_LIT>' ] = d . connection . upload . measure . get_total ( ) <EOL> a [ '<STR_LIT>' ] = d . connection . download . measure . get_total ( ) <EOL> if d . connection . download . have : <EOL> a [ '<STR_LIT>' ] = float ( len ( d . connection . download . have ) - d . connection . download . have . numfalse ) / float ( len ( d . connection . download . have ) ) <EOL> else : <EOL> a [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> a [ '<STR_LIT>' ] = d . connection . download . peermeasure . get_rate ( ) <EOL> l . append ( a ) <EOL> for dl in self . httpdl . get_downloads ( ) : <EOL> if dl . goodseed : <EOL> a = { } <EOL> a [ '<STR_LIT:id>' ] = '<STR_LIT>' <EOL> a [ '<STR_LIT>' ] = dl . baseurl <EOL> a [ '<STR_LIT>' ] = False <EOL> a [ '<STR_LIT>' ] = '<STR_LIT:L>' <EOL> a [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> a [ '<STR_LIT>' ] = False <EOL> a [ '<STR_LIT>' ] = False <EOL> a [ '<STR_LIT>' ] = int ( dl . measure . get_rate ( ) ) <EOL> a [ '<STR_LIT>' ] = True <EOL> a [ '<STR_LIT>' ] = not dl . active <EOL> a [ '<STR_LIT>' ] = not dl . active <EOL> a [ '<STR_LIT>' ] = None <EOL> a [ '<STR_LIT>' ] = dl . measure . get_total ( ) <EOL> a [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> a [ '<STR_LIT>' ] = None <EOL> l . append ( a ) <EOL> return l <EOL> def gather ( self , displayfunc = None ) : <EOL> s = { '<STR_LIT>' : self . statistics . update ( ) } <EOL> if self . sp . isSet ( ) : <EOL> s [ '<STR_LIT>' ] = self . spews ( ) <EOL> else : <EOL> s [ '<STR_LIT>' ] = None <EOL> s [ '<STR_LIT>' ] = self . upfunc ( ) <EOL> if self . finflag . isSet ( ) : <EOL> s [ '<STR_LIT>' ] = self . file_length <EOL> return s <EOL> s [ '<STR_LIT>' ] = self . downfunc ( ) <EOL> obtained , desired = self . leftfunc ( ) <EOL> s [ '<STR_LIT>' ] = obtained <EOL> s [ '<STR_LIT>' ] = desired <EOL> if desired > <NUM_LIT:0> : <EOL> s [ '<STR_LIT>' ] = float ( obtained ) / desired <EOL> else : <EOL> s [ '<STR_LIT>' ] = <NUM_LIT:1.0> <EOL> if desired == obtained : <EOL> s [ '<STR_LIT:time>' ] = <NUM_LIT:0> <EOL> else : <EOL> s [ '<STR_LIT:time>' ] = self . ratemeasure . get_time_left ( desired - obtained ) <EOL> return s <EOL> def display ( self , displayfunc ) : <EOL> if not self . doneprocessing . isSet ( ) : <EOL> return <EOL> self . doneprocessing . clear ( ) <EOL> stats = self . gather ( ) <EOL> if self . finflag . isSet ( ) : <EOL> displayfunc ( dpflag = self . doneprocessing , <EOL> upRate = stats [ '<STR_LIT>' ] , <EOL> statistics = stats [ '<STR_LIT>' ] , spew = stats [ '<STR_LIT>' ] ) <EOL> elif stats [ '<STR_LIT:time>' ] is not None : <EOL> displayfunc ( dpflag = self . doneprocessing , <EOL> fractionDone = stats [ '<STR_LIT>' ] , sizeDone = stats [ '<STR_LIT>' ] , <EOL> downRate = stats [ '<STR_LIT>' ] , upRate = stats [ '<STR_LIT>' ] , <EOL> statistics = stats [ '<STR_LIT>' ] , spew = stats [ '<STR_LIT>' ] , <EOL> timeEst = stats [ '<STR_LIT:time>' ] ) <EOL> else : <EOL> displayfunc ( dpflag = self . doneprocessing , <EOL> fractionDone = stats [ '<STR_LIT>' ] , sizeDone = stats [ '<STR_LIT>' ] , <EOL> downRate = stats [ '<STR_LIT>' ] , upRate = stats [ '<STR_LIT>' ] , <EOL> statistics = stats [ '<STR_LIT>' ] , spew = stats [ '<STR_LIT>' ] ) <EOL> def autodisplay ( self , displayfunc , interval ) : <EOL> self . displayfunc = displayfunc <EOL> self . interval = interval <EOL> self . _autodisplay ( ) <EOL> def _autodisplay ( self ) : <EOL> self . add_task ( self . _autodisplay , self . interval ) <EOL> self . display ( self . displayfunc ) </s>
