class HttpFile(ufsi.FileInterface): """ HttpFile is an implementation of ``ufsi.FileInterface`` that provides an interface to HTTP file systems. HTTP file systems (unless they are enhanced with WebDAV, or some other equivalent technology) are read-only file systems. Therefore, trying to write to one will result in an UnsupportedOperationError being raised. """ def __init__(self, path): """ Creates a HttpFile instance. """ self.__path = path self.__pathStr = str(path) self.__fileHandle = None def __str__(self): """ Returns the path (string) to the file. """ return self.__pathStr def open(self, mode='r'): """ Opens a HTTP connection to the file. If a previous connection was opened it is first closed. TODO: currently, mode is ignored but we should do a little more validation on it. TODO: add this level of error checking to other open methods TODO: make mode='r' for all, ie part of the interface TODO: write Interface Implementation certification code to ensure an implementation adheres to at least the method sigs Note: if you get freezes from any of the methods that open a connection to the server, it may be some issue between the sockets (or something to do with the makefile layer of socket). I have only experienced these problems using IIS5.something. Good Luck! :-) """ self.close() if mode in ('r', 'rb'): try: self.__fileHandle = urllib2.urlopen(self.__pathStr) except Exception, e: HttpUtils.handleException(e, self.__pathStr) elif mode in ('w', 'wb', 'a', 'ab'): raise ufsi.UnsupportedOperationError( 'HTTP has no facility to write.')
class TarFile(ufsi.FileInterface): """ """ def __init__(self,path): """ """ self.__path=path self.__tarPathStr=path.getPathString() self.__fileHandle=None self.__tarFileObject=None def __str__(self): """ """ return str(self.__path) def open(self,mode='r'): """ """ if mode in ['r','rb']: tarFile=self.__path.getTarFilePath() tf=tarFile.getFile() tf.open('r') self.__tarFileObject=tar=tarfile.TarFile('','r',tf) try: self.__fileHandle=tar.extractfile(self.__tarPathStr) except KeyError,e: raise ufsi.PathNotFoundError('Path "%s" not found' %self.__tarPathStr,e) elif mode in ['w','wb','a','ab']: raise ufsi.UnsupportedOperationError("Write is not supported")
def writeLines(self, lines): """ HTTP doesn't allow writing. This method raises an UnsupportedOperationError. """ raise ufsi.UnsupportedOperationError('HTTP has no facility to write.')
def writeLines(self,lines): raise ufsi.UnsupportedOperationError('Tar files can only be read.')