eric6/Helpviewer/Network/NetworkReply.py

branch
maintenance
changeset 7286
7eb04391adf7
parent 7226
babe80d84a3e
parent 7285
1ff497f33f31
child 7287
1c17f2191bdd
equal deleted inserted replaced
7226:babe80d84a3e 7286:7eb04391adf7
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing a network reply object for special data.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import QTimer, QIODevice, QByteArray
13 from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
14
15
16 class NetworkReply(QNetworkReply):
17 """
18 Class implementing a QNetworkReply subclass for special data.
19 """
20 def __init__(self, request, fileData, mimeType, parent=None):
21 """
22 Constructor
23
24 @param request reference to the request object (QNetworkRequest)
25 @param fileData reference to the data buffer (QByteArray)
26 @param mimeType for the reply (string)
27 @param parent reference to the parent object (QObject)
28 """
29 super(NetworkReply, self).__init__(parent)
30
31 self.__data = fileData
32
33 self.setRequest(request)
34 self.setOpenMode(QIODevice.ReadOnly)
35
36 self.setHeader(QNetworkRequest.ContentTypeHeader, mimeType)
37 self.setHeader(QNetworkRequest.ContentLengthHeader,
38 QByteArray.number(fileData.length()))
39 self.setAttribute(QNetworkRequest.HttpStatusCodeAttribute, 200)
40 self.setAttribute(QNetworkRequest.HttpReasonPhraseAttribute, "OK")
41 QTimer.singleShot(0, lambda: self.metaDataChanged.emit())
42 QTimer.singleShot(0, lambda: self.readyRead.emit())
43 QTimer.singleShot(0, lambda: self.finished.emit())
44
45 def abort(self):
46 """
47 Public slot to abort the operation.
48 """
49 # do nothing
50 pass
51
52 def bytesAvailable(self):
53 """
54 Public method to determined the bytes available for being read.
55
56 @return bytes available (integer)
57 """
58 return self.__data.length() + QNetworkReply.bytesAvailable(self)
59
60 def readData(self, maxlen):
61 """
62 Public method to retrieve data from the reply object.
63
64 @param maxlen maximum number of bytes to read (integer)
65 @return string containing the data (bytes)
66 """
67 len_ = min(maxlen, self.__data.length())
68 buffer = bytes(self.__data[:len_])
69 self.__data.remove(0, len_)
70 return buffer
71
72 def isFinished(self):
73 """
74 Public method to check, if the reply has finished.
75
76 @return flag indicating the finished state (boolean)
77 """
78 return True

eric ide

mercurial