eric6/WebBrowser/GreaseMonkey/GreaseMonkeyDownloader.py

Tue, 02 Mar 2021 17:17:09 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 02 Mar 2021 17:17:09 +0100
changeset 8143
2c730d5fd177
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.

1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7836
diff changeset
3 # Copyright (c) 2012 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the downloader for GreaseMonkey scripts.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QSettings, QFileInfo
4902
7a49807e493c Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4886
diff changeset
13 from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
15 from WebBrowser.WebBrowserWindow import WebBrowserWindow
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 class GreaseMonkeyDownloader(QObject):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Class implementing the downloader for GreaseMonkey scripts.
5726
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5715
diff changeset
21
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5715
diff changeset
22 @signal finished(fileName) emitted to indicate the end of a script download
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5715
diff changeset
23 (str)
e1dbd217214a Fixed a few source docu issues (forgotten signals and exceptions).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5715
diff changeset
24 @signal error() emitted to indicate a script download error
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
26 finished = pyqtSignal(str)
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
27 error = pyqtSignal()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
29 DownloadMainScript = 1
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
30 DownloadRequireScript = 2
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
31
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
32 def __init__(self, url, manager, mode):
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 Constructor
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
36 @param url URL to download script from
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
37 @type QUrl
3002
6ffc581f00f1 Continued to shorten the code lines to max. 79 characters.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2403
diff changeset
38 @param manager reference to the GreaseMonkey manager
4767
0bace7c5ebc9 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4763
diff changeset
39 @type GreaseMonkeyManager
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
40 @param mode download mode
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
41 @type int (one of DownloadMainScript, DownloadRequireScript)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 """
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2403
diff changeset
43 super(GreaseMonkeyDownloader, self).__init__()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 self.__manager = manager
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46
4902
7a49807e493c Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4886
diff changeset
47 self.__reply = WebBrowserWindow.networkManager().get(
7a49807e493c Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4886
diff changeset
48 QNetworkRequest(url))
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
49 if mode == GreaseMonkeyDownloader.DownloadMainScript:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
50 self.__reply.finished.connect(self.__scriptDownloaded)
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
51 else:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
52 self.__reply.finished.connect(self.__requireDownloaded)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 self.__fileName = ""
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
56 def updateScript(self, fileName):
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
57 """
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
58 Public method to set the file name for the script to be downloaded.
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
59
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
60 @param fileName file name for the script
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
61 @type str
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
62 """
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
63 self.__fileName = fileName
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
64
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
65 @pyqtSlot()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 def __scriptDownloaded(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 Private slot to handle the finished download of a script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
70 self.deleteLater()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
71 self.__reply.deleteLater()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
72
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
73 if self.__reply.error() != QNetworkReply.NetworkError.NoError:
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
74 self.error.emit()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 return
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 response = bytes(self.__reply.readAll()).decode()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
79 if "// ==UserScript==" not in response:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
80 self.error.emit()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
81 return
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
82
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
83 if not self.__fileName:
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
84 from WebBrowser.Tools import WebBrowserTools
3022
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
85 filePath = os.path.join(
57179e4cdadd Fixed a bunch of visible indentation issues.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3020
diff changeset
86 self.__manager.scriptsDirectory(),
4763
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
87 WebBrowserTools.getFileNameFromUrl(self.__reply.url()))
8ad353f31184 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
88 self.__fileName = WebBrowserTools.ensureUniqueFilename(filePath)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
90 try:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
91 with open(self.__fileName, "w", encoding="utf-8") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
92 f.write(response)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
93 except OSError:
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
94 self.error.emit()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
95 return
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
97 self.finished.emit(self.__fileName)
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
99 @pyqtSlot()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 def __requireDownloaded(self):
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 """
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 Private slot to handle the finished download of a required script.
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 """
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
104 self.deleteLater()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
105 self.__reply.deleteLater()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
106
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
107 if self.__reply.error() != QNetworkReply.NetworkError.NoError:
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
108 self.error.emit()
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 return
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 response = bytes(self.__reply.readAll()).decode()
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
113 if not response:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
114 self.error.emit()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
115 return
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
117 settings = QSettings(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
118 os.path.join(self.__manager.requireScriptsDirectory(),
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
119 "requires.ini"),
8143
2c730d5fd177 Changed the use of PyQt enums because the way they were used previously is deprecated since two years and replaced some deprecated Qt stuff.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7923
diff changeset
120 QSettings.Format.IniFormat)
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
121 settings.beginGroup("Files")
1953
26aa6fd94dc2 Added support for GreaseMonkey scripts.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
123 if not self.__fileName:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
124 self.__fileName = settings.value(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
125 self.__reply.request().url().toString())
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
126 if not self.__fileName:
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
127 name = (
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
128 QFileInfo(self.__reply.request().url().path())
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
129 .fileName()
7268
a28338eaf694 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
130 )
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
131 if not name:
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
132 name = "require.js"
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
133 elif not name.endswith(".js"):
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
134 name += ".js"
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
135 filePath = os.path.join(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
136 self.__manager.requireScriptsDirectory(), name)
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
137 from WebBrowser.Tools import WebBrowserTools
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
138 self.__fileName = WebBrowserTools.ensureUniqueFilename(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
139 filePath, "{0}")
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
140 if not QFileInfo(self.__fileName).isAbsolute():
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
141 self.__fileName = os.path.join(
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
142 self.__manager.requireScriptsDirectory(),
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
143 self.__fileName)
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
144
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
145 try:
7785
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
146 with open(self.__fileName, "w", encoding="utf-8") as f:
9978016560ec Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
147 f.write(response)
7836
2f0d208b8137 Changed code to not use the OSError aliases (IOError, EnvironmentError, socket.error and select.error) anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7785
diff changeset
148 except OSError:
5715
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
149 self.error.emit()
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
150 return
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
151
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
152 settings.setValue(self.__reply.request().url().toString(),
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
153 QFileInfo(self.__fileName).fileName())
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
154
cbcca230679f Updated the GreaseMonkey handler to better work with QWebEngine as of Qt 5.8.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5389
diff changeset
155 self.finished.emit(self.__fileName)

eric ide

mercurial