Package muntjac :: Package terminal :: Module file_resource
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.file_resource

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """File or directory resources on the local filesystem.""" 
 17   
 18  from os.path import getsize, basename 
 19   
 20  from muntjac.service.file_type_resolver import FileTypeResolver 
 21  from muntjac.terminal.application_resource import IApplicationResource 
 22  from muntjac.terminal.download_stream import DownloadStream 
 23  from muntjac.terminal.terminal import IErrorEvent 
 24   
 25   
26 -class FileResource(IApplicationResource):
27 """C{FileResources} are files or directories on local filesystem. The 28 files and directories are served through URI:s to the client terminal 29 and thus must be registered to an URI context before they can be used. 30 The resource is automatically registered to the application when it is 31 created. 32 33 @author: Vaadin Ltd. 34 @author: Richard Lincoln 35 @version: 1.1.2 36 """ 37
38 - def __init__(self, sourceFile, application):
39 """Creates a new file resource for providing given file for 40 client terminals. 41 """ 42 # Default buffer size for this stream resource. 43 self._bufferSize = 0 44 45 # File where the downloaded content is fetched from. 46 self._sourceFile = None 47 48 # Application. 49 self._application = None 50 51 # Default cache time for this stream resource. 52 self._cacheTime = DownloadStream.DEFAULT_CACHETIME 53 54 self._application = application 55 self.setSourceFile(sourceFile) 56 application.addResource(self)
57 58
59 - def getStream(self):
60 """Gets the resource as stream. 61 62 @see: L{IApplicationResource.getStream} 63 """ 64 try: 65 ds = DownloadStream(file(self._sourceFile, 'rb'), 66 self.getMIMEType(), 67 self.getFilename()) 68 length = str( getsize(self._sourceFile) ) 69 ds.setParameter('Content-Length', length) 70 ds.setCacheTime(self._cacheTime) 71 return ds 72 except IOError: 73 # Log the exception using the application error handler 74 class Error(IErrorEvent): 75 76 def getThrowable(self): 77 return self.e
78 79 self.getApplication().getErrorHandler().terminalError( Error() ) 80 return None 81 82
83 - def getSourceFile(self):
84 """Gets the source file. 85 86 @return: the source File. 87 """ 88 return self._sourceFile
89 90
91 - def setSourceFile(self, sourceFile):
92 """Sets the source file. 93 94 @param sourceFile: 95 the source file to set. 96 """ 97 self._sourceFile = sourceFile
98 99
100 - def getApplication(self):
101 """@see: L{IApplicationResource.getApplication}""" 102 return self._application
103 104
105 - def getFilename(self):
106 """@see: L{IApplicationResource.getFilename}""" 107 return basename(self._sourceFile)
108 109
110 - def getMIMEType(self):
111 """@see: L{IResource.getMIMEType}""" 112 return FileTypeResolver.getMIMEType(self._sourceFile)
113 114
115 - def getCacheTime(self):
116 """Gets the length of cache expiration time. This gives the adapter 117 the possibility cache streams sent to the client. The caching may be 118 made in adapter or at the client if the client supports caching. 119 Default is C{DownloadStream.DEFAULT_CACHETIME}. 120 121 @return: Cache time in milliseconds. 122 """ 123 return self._cacheTime
124 125
126 - def setCacheTime(self, cacheTime):
127 """Sets the length of cache expiration time. This gives the adapter 128 the possibility cache streams sent to the client. The caching may be 129 made in adapter or at the client if the client supports caching. Zero 130 or negavive value disbales the caching of this stream. 131 132 @param cacheTime: 133 the cache time in milliseconds. 134 """ 135 self._cacheTime = cacheTime
136 137
138 - def getBufferSize(self):
139 return self._bufferSize
140 141
142 - def setBufferSize(self, bufferSize):
143 """Sets the size of the download buffer used for this resource. 144 145 @param bufferSize: 146 the size of the buffer in bytes. 147 """ 148 self._bufferSize = bufferSize
149