<s> from threading import Event <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> class Statistics_Response : <EOL> pass <EOL> class Statistics : <EOL> def __init__ ( self , upmeasure , downmeasure , connecter , httpdl , <EOL> ratelimiter , rerequest_lastfailed , fdatflag ) : <EOL> self . upmeasure = upmeasure <EOL> self . downmeasure = downmeasure <EOL> self . connecter = connecter <EOL> self . httpdl = httpdl <EOL> self . ratelimiter = ratelimiter <EOL> self . downloader = connecter . downloader <EOL> self . picker = connecter . downloader . picker <EOL> self . storage = connecter . downloader . storage <EOL> self . torrentmeasure = connecter . downloader . totalmeasure <EOL> self . rerequest_lastfailed = rerequest_lastfailed <EOL> self . fdatflag = fdatflag <EOL> self . fdatactive = False <EOL> self . piecescomplete = None <EOL> self . placesopen = None <EOL> self . storage_totalpieces = len ( self . storage . hashes ) <EOL> def set_dirstats ( self , files , piece_length ) : <EOL> self . piecescomplete = <NUM_LIT:0> <EOL> self . placesopen = <NUM_LIT:0> <EOL> self . filelistupdated = Event ( ) <EOL> self . filelistupdated . set ( ) <EOL> frange = xrange ( len ( files ) ) <EOL> self . filepieces = [ [ ] for x in frange ] <EOL> self . filepieces2 = [ [ ] for x in frange ] <EOL> self . fileamtdone = [ <NUM_LIT:0.0> for x in frange ] <EOL> self . filecomplete = [ False for x in frange ] <EOL> self . fileinplace = [ False for x in frange ] <EOL> start = <NUM_LIT:0> L <EOL> for i in frange : <EOL> l = files [ i ] [ <NUM_LIT:1> ] <EOL> if l == <NUM_LIT:0> : <EOL> self . fileamtdone [ i ] = <NUM_LIT:1.0> <EOL> self . filecomplete [ i ] = True <EOL> self . fileinplace [ i ] = True <EOL> else : <EOL> fp = self . filepieces [ i ] <EOL> fp2 = self . filepieces2 [ i ] <EOL> for piece in range ( int ( start / piece_length ) , <EOL> int ( ( start + l - <NUM_LIT:1> ) / piece_length ) + <NUM_LIT:1> ) : <EOL> fp . append ( piece ) <EOL> fp2 . append ( piece ) <EOL> start += l <EOL> def update ( self ) : <EOL> s = Statistics_Response ( ) <EOL> s . upTotal = self . upmeasure . get_total ( ) <EOL> s . downTotal = self . downmeasure . get_total ( ) <EOL> s . last_failed = self . rerequest_lastfailed ( ) <EOL> s . external_connection_made = self . connecter . external_connection_made <EOL> if s . downTotal > <NUM_LIT:0> : <EOL> s . shareRating = float ( s . upTotal ) / s . downTotal <EOL> elif s . upTotal == <NUM_LIT:0> : <EOL> s . shareRating = <NUM_LIT:0.0> <EOL> else : <EOL> s . shareRating = - <NUM_LIT:1.0> <EOL> s . torrentRate = self . torrentmeasure . get_rate ( ) <EOL> s . torrentTotal = self . torrentmeasure . get_total ( ) <EOL> s . numSeeds = self . picker . seeds_connected <EOL> s . numOldSeeds = self . downloader . num_disconnected_seeds ( ) <EOL> s . numPeers = len ( self . downloader . downloads ) - s . numSeeds <EOL> s . numCopies = <NUM_LIT:0.0> <EOL> for i in self . picker . crosscount : <EOL> if i == <NUM_LIT:0> : <EOL> s . numCopies += <NUM_LIT:1> <EOL> else : <EOL> s . numCopies += <NUM_LIT:1> - float ( i ) / self . picker . numpieces <EOL> break <EOL> if self . picker . done : <EOL> s . numCopies2 = s . numCopies + <NUM_LIT:1> <EOL> else : <EOL> s . numCopies2 = <NUM_LIT:0.0> <EOL> for i in self . picker . crosscount2 : <EOL> if i == <NUM_LIT:0> : <EOL> s . numCopies2 += <NUM_LIT:1> <EOL> else : <EOL> s . numCopies2 += <NUM_LIT:1> - float ( i ) / self . picker . numpieces <EOL> break <EOL> s . discarded = self . downloader . discarded <EOL> s . numSeeds += self . httpdl . seedsfound <EOL> s . numOldSeeds += self . httpdl . seedsfound <EOL> if s . numPeers == <NUM_LIT:0> or self . picker . numpieces == <NUM_LIT:0> : <EOL> s . percentDone = <NUM_LIT:0.0> <EOL> else : <EOL> s . percentDone = <NUM_LIT> * ( float ( self . picker . totalcount ) / self . picker . numpieces ) / s . numPeers <EOL> s . backgroundallocating = self . storage . bgalloc_active <EOL> s . storage_totalpieces = len ( self . storage . hashes ) <EOL> s . storage_active = len ( self . storage . stat_active ) <EOL> s . storage_new = len ( self . storage . stat_new ) <EOL> s . storage_dirty = len ( self . storage . dirty ) <EOL> numdownloaded = self . storage . stat_numdownloaded <EOL> s . storage_justdownloaded = numdownloaded <EOL> s . storage_numcomplete = self . storage . stat_numfound + numdownloaded <EOL> s . storage_numflunked = self . storage . stat_numflunked <EOL> s . storage_isendgame = self . downloader . endgamemode <EOL> s . peers_kicked = self . downloader . kicked . items ( ) <EOL> s . peers_banned = self . downloader . banned . items ( ) <EOL> try : <EOL> s . upRate = int ( self . ratelimiter . upload_rate / <NUM_LIT:1000> ) <EOL> assert s . upRate < <NUM_LIT> <EOL> except : <EOL> s . upRate = <NUM_LIT:0> <EOL> s . upSlots = self . ratelimiter . slots <EOL> if self . piecescomplete is None : <EOL> return s <EOL> if self . fdatflag . isSet ( ) : <EOL> if not self . fdatactive : <EOL> self . fdatactive = True <EOL> else : <EOL> self . fdatactive = False <EOL> if self . piecescomplete != self . picker . numgot : <EOL> for i in xrange ( len ( self . filecomplete ) ) : <EOL> if self . filecomplete [ i ] : <EOL> continue <EOL> oldlist = self . filepieces [ i ] <EOL> newlist = [ piece <EOL> for piece in oldlist <EOL> if not self . storage . have [ piece ] ] <EOL> if len ( newlist ) != len ( oldlist ) : <EOL> self . filepieces [ i ] = newlist <EOL> self . fileamtdone [ i ] = ( <EOL> ( len ( self . filepieces2 [ i ] ) - len ( newlist ) ) <EOL> / float ( len ( self . filepieces2 [ i ] ) ) ) <EOL> if not newlist : <EOL> self . filecomplete [ i ] = True <EOL> self . filelistupdated . set ( ) <EOL> self . piecescomplete = self . picker . numgot <EOL> if ( self . filelistupdated . isSet ( ) <EOL> or self . placesopen != len ( self . storage . places ) ) : <EOL> for i in xrange ( len ( self . filecomplete ) ) : <EOL> if not self . filecomplete [ i ] or self . fileinplace [ i ] : <EOL> continue <EOL> while self . filepieces2 [ i ] : <EOL> piece = self . filepieces2 [ i ] [ - <NUM_LIT:1> ] <EOL> if self . storage . places [ piece ] != piece : <EOL> break <EOL> del self . filepieces2 [ i ] [ - <NUM_LIT:1> ] <EOL> if not self . filepieces2 [ i ] : <EOL> self . fileinplace [ i ] = True <EOL> self . storage . set_file_readonly ( i ) <EOL> self . filelistupdated . set ( ) <EOL> self . placesopen = len ( self . storage . places ) <EOL> s . fileamtdone = self . fileamtdone <EOL> s . filecomplete = self . filecomplete <EOL> s . fileinplace = self . fileinplace <EOL> s . filelistupdated = self . filelistupdated <EOL> return s </s>
