<s> import argparse <EOL> import math <EOL> import signal <EOL> import sys <EOL> import time <EOL> try : <EOL> import progressbar <EOL> except ImportError : <EOL> pass <EOL> from requestbuilder import Arg , MutuallyExclusiveArgList <EOL> if '<STR_LIT>' in sys . modules : <EOL> _PROGRESS_BAR_COMMAND_ARGS = [ <EOL> MutuallyExclusiveArgList ( <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> default = sys . stdout . isatty ( ) , route_to = None , <EOL> help = '<STR_LIT>' ) , <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = sys . stdout . isatty ( ) , route_to = None , help = '''<STR_LIT>''' ) , <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> route_to = None , help = argparse . SUPPRESS ) ) ] <EOL> else : <EOL> _PROGRESS_BAR_COMMAND_ARGS = [ <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = False , route_to = None , help = argparse . SUPPRESS ) , <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT>' , <EOL> default = False , route_to = None , help = argparse . SUPPRESS ) , <EOL> Arg ( '<STR_LIT>' , dest = '<STR_LIT>' , action = '<STR_LIT:store_true>' , <EOL> route_to = None , help = argparse . SUPPRESS ) ] <EOL> class FileTransferProgressBarMixin ( object ) : <EOL> '''<STR_LIT>''' <EOL> ARGS = _PROGRESS_BAR_COMMAND_ARGS <EOL> def get_progressbar ( self , label = None , maxval = None ) : <EOL> if self . args . get ( '<STR_LIT>' ) : <EOL> return _MachineReadableCounter ( label = label , maxval = maxval ) <EOL> elif '<STR_LIT>' in sys . modules and self . args . get ( '<STR_LIT>' , <EOL> False ) : <EOL> widgets = [ ] <EOL> if label is not None : <EOL> widgets += [ label , '<STR_LIT:U+0020>' ] <EOL> if maxval is not None : <EOL> widgets += [ progressbar . Percentage ( ) , '<STR_LIT:U+0020>' , <EOL> progressbar . Bar ( marker = '<STR_LIT:=>' ) , '<STR_LIT:U+0020>' , <EOL> _FileSize ( ) , '<STR_LIT:U+0020>' , <EOL> progressbar . FileTransferSpeed ( ) , '<STR_LIT:U+0020>' ] <EOL> if '<STR_LIT>' in dir ( progressbar ) : <EOL> widgets . append ( progressbar . AdaptiveETA ( ) ) <EOL> else : <EOL> widgets . append ( progressbar . ETA ( ) ) <EOL> pbar = progressbar . ProgressBar ( widgets = widgets , <EOL> maxval = ( maxval or sys . maxint ) , <EOL> poll = <NUM_LIT> ) <EOL> signal . siginterrupt ( signal . SIGWINCH , False ) <EOL> return pbar <EOL> else : <EOL> widgets += [ _IndeterminateBouncingBar ( marker = '<STR_LIT:=>' ) , '<STR_LIT:U+0020>' , <EOL> _FileSize ( ) , '<STR_LIT:U+0020>' , <EOL> progressbar . FileTransferSpeed ( ) , '<STR_LIT:U+0020>' , <EOL> progressbar . Timer ( format = '<STR_LIT>' ) ] <EOL> pbar = _IndeterminateProgressBar ( widgets = widgets , <EOL> maxval = ( maxval or sys . maxint ) , <EOL> poll = <NUM_LIT> ) <EOL> signal . siginterrupt ( signal . SIGWINCH , False ) <EOL> return pbar <EOL> else : <EOL> return _EveryMethodObject ( ) <EOL> class _EveryMethodObject ( object ) : <EOL> def do_nothing ( self , * args , ** kwargs ) : <EOL> pass <EOL> def __getattribute__ ( self , name ) : <EOL> return object . __getattribute__ ( self , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in sys . modules : <EOL> class _IndeterminateProgressBar ( progressbar . ProgressBar ) : <EOL> def finish ( self ) : <EOL> self . maxval = self . currval <EOL> progressbar . ProgressBar . finish ( self ) <EOL> class _IndeterminateBouncingBar ( progressbar . BouncingBar ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> progressbar . BouncingBar . __init__ ( self , * args , ** kwargs ) <EOL> self . __update_count = <NUM_LIT:0> <EOL> def update ( self , pbar , width ) : <EOL> orig_currval = pbar . currval <EOL> pbar . currval = self . __update_count <EOL> retval = progressbar . BouncingBar . update ( self , pbar , width ) <EOL> pbar . currval = orig_currval <EOL> self . __update_count += <NUM_LIT:1> <EOL> return retval <EOL> class _FileSize ( progressbar . Widget ) : <EOL> PREFIXES = '<STR_LIT>' <EOL> def update ( self , pbar ) : <EOL> if pbar . currval == <NUM_LIT:0> : <EOL> power = <NUM_LIT:0> <EOL> scaledval = <NUM_LIT:0> <EOL> else : <EOL> power = int ( math . log ( pbar . currval , <NUM_LIT> ) ) <EOL> scaledval = pbar . currval / <NUM_LIT> ** power <EOL> return '<STR_LIT>' . format ( scaledval , self . PREFIXES [ power ] ) <EOL> class _MachineReadableCounter ( object ) : <EOL> def __init__ ( self , maxval = None , label = None ) : <EOL> self . maxval = maxval <EOL> self . currval = <NUM_LIT:0> <EOL> self . _last_displayed_val = None <EOL> self . _last_updated = <NUM_LIT:0> <EOL> self . _finished = False <EOL> if label : <EOL> self . __template = '<STR_LIT>' . format ( label ) <EOL> else : <EOL> self . __template = '<STR_LIT>' <EOL> if self . maxval : <EOL> self . __template = '<STR_LIT>' . format ( self . __template , <EOL> int ( self . maxval ) ) <EOL> else : <EOL> self . __template = '<STR_LIT>' . format ( self . __template ) <EOL> def start ( self ) : <EOL> self . _display ( ) <EOL> def update ( self , val ) : <EOL> self . currval = val <EOL> delta = time . time ( ) - self . _last_updated <EOL> if ( delta > <NUM_LIT:0.1> and self . currval != self . _last_displayed_val and <EOL> not self . _finished ) : <EOL> self . _display ( ) <EOL> self . _last_updated = time . time ( ) <EOL> def finish ( self ) : <EOL> if self . maxval : <EOL> self . currval = self . maxval <EOL> self . _display ( ) <EOL> self . _finished = True <EOL> def _display ( self ) : <EOL> sys . stderr . write ( self . __template . format ( int ( self . currval ) ) ) <EOL> self . _last_displayed_val = self . currval </s>
