File: networkaccessmanager.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (165 lines) | stat: -rw-r--r-- 6,205 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# This file is part of the Frescobaldi project, http://www.frescobaldi.org/
#
# Copyright (c) 2008 - 2014 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
A QNetworkAccessManager subclass with easy registration of custom url schemes.
"""


from PyQt5.QtCore import QThread, QTimer, QUrl
from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest


class NetworkAccessManager(QNetworkAccessManager):
    """A QNetworkAccessManager subclass with easy registration of custom url schemes.
    
    Use the registerHandler() method to add custom scheme handlers.
    The registerHtmlHandler() method is used to add a simple callable as handler that
    gets a QUrl and should return the HTML as a normal string.
    
    The headers instance attribute is a dictionary (empty by default) containing
    extra headers (as key, value) that are added to outgoing requests.
    
    """
    
    def __init__(self, parent=None):
        QNetworkAccessManager.__init__(self, parent)
        self._dispatcher = {}
        self.headers = {}
        
    def createRequest(self, operation, request, data):
        try:
            requestFunc = self._dispatcher[request.url().scheme()]
        except KeyError:
            self.addHeadersToRequest(request)
            return QNetworkAccessManager.createRequest(self, operation, request, data)
        return requestFunc(self, operation, request, data)
    
    def addHeadersToRequest(self, request):
        """Called on outgoing requests and should add raw headers to the request.
        
        The default implementation of this method simply adds all the headers in the
        headers instance attribute.
        
        """
        for name, value in self.headers.items():
            request.setRawHeader(bytearray(name, "latin_1"), bytearray(value, "latin_1"))
    
    def registerHandler(self, scheme, handler):
        """Registers a handler for the given scheme.
        
        The handler is called with four arguments (manager, operation, request,
        data), just like the QNetworkAccessManager.createRequest method, and
        should return a QNetworkReply instance.
        
        """
        self._dispatcher[scheme] = handler
        
    def registerHtmlHandler(self, scheme, handler, threaded=False, encoding="UTF-8"):
        """Registers a simple callable as the handler for the given scheme.
        
        The handler only gets a GET URL (QUrl) and should return a HTML string.
        If threaded is True, the handler is called in a background thread.
        The encoding defaults to UTF-8 and is used for encoding the HTML returned
        by then handler and also set in the Content-Type header.
        
        """
        cls = ThreadedHtmlReply if threaded else HtmlReply
        def createRequest(mgr, operation, request, data):
            return cls(mgr, request.url(), handler, encoding)
        self._dispatcher[scheme] = createRequest

    def unregisterHandler(self, scheme):
        """Removes the special handling for the given scheme."""
        try:
            del self._dispatcher[scheme]
        except KeyError:
            pass


class HtmlReplyBase(QNetworkReply):
    """Abstract base class for a QNetworkReply that represents a generated HTML string."""
    def __init__(self, manager, url, handler, encoding="UTF-8"):
        QNetworkReply.__init__(self, manager)
        self.setUrl(url)
        self._handler = handler
        self._encoding = encoding
    
    def callHandler(self):
        self._content = self._handler(self.url()).encode(self._encoding)
        
    def outputReady(self):
        self._offset = 0
        self.setHeader(QNetworkRequest.ContentTypeHeader, "text/html; charset={0}".format(self._encoding))
        self.setHeader(QNetworkRequest.ContentLengthHeader, len(self._content))
        self.open(QNetworkReply.ReadOnly | QNetworkReply.Unbuffered)
    
    def emitSignals(self):
        self.readyRead.emit()
        self.finished.emit()
    
    def abort(self):
        pass
    
    def bytesAvailable(self):
        return len(self._content) - self._offset
    
    def isSequential(self):
        return True
    
    def readData(self, maxSize):
        if self._offset < len(self._content):
            end = min(self._offset + maxSize, len(self._content))
            data = self._content[self._offset:end]
            self._offset = end
            return data


class HtmlReply(HtmlReplyBase):
    """QNetworkReply that generates a HTML string by calling handler(url).encode(encoding)."""
    def __init__(self, manager, url, handler, encoding="UTF-8"):
        HtmlReplyBase.__init__(self, manager, url, handler, encoding)
        self.callHandler()
        self.outputReady()
        QTimer.singleShot(0, self.emitSignals)


class ThreadedHtmlReply(HtmlReplyBase):
    """HtmlReply that calls handler(url) in a background thread."""
    def __init__(self, manager, url, handler, encoding="UTF-8"):
        HtmlReplyBase.__init__(self, manager, url, handler, encoding)
        self._thread = Thread(self.callHandler)
        self._thread.finished.connect(self.threadFinished)
        self._thread.start()
    
    def threadFinished(self):
        self.outputReady()
        self.emitSignals()


class Thread(QThread):
    """QThread that runs a single callable."""
    def __init__(self, func):
        QThread.__init__(self)
        self._func = func
    
    def run(self):
        self._func()