<s> import os <EOL> import multiprocessing <EOL> from progressbar import ( ProgressBar , Bar , <EOL> ETA , FileTransferSpeed ) <EOL> from lacli . log import getLogger <EOL> from abc import ABCMeta , abstractmethod <EOL> from logutils . queue import QueueListener <EOL> class queueHandler ( object ) : <EOL> def __enter__ ( self ) : <EOL> q = multiprocessing . Queue ( ) <EOL> self . listener = QueueListener ( q , self ) <EOL> self . listener . start ( ) <EOL> return q <EOL> def __exit__ ( self , type , value , traceback ) : <EOL> self . listener . stop ( ) <EOL> class BaseProgressHandler ( queueHandler , ProgressBar ) : <EOL> __metaclass__ = ABCMeta <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . tx = { } <EOL> self . progress = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> super ( BaseProgressHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def update_current ( self , msg ) : <EOL> self . tx [ msg [ '<STR_LIT>' ] ] = int ( msg [ '<STR_LIT>' ] ) <EOL> return sum ( self . tx . values ( ) ) <EOL> def save_current ( self ) : <EOL> self . progress += sum ( self . tx . values ( ) ) <EOL> self . tx = { } <EOL> def handle ( self , msg ) : <EOL> if '<STR_LIT>' in msg : <EOL> self . update ( self . progress + self . update_current ( msg ) ) <EOL> elif '<STR_LIT>' in msg : <EOL> self . keydone ( msg ) <EOL> self . save_current ( ) <EOL> def __enter__ ( self ) : <EOL> q = super ( BaseProgressHandler , self ) . __enter__ ( ) <EOL> progressToQueue ( q ) <EOL> self . start ( initval = self . progress ) <EOL> return q <EOL> @ abstractmethod <EOL> def keydone ( self , msg ) : <EOL> getLogger ( ) . debug ( "<STR_LIT>" . format ( msg ) ) <EOL> class StateProgressHandler ( BaseProgressHandler ) : <EOL> uploads = { } <EOL> def __init__ ( self , state = None , ** kwargs ) : <EOL> assert state is not None , "<STR_LIT>" <EOL> self . state = state <EOL> progress = kwargs . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> for seq , key in enumerate ( self . state . keys ) : <EOL> progress += key [ '<STR_LIT:size>' ] <EOL> kwargs [ '<STR_LIT>' ] = progress <EOL> super ( StateProgressHandler , self ) . __init__ ( ** kwargs ) <EOL> def update ( self , value = None ) : <EOL> if value is not None : <EOL> self . state . update ( value ) <EOL> super ( StateProgressHandler , self ) . update ( value ) <EOL> getLogger ( ) . debug ( "<STR_LIT>" . format ( self . state . progress ) ) <EOL> def __enter__ ( self ) : <EOL> type ( self ) . uploads [ self . state ] = self <EOL> return super ( StateProgressHandler , self ) . __enter__ ( ) <EOL> def __exit__ ( self , cls , value , traceback ) : <EOL> del type ( self ) . uploads [ self . state ] <EOL> def keydone ( self , msg ) : <EOL> getLogger ( ) . debug ( "<STR_LIT>" + str ( msg [ '<STR_LIT:key>' ] ) + "<STR_LIT>" ) <EOL> self . state . keydone ( msg [ '<STR_LIT:key>' ] , msg [ '<STR_LIT:size>' ] ) <EOL> class ConsoleProgressHandler ( StateProgressHandler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> fname = kwargs . pop ( '<STR_LIT>' , "<STR_LIT:->" ) <EOL> kwargs . setdefault ( '<STR_LIT>' , [ fname , '<STR_LIT>' , Bar ( ) , '<STR_LIT:U+0020>' , <EOL> ETA ( ) , '<STR_LIT:U+0020>' , FileTransferSpeed ( ) ] ) <EOL> super ( ConsoleProgressHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def __enter__ ( self ) : <EOL> self . start ( ) <EOL> return super ( ConsoleProgressHandler , self ) . __enter__ ( ) <EOL> def update ( self , value = None ) : <EOL> super ( ConsoleProgressHandler , self ) . update ( value ) <EOL> self . fd . flush ( ) <EOL> class ServerProgressHandler ( StateProgressHandler ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . eta = "<STR_LIT>" <EOL> kwargs . setdefault ( '<STR_LIT>' , [ ] ) <EOL> kwargs . setdefault ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = open ( os . devnull , '<STR_LIT>' ) <EOL> super ( ServerProgressHandler , self ) . __init__ ( * args , ** kwargs ) <EOL> def eta ( self ) : <EOL> if self . currval == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> else : <EOL> elapsed = self . seconds_elapsed <EOL> eta = elapsed * self . maxval / self . currval - elapsed <EOL> return self . format_time ( eta ) <EOL> progress = None <EOL> def progressToQueue ( queue ) : <EOL> global progress <EOL> progress = queue <EOL> def make_progress ( msg ) : <EOL> global progress <EOL> progress . put ( msg ) <EOL> def save_progress ( key , size ) : <EOL> global progress <EOL> progress . put ( { '<STR_LIT>' : True , '<STR_LIT:key>' : key , '<STR_LIT:size>' : size } ) </s>
