<s> import time <EOL> from StringIO import StringIO <EOL> from muntjac . api import ( VerticalLayout , Label , ProgressIndicator , HorizontalLayout , <EOL> Upload , Alignment , Button ) <EOL> from muntjac . ui import button , upload <EOL> class ImmediateUploadExample ( VerticalLayout ) : <EOL> def __init__ ( self ) : <EOL> super ( ImmediateUploadExample , self ) . __init__ ( ) <EOL> self . setSpacing ( True ) <EOL> self . _status = Label ( '<STR_LIT>' ) <EOL> self . _pi = ProgressIndicator ( ) <EOL> self . _receiver = MyReceiver ( ) <EOL> self . _progressLayout = HorizontalLayout ( ) <EOL> self . _upload = Upload ( None , self . _receiver ) <EOL> self . _receiver . setSlow ( True ) <EOL> self . addComponent ( self . _status ) <EOL> self . addComponent ( self . _upload ) <EOL> self . addComponent ( self . _progressLayout ) <EOL> self . _upload . setImmediate ( True ) <EOL> self . _upload . setButtonCaption ( '<STR_LIT>' ) <EOL> self . _progressLayout . setSpacing ( True ) <EOL> self . _progressLayout . setVisible ( False ) <EOL> self . _progressLayout . addComponent ( self . _pi ) <EOL> self . _progressLayout . setComponentAlignment ( self . _pi , <EOL> Alignment . MIDDLE_LEFT ) <EOL> cancelProcessing = Button ( '<STR_LIT>' ) <EOL> cancelProcessing . addListener ( CancelListener ( self ) , <EOL> button . IClickListener ) <EOL> cancelProcessing . setStyleName ( '<STR_LIT>' ) <EOL> self . _progressLayout . addComponent ( cancelProcessing ) <EOL> self . _upload . addListener ( StartedListener ( self ) , <EOL> upload . IStartedListener ) <EOL> self . _upload . addListener ( ProgressListener ( self ) , <EOL> upload . IProgressListener ) <EOL> self . _upload . addListener ( SucceededListener ( self ) , <EOL> upload . ISucceededListener ) <EOL> self . _upload . addListener ( FailedListener ( self ) , <EOL> upload . IFailedListener ) <EOL> self . _upload . addListener ( FinishedListener ( self ) , <EOL> upload . IFinishedListener ) <EOL> class CancelListener ( button . IClickListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def buttonClick ( self , event ) : <EOL> self . _c . _upload . interruptUpload ( ) <EOL> class StartedListener ( upload . IStartedListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadStarted ( self , event ) : <EOL> self . _c . _upload . setVisible ( False ) <EOL> self . _c . _progressLayout . setVisible ( True ) <EOL> self . _c . _pi . setValue ( <NUM_LIT:0.0> ) <EOL> self . _c . _pi . setPollingInterval ( <NUM_LIT> ) <EOL> self . _c . _status . setValue ( '<STR_LIT>' <EOL> + event . getFilename ( ) + '<STR_LIT>' ) <EOL> class ProgressListener ( upload . IProgressListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def updateProgress ( self , readBytes , contentLength ) : <EOL> self . _c . _pi . setValue ( float ( readBytes / contentLength ) ) <EOL> class SucceededListener ( upload . ISucceededListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadSucceeded ( self , event ) : <EOL> self . _c . _status . setValue ( '<STR_LIT>' <EOL> + event . getFilename ( ) + '<STR_LIT>' ) <EOL> class FailedListener ( upload . IFailedListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadFailed ( self , event ) : <EOL> self . _c . _status . setValue ( '<STR_LIT>' ) <EOL> class FinishedListener ( upload . IFinishedListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadFinished ( self , event ) : <EOL> self . _c . _progressLayout . setVisible ( False ) <EOL> self . _c . _upload . setVisible ( True ) <EOL> self . _c . _upload . setCaption ( '<STR_LIT>' ) <EOL> class MyReceiver ( upload . IReceiver ) : <EOL> def __init__ ( self ) : <EOL> self . _fileName = None <EOL> self . _mtype = None <EOL> self . _sleep = None <EOL> self . _total = <NUM_LIT:0> <EOL> def receiveUpload ( self , filename , mimetype ) : <EOL> self . _fileName = filename <EOL> self . _mtype = mimetype <EOL> return UploadStream ( self ) <EOL> def getFileName ( self ) : <EOL> return self . _fileName <EOL> def getMimeType ( self ) : <EOL> return self . _mtype <EOL> def setSlow ( self , value ) : <EOL> self . _sleep = value <EOL> class UploadStream ( StringIO ) : <EOL> def __init__ ( self , r ) : <EOL> super ( UploadStream , self ) . __init__ ( ) <EOL> self . _r = r <EOL> def write ( self , b ) : <EOL> self . _r . _total += <NUM_LIT:1> <EOL> if self . _r . _sleep and ( self . _r . _total % <NUM_LIT> == <NUM_LIT:0> ) : <EOL> time . sleep ( <NUM_LIT:100> ) </s>
