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 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
# -*- coding: utf-8 -*-
# Copyright (c) 2012 - 2017 Detlev Offenbach <detlev@die-offenbachs.de>
#
"""
Module implementing the downloader for GreaseMonkey scripts.
"""
from __future__ import unicode_literals
import os
from PyQt5.QtCore import pyqtSignal, QObject, QSettings, QRegExp, QUrl
from PyQt5.QtWidgets import QDialog
from PyQt5.QtNetwork import QNetworkReply, QNetworkRequest
from E5Gui import E5MessageBox
from WebBrowser.WebBrowserWindow import WebBrowserWindow
class GreaseMonkeyDownloader(QObject):
"""
Class implementing the downloader for GreaseMonkey scripts.
"""
finished = pyqtSignal()
def __init__(self, url, manager):
"""
Constructor
@param url URL to download script from
@type QUrl
@param manager reference to the GreaseMonkey manager
@type GreaseMonkeyManager
"""
super(GreaseMonkeyDownloader, self).__init__()
self.__manager = manager
self.__reply = WebBrowserWindow.networkManager().get(
QNetworkRequest(url))
self.__reply.finished.connect(self.__scriptDownloaded)
self.__fileName = ""
self.__requireUrls = []
def __scriptDownloaded(self):
"""
Private slot to handle the finished download of a script.
"""
if self.sender() != self.__reply:
self.finished.emit()
return
response = bytes(self.__reply.readAll()).decode()
if self.__reply.error() == QNetworkReply.NoError and \
"// ==UserScript==" in response:
from WebBrowser.Tools import WebBrowserTools
filePath = os.path.join(
self.__manager.scriptsDirectory(),
WebBrowserTools.getFileNameFromUrl(self.__reply.url()))
self.__fileName = WebBrowserTools.ensureUniqueFilename(filePath)
try:
f = open(self.__fileName, "w", encoding="utf-8")
except (IOError, OSError) as err:
E5MessageBox.critical(
None,
self.tr("GreaseMonkey Download"),
self.tr(
"""<p>The file <b>{0}</b> could not be opened"""
""" for writing.<br/>Reason: {1}</p>""").format(
self.__fileName, str(err)))
self.finished.emit()
return
f.write(response)
f.close()
settings = QSettings(
os.path.join(self.__manager.requireScriptsDirectory(),
"requires.ini"),
QSettings.IniFormat)
settings.beginGroup("Files")
rx = QRegExp("@require(.*)\\n")
rx.setMinimal(True)
rx.indexIn(response)
for i in range(1, rx.captureCount() + 1):
url = rx.cap(i).strip()
if url and not settings.contains(url):
self.__requireUrls.append(QUrl(url))
self.__reply.deleteLater()
self.__reply = None
self.__downloadRequires()
def __requireDownloaded(self):
"""
Private slot to handle the finished download of a required script.
"""
if self.sender() != self.__reply:
self.finished.emit()
return
response = bytes(self.__reply.readAll()).decode()
if self.__reply.error() == QNetworkReply.NoError and response:
from WebBrowser.Tools import WebBrowserTools
filePath = os.path.join(self.__manager.requireScriptsDirectory(),
"require.js")
fileName = WebBrowserTools.ensureUniqueFilename(filePath, "{0}")
try:
f = open(fileName, "w", encoding="utf-8")
except (IOError, OSError) as err:
E5MessageBox.critical(
None,
self.tr("GreaseMonkey Download"),
self.tr(
"""<p>The file <b>{0}</b> could not be opened"""
""" for writing.<br/>Reason: {1}</p>""").format(
fileName, str(err)))
self.finished.emit()
return
f.write(response)
f.close()
settings = QSettings(
os.path.join(self.__manager.requireScriptsDirectory(),
"requires.ini"),
QSettings.IniFormat)
settings.beginGroup("Files")
settings.setValue(self.__reply.originalUrl().toString(), fileName)
self.__reply.deleteLater()
self.__reply = None
self.__downloadRequires()
def __downloadRequires(self):
"""
Private slot to initiate the download of required scripts.
"""
if self.__requireUrls:
self.__reply = WebBrowserWindow.networkManager().get(
QNetworkRequest(self.__requireUrls.pop(0)))
self.__reply.finished.connect(self.__requireDownloaded)
else:
from .GreaseMonkeyScript import GreaseMonkeyScript
deleteScript = True
script = GreaseMonkeyScript(self.__manager, self.__fileName)
if script.isValid():
if not self.__manager.containsScript(script.fullName()):
from .GreaseMonkeyAddScriptDialog import \
GreaseMonkeyAddScriptDialog
dlg = GreaseMonkeyAddScriptDialog(self.__manager, script)
deleteScript = dlg.exec_() != QDialog.Accepted
else:
E5MessageBox.information(
None,
self.tr("GreaseMonkey Download"),
self.tr(
"""<p><b>{0}</b> is already installed.</p>""")
.format(script.name()))
if deleteScript:
try:
os.remove(self.__fileName)
except (IOError, OSError):
# ignore
pass
self.finished.emit()
|