Guest User

QNetworkReply subclass

a guest
Oct 5th, 2010
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class NetworkReply(QNetworkReply):
  2.     def __init__(self, reply):
  3.         self.reply = reply
  4.         QNetworkReply.__init__(self)
  5.  
  6.         # handle these to forward
  7.         reply.metaDataChanged.connect(self.applyMetaData)
  8.         reply.readyRead.connect(self.readInternal)
  9.         reply.error.connect(self.errorInternal)
  10.         # forward signals
  11.         reply.finished.connect(self.finished)
  12.         reply.uploadProgress.connect(self.uploadProgress)
  13.         reply.downloadProgress.connect(self.downloadProgress)
  14.  
  15.         self.setOpenMode(QNetworkReply.ReadOnly)
  16.         self.data = self.buffer = ''
  17.  
  18.  
  19.     def operation(self):
  20.         return self.reply.operation()
  21.  
  22.  
  23.     def request(self):
  24.         return self.reply.request()
  25.  
  26.  
  27.     def url(self):
  28.         return self.reply.url()
  29.  
  30.  
  31.     def abort(self):
  32.         self.reply.abort()
  33.  
  34.     def close(self):
  35.         self.reply.close()
  36.  
  37.  
  38.     def isSequential(self):
  39.         return self.reply.isSequential()
  40.  
  41.  
  42.     def setReadBufferSize(self, size):
  43.         QNetworkReply.setReadBufferSize(size)
  44.         self.reply.setReadBufferSize(size)
  45.  
  46.  
  47.     def applyMetaData(self):
  48.         for header in self.reply.rawHeaderList():
  49.             self.setRawHeader(header, self.reply.rawHeader(header))
  50.  
  51.         self.setHeader(QNetworkRequest.ContentTypeHeader, self.reply.header(QNetworkRequest.ContentTypeHeader))
  52.         self.setHeader(QNetworkRequest.ContentLengthHeader, self.reply.header(QNetworkRequest.ContentLengthHeader))
  53.         self.setHeader(QNetworkRequest.LocationHeader, self.reply.header(QNetworkRequest.LocationHeader))
  54.         self.setHeader(QNetworkRequest.LastModifiedHeader, self.reply.header(QNetworkRequest.LastModifiedHeader))
  55.         self.setHeader(QNetworkRequest.SetCookieHeader, self.reply.header(QNetworkRequest.SetCookieHeader))
  56.  
  57.         self.setAttribute(QNetworkRequest.HttpStatusCodeAttribute, self.reply.attribute(QNetworkRequest.HttpStatusCodeAttribute))
  58.         self.setAttribute(QNetworkRequest.HttpReasonPhraseAttribute, self.reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute))
  59.         self.setAttribute(QNetworkRequest.RedirectionTargetAttribute, self.reply.attribute(QNetworkRequest.RedirectionTargetAttribute))
  60.         self.setAttribute(QNetworkRequest.ConnectionEncryptedAttribute, self.reply.attribute(QNetworkRequest.ConnectionEncryptedAttribute))
  61.         self.setAttribute(QNetworkRequest.CacheLoadControlAttribute, self.reply.attribute(QNetworkRequest.CacheLoadControlAttribute))
  62.         self.setAttribute(QNetworkRequest.CacheSaveControlAttribute, self.reply.attribute(QNetworkRequest.CacheSaveControlAttribute))
  63.         self.setAttribute(QNetworkRequest.SourceIsFromCacheAttribute, self.reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute))
  64.         # attribute does not exist
  65.         #self.setAttribute(QNetworkRequest.DoNotBufferUploadDataAttribute, self.reply.attribute(QNetworkRequest.DoNotBufferUploadDataAttribute))
  66.         self.metaDataChanged.emit()
  67.  
  68.  
  69.     def errorInternal(self, e):
  70.         self.error.emit(e)
  71.         self.setError(e, str(e))
  72.  
  73.  
  74.     def readInternal(self):
  75.         # this is called
  76.         s = self.reply.readAll()
  77.         self.data += s
  78.         self.buffer += s
  79.         self.readyRead.emit()
  80.  
  81.  
  82.     def bytesAvailable(self):
  83.         return len(self.buffer) + self.reply.bytesAvailable()
  84.  
  85.  
  86.     def readAll(self):
  87.         # this is never called
  88.         return self.data
  89.  
  90.  
  91.     def readData(self, data, size):
  92.         # this is never called
  93.         size = min(size, len(self.buffer))
  94.         data, self.buffer = self.buffer[:size], self.buffer[size:]
  95.         return size
RAW Paste Data