<s> '''<STR_LIT>''' <EOL> from __future__ import division <EOL> import datetime <EOL> import math <EOL> try : <EOL> from abc import ABCMeta , abstractmethod <EOL> except ImportError : <EOL> AbstractWidget = object <EOL> abstractmethod = lambda fn : fn <EOL> else : <EOL> AbstractWidget = ABCMeta ( '<STR_LIT>' , ( object , ) , { } ) <EOL> def format_updatable ( updatable , pbar ) : <EOL> if hasattr ( updatable , '<STR_LIT>' ) : return updatable . update ( pbar ) <EOL> else : return updatable <EOL> class Widget ( AbstractWidget ) : <EOL> '''<STR_LIT>''' <EOL> TIME_SENSITIVE = False <EOL> @ abstractmethod <EOL> def update ( self , pbar ) : <EOL> '''<STR_LIT>''' <EOL> class WidgetHFill ( Widget ) : <EOL> '''<STR_LIT>''' <EOL> @ abstractmethod <EOL> def update ( self , pbar , width ) : <EOL> '''<STR_LIT>''' <EOL> class Timer ( Widget ) : <EOL> '<STR_LIT>' <EOL> TIME_SENSITIVE = True <EOL> def __init__ ( self , format = '<STR_LIT>' ) : <EOL> self . format = format <EOL> @ staticmethod <EOL> def format_time ( seconds ) : <EOL> '<STR_LIT>' <EOL> return str ( datetime . timedelta ( seconds = int ( seconds ) ) ) <EOL> def update ( self , pbar ) : <EOL> '<STR_LIT>' <EOL> return self . format % self . format_time ( pbar . seconds_elapsed ) <EOL> class ETA ( Timer ) : <EOL> '<STR_LIT>' <EOL> TIME_SENSITIVE = True <EOL> def update ( self , pbar ) : <EOL> '<STR_LIT>' <EOL> if pbar . currval == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> elif pbar . finished : <EOL> return '<STR_LIT>' % self . format_time ( pbar . seconds_elapsed ) <EOL> else : <EOL> elapsed = pbar . seconds_elapsed <EOL> eta = elapsed * pbar . maxval / pbar . currval - elapsed <EOL> return '<STR_LIT>' % self . format_time ( eta ) <EOL> class FileTransferSpeed ( Widget ) : <EOL> '<STR_LIT>' <EOL> format = '<STR_LIT>' <EOL> prefixes = '<STR_LIT>' <EOL> def __init__ ( self , unit = '<STR_LIT:B>' ) : <EOL> self . unit = unit <EOL> def update ( self , pbar ) : <EOL> '<STR_LIT>' <EOL> if pbar . seconds_elapsed < <NUM_LIT> or pbar . currval < <NUM_LIT> : <EOL> scaled = power = <NUM_LIT:0> <EOL> else : <EOL> speed = pbar . currval / pbar . seconds_elapsed <EOL> power = int ( math . log ( speed , <NUM_LIT:1000> ) ) <EOL> scaled = speed / <NUM_LIT> ** power <EOL> return self . format % ( scaled , self . prefixes [ power ] , self . unit ) <EOL> class Rate ( Widget ) : <EOL> '<STR_LIT>' <EOL> def update ( self , pbar ) : <EOL> '<STR_LIT>' <EOL> if pbar . seconds_elapsed < <NUM_LIT> or pbar . currval < <NUM_LIT> : <EOL> return "<STR_LIT>" <EOL> else : <EOL> speed = pbar . currval / pbar . seconds_elapsed <EOL> return "<STR_LIT>" . format ( int ( speed ) ) <EOL> return self . format % ( scaled , self . prefixes [ power ] , self . unit ) <EOL> class AnimatedMarker ( Widget ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , markers = '<STR_LIT>' ) : <EOL> self . markers = markers <EOL> self . curmark = - <NUM_LIT:1> <EOL> def update ( self , pbar ) : <EOL> '''<STR_LIT>''' <EOL> if pbar . finished : return self . markers [ <NUM_LIT:0> ] <EOL> self . curmark = ( self . curmark + <NUM_LIT:1> ) % len ( self . markers ) <EOL> return self . markers [ self . curmark ] <EOL> RotatingMarker = AnimatedMarker <EOL> class Counter ( Widget ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , format = '<STR_LIT>' ) : <EOL> self . format = format <EOL> def update ( self , pbar ) : <EOL> return self . format % pbar . currval <EOL> class Percentage ( Widget ) : <EOL> '<STR_LIT>' <EOL> def update ( self , pbar ) : <EOL> return '<STR_LIT>' % pbar . percentage ( ) <EOL> class FormatLabel ( Timer ) : <EOL> '<STR_LIT>' <EOL> mapping = { <EOL> '<STR_LIT>' : ( '<STR_LIT>' , Timer . format_time ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:start>' : ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:value>' : ( '<STR_LIT>' , None ) <EOL> } <EOL> def __init__ ( self , format ) : <EOL> self . format = format <EOL> def update ( self , pbar ) : <EOL> context = { } <EOL> for name , ( key , transform ) in self . mapping . items ( ) : <EOL> try : <EOL> value = getattr ( pbar , key ) <EOL> if transform is None : <EOL> context [ name ] = value <EOL> else : <EOL> context [ name ] = transform ( value ) <EOL> except : pass <EOL> return self . format % context <EOL> class SimpleProgress ( Widget ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , sep = '<STR_LIT>' ) : <EOL> self . sep = sep <EOL> def update ( self , pbar ) : <EOL> return '<STR_LIT>' % ( pbar . currval , self . sep , pbar . maxval ) <EOL> class Bar ( WidgetHFill ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , marker = '<STR_LIT:#>' , left = '<STR_LIT:|>' , right = '<STR_LIT:|>' , fill = '<STR_LIT:U+0020>' , <EOL> fill_left = True ) : <EOL> '''<STR_LIT>''' <EOL> self . marker = marker <EOL> self . left = left <EOL> self . right = right <EOL> self . fill = fill <EOL> self . fill_left = fill_left <EOL> def update ( self , pbar , width ) : <EOL> '<STR_LIT>' <EOL> left , marker , right = ( format_updatable ( i , pbar ) for i in <EOL> ( self . left , self . marker , self . right ) ) <EOL> width -= len ( left ) + len ( right ) <EOL> marker *= int ( pbar . currval / pbar . maxval * width ) <EOL> if self . fill_left : <EOL> return '<STR_LIT>' % ( left , marker . ljust ( width , self . fill ) , right ) <EOL> else : <EOL> return '<STR_LIT>' % ( left , marker . rjust ( width , self . fill ) , right ) <EOL> class ReverseBar ( Bar ) : <EOL> '<STR_LIT>' <EOL> def __init__ ( self , marker = '<STR_LIT:#>' , left = '<STR_LIT:|>' , right = '<STR_LIT:|>' , fill = '<STR_LIT:U+0020>' , <EOL> fill_left = False ) : <EOL> '''<STR_LIT>''' <EOL> self . marker = marker <EOL> self . left = left <EOL> self . right = right <EOL> self . fill = fill <EOL> self . fill_left = fill_left <EOL> class BouncingBar ( Bar ) : <EOL> def update ( self , pbar , width ) : <EOL> '<STR_LIT>' <EOL> left , marker , right = ( format_updatable ( i , pbar ) for i in <EOL> ( self . left , self . marker , self . right ) ) <EOL> width -= len ( left ) + len ( right ) <EOL> if pbar . finished : return '<STR_LIT>' % ( left , width * marker , right ) <EOL> position = int ( pbar . currval % ( width * <NUM_LIT:2> - <NUM_LIT:1> ) ) <EOL> if position > width : position = width * <NUM_LIT:2> - position <EOL> lpad = self . fill * ( position - <NUM_LIT:1> ) <EOL> rpad = self . fill * ( width - len ( marker ) - len ( lpad ) ) <EOL> if not self . fill_left : rpad , lpad = lpad , rpad <EOL> return '<STR_LIT>' % ( left , lpad , marker , rpad , right ) </s>
