<s> import time <EOL> from StringIO import StringIO <EOL> from muntjac . api import ( VerticalLayout , Label , ProgressIndicator , Upload , CheckBox , <EOL> Button , Panel , FormLayout , HorizontalLayout ) <EOL> from muntjac . ui import button , upload <EOL> class UploadWithProgressMonitoringExample ( VerticalLayout ) : <EOL> def __init__ ( self ) : <EOL> super ( UploadWithProgressMonitoringExample , self ) . __init__ ( ) <EOL> self . setSpacing ( True ) <EOL> self . _state = Label ( ) <EOL> self . _result = Label ( ) <EOL> self . _fileName = Label ( ) <EOL> self . _textualProgress = Label ( ) <EOL> self . _pi = ProgressIndicator ( ) <EOL> self . _counter = LineBreakCounter ( ) <EOL> self . _upload = Upload ( None , self . _counter ) <EOL> self . addComponent ( Label ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> self . _upload . setImmediate ( True ) <EOL> self . _upload . setButtonCaption ( '<STR_LIT>' ) <EOL> self . addComponent ( self . _upload ) <EOL> handBrake = CheckBox ( '<STR_LIT>' ) <EOL> handBrake . setValue ( True ) <EOL> self . _counter . setSlow ( True ) <EOL> handBrake . setDescription ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> handBrake . addListener ( HandBrakeListener ( self ) , button . IClickListener ) <EOL> cancelProcessing = Button ( '<STR_LIT>' ) <EOL> cancelProcessing . addListener ( CancelListener ( self ) , <EOL> button . IClickListener ) <EOL> cancelProcessing . setVisible ( False ) <EOL> cancelProcessing . setStyleName ( '<STR_LIT>' ) <EOL> handBrake . setImmediate ( True ) <EOL> self . addComponent ( handBrake ) <EOL> p = Panel ( '<STR_LIT>' ) <EOL> p . setSizeUndefined ( ) <EOL> l = FormLayout ( ) <EOL> l . setMargin ( True ) <EOL> p . setContent ( l ) <EOL> stateLayout = HorizontalLayout ( ) <EOL> stateLayout . setSpacing ( True ) <EOL> stateLayout . addComponent ( self . _state ) <EOL> stateLayout . addComponent ( cancelProcessing ) <EOL> stateLayout . setCaption ( '<STR_LIT>' ) <EOL> self . _state . setValue ( '<STR_LIT>' ) <EOL> l . addComponent ( stateLayout ) <EOL> self . _fileName . setCaption ( '<STR_LIT>' ) <EOL> l . addComponent ( self . _fileName ) <EOL> self . _result . setCaption ( '<STR_LIT>' ) <EOL> l . addComponent ( self . _result ) <EOL> self . _pi . setCaption ( '<STR_LIT>' ) <EOL> self . _pi . setVisible ( False ) <EOL> l . addComponent ( self . _pi ) <EOL> self . _textualProgress . setVisible ( False ) <EOL> l . addComponent ( self . _textualProgress ) <EOL> self . addComponent ( p ) <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 HandBrakeListener ( button . IClickListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def buttonClick ( self , event ) : <EOL> self . _c . _counter . setSlow ( bool ( event . getButton ( ) ) ) <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 . _pi . setValue ( <NUM_LIT:0.0> ) <EOL> self . _c . _pi . setVisible ( True ) <EOL> self . _c . _pi . setPollingInterval ( <NUM_LIT> ) <EOL> self . _c . _textualProgress . setVisible ( True ) <EOL> self . _c . _state . setValue ( '<STR_LIT>' ) <EOL> self . _c . _fileName . setValue ( event . getFilename ( ) ) <EOL> self . cancelProcessing . setVisible ( True ) <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> self . _c . _textualProgress . setValue ( '<STR_LIT>' + readBytes <EOL> + '<STR_LIT>' + contentLength ) <EOL> self . _c . _result . setValue ( self . _c . _counter . getLineBreakCount ( ) <EOL> + '<STR_LIT>' ) <EOL> class SucceededListener ( upload . ISucceededListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadSucceeded ( self , event ) : <EOL> self . _c . _result . setValue ( self . _c . _counter . getLineBreakCount ( ) <EOL> + '<STR_LIT>' ) <EOL> class FailedListener ( upload . IFailedListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadFailed ( self , event ) : <EOL> self . _c . _result . setValue ( self . _c . _counter . getLineBreakCount ( ) <EOL> + '<STR_LIT>' <EOL> + round ( <NUM_LIT:100> * self . _c . _pi . getValue ( ) ) + '<STR_LIT>' ) <EOL> class FinishedListener ( upload . IFinishedListener ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def uploadFinished ( self , event ) : <EOL> self . _c . _state . setValue ( '<STR_LIT>' ) <EOL> self . _c . _pi . setVisible ( False ) <EOL> self . _c . _textualProgress . setVisible ( False ) <EOL> self . cancelProcessing . setVisible ( False ) <EOL> class LineBreakCounter ( upload . IReceiver ) : <EOL> def __init__ ( self ) : <EOL> self . _fileName = None <EOL> self . _mtype = None <EOL> self . _counter = None <EOL> self . _total = None <EOL> self . _sleep = None <EOL> def receiveUpload ( self , filename , MIMEType ) : <EOL> """<STR_LIT>""" <EOL> self . _counter = <NUM_LIT:0> <EOL> self . _total = <NUM_LIT:0> <EOL> self . _fileName = filename <EOL> self . _mtype = MIMEType <EOL> return OutputStream ( ) <EOL> def getFileName ( self ) : <EOL> return self . _fileName <EOL> def getMimeType ( self ) : <EOL> return self . _mtype <EOL> def getLineBreakCount ( self ) : <EOL> return self . _counter <EOL> def setSlow ( self , value ) : <EOL> self . _sleep = value <EOL> class OutputStream ( StringIO ) : <EOL> def __init__ ( self , c ) : <EOL> self . _c = c <EOL> def write ( self , b ) : <EOL> self . _c . _total += <NUM_LIT:1> <EOL> if b == '<STR_LIT:\n>' : <EOL> self . _c . _counter += <NUM_LIT:1> <EOL> if self . _c . _sleep and self . _c . _total % <NUM_LIT:1000> == <NUM_LIT:0> : <EOL> time . sleep ( <NUM_LIT:100> ) </s>
