<s> """<STR_LIT>""" <EOL> from warnings import warn <EOL> from muntjac . ui . abstract_component import AbstractComponent <EOL> from muntjac . ui . component import IFocusable , Event as ComponentEvent <EOL> from muntjac . terminal . stream_variable import IStreamVariable , IStreamingEvent <EOL> from muntjac . terminal . gwt . server . exceptions import NoInputStreamException , NoOutputStreamException <EOL> from muntjac . util import OrderedSet <EOL> class IStartedListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def uploadStarted ( self , event ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class IFinishedListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def uploadFinished ( self , event ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class IFailedListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def uploadFailed ( self , event ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class ISucceededListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def uploadSucceeded ( self , event ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class IProgressListener ( object ) : <EOL> """<STR_LIT>""" <EOL> def updateProgress ( self , readBytes , contentLength ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> _UPLOAD_FINISHED_METHOD = getattr ( IFinishedListener , '<STR_LIT>' ) <EOL> _UPLOAD_FAILED_METHOD = getattr ( IFailedListener , '<STR_LIT>' ) <EOL> _UPLOAD_STARTED_METHOD = getattr ( IStartedListener , '<STR_LIT>' ) <EOL> _UPLOAD_SUCCEEDED_METHOD = getattr ( ISucceededListener , '<STR_LIT>' ) <EOL> class Upload ( AbstractComponent , IFocusable ) : <EOL> """<STR_LIT>""" <EOL> CLIENT_WIDGET = None <EOL> def __init__ ( self , caption = None , uploadReceiver = None ) : <EOL> """<STR_LIT>""" <EOL> super ( Upload , self ) . __init__ ( ) <EOL> self . _focus = False <EOL> self . _tabIndex = <NUM_LIT:0> <EOL> self . _receiver = None <EOL> self . _isUploading = False <EOL> self . _contentLength = - <NUM_LIT:1> <EOL> self . _totalBytes = None <EOL> self . _buttonCaption = '<STR_LIT>' <EOL> self . _progressListeners = OrderedSet ( ) <EOL> self . _progressCallbacks = dict ( ) <EOL> self . _interrupted = False <EOL> self . _notStarted = None <EOL> self . _nextid = <NUM_LIT:0> <EOL> self . _forceSubmit = None <EOL> if caption : <EOL> self . setCaption ( caption ) <EOL> if uploadReceiver is not None : <EOL> self . _receiver = uploadReceiver <EOL> self . _streamVariable = None <EOL> def changeVariables ( self , source , variables ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in variables : <EOL> idd = variables . get ( '<STR_LIT>' ) <EOL> if not self . _isUploading and idd == self . _nextid : <EOL> self . _notStarted = True <EOL> self . requestRepaint ( ) <EOL> else : <EOL> pass <EOL> def paintContent ( self , target ) : <EOL> """<STR_LIT>""" <EOL> if self . _notStarted : <EOL> target . addAttribute ( '<STR_LIT>' , True ) <EOL> self . _notStarted = False <EOL> return <EOL> if self . _forceSubmit : <EOL> target . addAttribute ( '<STR_LIT>' , True ) <EOL> self . _forceSubmit = True <EOL> return <EOL> if self . _focus : <EOL> target . addAttribute ( '<STR_LIT>' , True ) <EOL> if self . _tabIndex >= <NUM_LIT:0> : <EOL> target . addAttribute ( '<STR_LIT>' , self . _tabIndex ) <EOL> target . addAttribute ( '<STR_LIT:state>' , self . _isUploading ) <EOL> if self . _buttonCaption is not None : <EOL> target . addAttribute ( '<STR_LIT>' , self . _buttonCaption ) <EOL> target . addAttribute ( '<STR_LIT>' , self . _nextid ) <EOL> target . addVariable ( self , '<STR_LIT:action>' , self . getStreamVariable ( ) ) <EOL> def addListener ( self , listener , iface = None ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( listener , IFailedListener ) and <EOL> ( iface is None or issubclass ( iface , IFailedListener ) ) ) : <EOL> self . registerListener ( FailedEvent , <EOL> listener , _UPLOAD_FAILED_METHOD ) <EOL> if ( isinstance ( listener , IFinishedListener ) and <EOL> ( iface is None or issubclass ( iface , IFinishedListener ) ) ) : <EOL> self . registerListener ( FinishedEvent , <EOL> listener , _UPLOAD_FINISHED_METHOD ) <EOL> if ( isinstance ( listener , IProgressListener ) and <EOL> ( iface is None or issubclass ( iface , IProgressListener ) ) ) : <EOL> self . _progressListeners . add ( listener ) <EOL> if ( isinstance ( listener , IStartedListener ) and <EOL> ( iface is None or issubclass ( iface , IStartedListener ) ) ) : <EOL> self . registerListener ( StartedEvent , <EOL> listener , _UPLOAD_STARTED_METHOD ) <EOL> if ( isinstance ( listener , ISucceededListener ) and <EOL> ( iface is None or issubclass ( iface , ISucceededListener ) ) ) : <EOL> self . registerListener ( SucceededEvent , <EOL> listener , _UPLOAD_SUCCEEDED_METHOD ) <EOL> super ( Upload , self ) . addListener ( listener , iface ) <EOL> def addCallback ( self , callback , eventType = None , * args ) : <EOL> if eventType is None : <EOL> eventType = callback . _eventType <EOL> if issubclass ( eventType , FailedEvent ) : <EOL> self . registerCallback ( FailedEvent , callback , None , * args ) <EOL> elif issubclass ( eventType , FinishedEvent ) : <EOL> self . registerCallback ( FinishedEvent , callback , None , * args ) <EOL> elif issubclass ( eventType , IProgressListener ) : <EOL> self . _progressCallbacks [ callback ] = args <EOL> elif issubclass ( eventType , StartedEvent ) : <EOL> self . registerCallback ( StartedEvent , callback , None , * args ) <EOL> elif issubclass ( eventType , SucceededEvent ) : <EOL> self . registerCallback ( SucceededEvent , callback , None , * args ) <EOL> else : <EOL> super ( Upload , self ) . addCallback ( callback , eventType , * args ) <EOL> def removeListener ( self , listener , iface = None ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( listener , IFailedListener ) and <EOL> ( iface is None or issubclass ( iface , IFailedListener ) ) ) : <EOL> self . withdrawListener ( FailedEvent , <EOL> listener , _UPLOAD_FAILED_METHOD ) <EOL> if ( isinstance ( listener , IFinishedListener ) and <EOL> ( iface is None or issubclass ( iface , IFinishedListener ) ) ) : <EOL> self . withdrawListener ( FinishedEvent , <EOL> listener , _UPLOAD_FINISHED_METHOD ) <EOL> if ( isinstance ( listener , IProgressListener ) and <EOL> ( iface is None or issubclass ( iface , IProgressListener ) ) ) : <EOL> if listener in self . _progressListeners : <EOL> self . _progressListeners . remove ( listener ) <EOL> if ( isinstance ( listener , IStartedListener ) and <EOL> ( iface is None or issubclass ( iface , IStartedListener ) ) ) : <EOL> self . withdrawListener ( StartedEvent , <EOL> listener , _UPLOAD_STARTED_METHOD ) <EOL> if ( isinstance ( listener , ISucceededListener ) and <EOL> ( iface is None or issubclass ( iface , ISucceededListener ) ) ) : <EOL> self . withdrawListener ( SucceededEvent , <EOL> listener , _UPLOAD_SUCCEEDED_METHOD ) <EOL> super ( Upload , self ) . removeListener ( listener , iface ) <EOL> def removeCallback ( self , callback , eventType = None ) : <EOL> if eventType is None : <EOL> eventType = callback . _eventType <EOL> if issubclass ( eventType , FailedEvent ) : <EOL> self . withdrawCallback ( FailedEvent , callback ) <EOL> elif issubclass ( eventType , FinishedEvent ) : <EOL> self . withdrawCallback ( FinishedEvent , callback ) <EOL> elif issubclass ( eventType , IProgressListener ) : <EOL> if callback in self . _progressCallbacks : <EOL> del self . _progressListeners [ callback ] <EOL> elif issubclass ( eventType , StartedEvent ) : <EOL> self . withdrawCallback ( StartedEvent , callback ) <EOL> elif issubclass ( eventType , SucceededEvent ) : <EOL> self . withdrawCallback ( SucceededEvent , callback ) <EOL> else : <EOL> super ( Upload , self ) . removeCallback ( callback , eventType ) <EOL> def fireStarted ( self , filename , MIMEType ) : <EOL> """<STR_LIT>""" <EOL> evt = StartedEvent ( self , filename , MIMEType , self . _contentLength ) <EOL> self . fireEvent ( evt ) <EOL> def fireUploadInterrupted ( self , filename , MIMEType , length , e = None ) : <EOL> """<STR_LIT>""" <EOL> if e is None : <EOL> evt = FailedEvent ( self , filename , MIMEType , length ) <EOL> else : <EOL> evt = FailedEvent ( self , filename , MIMEType , length , e ) <EOL> self . fireEvent ( evt ) <EOL> def fireNoInputStream ( self , filename , MIMEType , length ) : <EOL> evt = NoInputStreamEvent ( self , filename , MIMEType , length ) <EOL> self . fireEvent ( evt ) <EOL> def fireNoOutputStream ( self , filename , MIMEType , length ) : <EOL> evt = NoOutputStreamEvent ( self , filename , MIMEType , length ) <EOL> self . fireEvent ( evt ) <EOL> def fireUploadSuccess ( self , filename , MIMEType , length ) : <EOL> """<STR_LIT>""" <EOL> evt = SucceededEvent ( self , filename , MIMEType , length ) <EOL> self . fireEvent ( evt ) <EOL> def fireUpdateProgress ( self , totalBytes , contentLength ) : <EOL> """<STR_LIT>""" <EOL> for l in self . _progressListeners : <EOL> l . updateProgress ( totalBytes , contentLength ) <EOL> for callback , args in self . _progressCallbacks . iteritems ( ) : <EOL> callback ( totalBytes , contentLength , * args ) <EOL> def getReceiver ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _receiver <EOL> def setReceiver ( self , receiver ) : <EOL> """<STR_LIT>""" <EOL> self . _receiver = receiver <EOL> def focus ( self ) : <EOL> super ( Upload , self ) . focus ( ) <EOL> def getTabIndex ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _tabIndex <EOL> def setTabIndex ( self , tabIndex ) : <EOL> """<STR_LIT>""" <EOL> self . _tabIndex = tabIndex <EOL> def startUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _isUploading : <EOL> raise ValueError , '<STR_LIT>' <EOL> self . _isUploading = True <EOL> self . _nextid += <NUM_LIT:1> <EOL> def interruptUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _isUploading : <EOL> self . _interrupted = True <EOL> def endUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _isUploading = False <EOL> self . _contentLength = - <NUM_LIT:1> <EOL> self . _interrupted = False <EOL> self . requestRepaint ( ) <EOL> def isUploading ( self ) : <EOL> return self . _isUploading <EOL> def getBytesRead ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _totalBytes <EOL> def getUploadSize ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _contentLength <EOL> def setProgressListener ( self , progressListener ) : <EOL> """<STR_LIT>""" <EOL> warn ( '<STR_LIT>' , DeprecationWarning ) <EOL> self . addListener ( progressListener , IProgressListener ) <EOL> def getProgressListener ( self ) : <EOL> """<STR_LIT>""" <EOL> warn ( '<STR_LIT>' , DeprecationWarning ) <EOL> if len ( self . _progressListeners ) == <NUM_LIT:0> : <EOL> return None <EOL> else : <EOL> return iter ( self . _progressListeners ) . next ( ) <EOL> def getButtonCaption ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _buttonCaption <EOL> def setButtonCaption ( self , buttonCaption ) : <EOL> """<STR_LIT>""" <EOL> self . _buttonCaption = buttonCaption <EOL> self . requestRepaint ( ) <EOL> def submitUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> self . requestRepaint ( ) <EOL> self . _forceSubmit = True <EOL> def requestRepaint ( self ) : <EOL> self . _forceSubmit = False <EOL> super ( Upload , self ) . requestRepaint ( ) <EOL> def getStreamVariable ( self ) : <EOL> if self . _streamVariable is None : <EOL> self . _streamVariable = InnerStreamVariable ( self ) <EOL> return self . _streamVariable <EOL> def getListeners ( self , eventType ) : <EOL> if issubclass ( eventType , IStreamingEvent ) : <EOL> return list ( self . _progressListeners ) <EOL> return super ( Upload , self ) . getListeners ( eventType ) <EOL> def getCallbacks ( self , eventType ) : <EOL> if issubclass ( eventType , IStreamingEvent ) : <EOL> return dict ( self . _progressCallbacks ) <EOL> return super ( Upload , self ) . getCallbacks ( eventType ) <EOL> class InnerStreamVariable ( IStreamVariable ) : <EOL> def __init__ ( self , upload ) : <EOL> self . _upload = upload <EOL> self . _lastStartedEvent = None <EOL> def listenProgress ( self ) : <EOL> return ( self . _upload . progressListeners is not None <EOL> and len ( self . _upload . progressListeners ) > <NUM_LIT:0> ) <EOL> def onProgress ( self , event ) : <EOL> self . _upload . fireUpdateProgress ( event . getBytesReceived ( ) , <EOL> event . getContentLength ( ) ) <EOL> def isInterrupted ( self ) : <EOL> return self . _upload . interrupted <EOL> def getOutputStream ( self ) : <EOL> receiveUpload = self . _upload . receiver . receiveUpload ( <EOL> self . _lastStartedEvent . getFileName ( ) , <EOL> self . _lastStartedEvent . getMimeType ( ) ) <EOL> self . _lastStartedEvent = None <EOL> return receiveUpload <EOL> def streamingStarted ( self , event ) : <EOL> self . startUpload ( ) <EOL> self . _upload . contentLength = event . getContentLength ( ) <EOL> self . _upload . fireStarted ( event . getFileName ( ) , <EOL> event . getMimeType ( ) ) <EOL> self . _lastStartedEvent = event <EOL> def streamingFinished ( self , event ) : <EOL> self . _upload . fireUploadSuccess ( event . getFileName ( ) , <EOL> event . getMimeType ( ) , event . getContentLength ( ) ) <EOL> self . _upload . endUpload ( ) <EOL> self . _upload . requestRepaint ( ) <EOL> def streamingFailed ( self , event ) : <EOL> exception = event . getException ( ) <EOL> if isinstance ( exception , NoInputStreamException ) : <EOL> self . _upload . fireNoInputStream ( event . getFileName ( ) , <EOL> event . getMimeType ( ) , <NUM_LIT:0> ) <EOL> elif isinstance ( exception , NoOutputStreamException ) : <EOL> self . _upload . fireNoOutputStream ( event . getFileName ( ) , <EOL> event . getMimeType ( ) , <NUM_LIT:0> ) <EOL> else : <EOL> self . _upload . fireUploadInterrupted ( event . getFileName ( ) , <EOL> event . getMimeType ( ) , <NUM_LIT:0> , exception ) <EOL> self . _upload . endUpload ( ) <EOL> class IReceiver ( object ) : <EOL> """<STR_LIT>""" <EOL> def receiveUpload ( self , filename , mimeType ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class FinishedEvent ( ComponentEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , length ) : <EOL> """<STR_LIT>""" <EOL> super ( FinishedEvent , self ) . __init__ ( source ) <EOL> self . _type = MIMEType <EOL> self . _filename = filename <EOL> self . _length = length <EOL> def getUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getSource ( ) <EOL> def getFilename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _filename <EOL> def getMIMEType ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _type <EOL> def getLength ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _length <EOL> class FailedEvent ( FinishedEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , length , reason = None ) : <EOL> super ( FailedEvent , self ) . __init__ ( source , filename , MIMEType , length ) <EOL> self . _reason = reason <EOL> def getReason ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _reason <EOL> class NoOutputStreamEvent ( FailedEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , length ) : <EOL> super ( NoOutputStreamEvent , self ) . __init__ ( source , filename , MIMEType , <EOL> length ) <EOL> class NoInputStreamEvent ( FailedEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , length ) : <EOL> super ( NoInputStreamEvent , self ) . __init__ ( source , filename , MIMEType , <EOL> length ) <EOL> class SucceededEvent ( FinishedEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , length ) : <EOL> super ( SucceededEvent , self ) . __init__ ( source , filename , MIMEType , length ) <EOL> class StartedEvent ( ComponentEvent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , source , filename , MIMEType , contentLength ) : <EOL> super ( StartedEvent , self ) . __init__ ( source ) <EOL> self . _filename = filename <EOL> self . _type = MIMEType <EOL> self . _length = contentLength <EOL> def getUpload ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getSource ( ) <EOL> def getFilename ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _filename <EOL> def getMIMEType ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _type <EOL> def getContentLength ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _length </s>
