def setInfo(self, msg, error=False): if error: self.txtLog.append('<span style="color:red"><br>%s<br></span>' % msg) else: self.txtLog.append(msg) QCoreApplication.processEvents()
def grabHTTP(self, url, loadFunction, arguments=None): """Grab distant content via QGIS internal classes and QtNetwork.""" QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) request = QUrl(url) reply = self.manager.get(QNetworkRequest(request)) if arguments: reply.finished.connect(partial(loadFunction, reply, arguments)) else: reply.finished.connect(partial(loadFunction, reply)) while not reply.isFinished(): QCoreApplication.processEvents()
def write(self, m): if self.style == "_traceback": # Show errors in red stderrColor = QColor(self.sO.settings.value("pythonConsole/stderrFontColor", QColor(Qt.red))) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETFORE, 0o01, stderrColor) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETITALIC, 0o01, True) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETBOLD, 0o01, True) pos = self.sO.SendScintilla(QsciScintilla.SCI_GETCURRENTPOS) self.sO.SendScintilla(QsciScintilla.SCI_STARTSTYLING, pos, 31) self.sO.append(m) self.sO.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 0o01) else: self.sO.append(m) if self.out: self.out.write(m) self.move_cursor_to_end() if self.style != "_traceback": QCoreApplication.processEvents()
def okPressed(self): toDownload = [] for i in xrange(self.toupdateItem.childCount()): item = self.toupdateItem.child(i) if item.checkState(0) == Qt.Checked: toDownload.append(item.filename) for i in xrange(self.notinstalledItem.childCount()): item = self.notinstalledItem.child(i) if item.checkState(0) == Qt.Checked: toDownload.append(item.filename) if toDownload: self.progressBar.setMaximum(len(toDownload) * 2) for i, filename in enumerate(toDownload): QCoreApplication.processEvents() url = self.urlBase + filename.replace(' ', '%20') self.grabHTTP(url, self.storeFile, filename) url += '.help' self.grabHTTP(url, self.storeFile, filename + '.help') toDelete = [] for i in xrange(self.uptodateItem.childCount()): item = self.uptodateItem.child(i) if item.checkState(0) == Qt.Unchecked: toDelete.append(item.filename) # Remove py and help files if they exist for filename in toDelete: for pathname in (filename, filename + u".help"): path = os.path.join(self.folder, pathname) if os.path.exists(path): os.remove(path) self.updateToolbox = len(toDownload) + len(toDelete) > 0 super(GetScriptsAndModelsDialog, self).accept()
def write(self, m): if self.style == "_traceback": # Show errors in red stderrColor = QColor( self.sO.settings.value("pythonConsole/stderrFontColor", QColor(Qt.red))) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETFORE, 0o01, stderrColor) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETITALIC, 0o01, True) self.sO.SendScintilla(QsciScintilla.SCI_STYLESETBOLD, 0o01, True) pos = self.sO.SendScintilla(QsciScintilla.SCI_GETCURRENTPOS) self.sO.SendScintilla(QsciScintilla.SCI_STARTSTYLING, pos, 31) self.sO.append(m) self.sO.SendScintilla(QsciScintilla.SCI_SETSTYLING, len(m), 0o01) else: self.sO.append(m) if self.out: self.out.write(m) self.move_cursor_to_end() if self.style != "_traceback": QCoreApplication.processEvents()
def setCommand(self, cmd): if self.showDebug: self.setInfo('<code>%s<code>' % cmd) QCoreApplication.processEvents()
def setText(self, text): self.lblProgress.setText(text) self.setInfo(text, False) QCoreApplication.processEvents()
def setPercentage(self, value): if self.progressBar.maximum() == 0: self.progressBar.setMaximum(100) self.progressBar.setValue(value) QCoreApplication.processEvents()
class TestQgsNetworkContentFetcher(unittest.TestCase): @classmethod def setUpClass(cls): # Bring up a simple HTTP server os.chdir(unitTestDataPath() + '') handler = SimpleHTTPServer.SimpleHTTPRequestHandler cls.httpd = SocketServer.TCPServer(('localhost', 0), handler) cls.port = cls.httpd.server_address[1] cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever) cls.httpd_thread.setDaemon(True) cls.httpd_thread.start() def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) self.loaded = False self.app = QCoreApplication([]) def contentLoaded(self): self.loaded = True def testFetchEmptyUrl(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl()) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() != QNetworkReply.NoError def testFetchBadUrl(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://x')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() != QNetworkReply.NoError def testFetchUrlContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html def testDoubleFetch(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://www.qgis.org/')) # double fetch - this should happen before previous request finishes fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html def testFetchEncodedContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/encoded_html.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert unichr(6040) in html
def setDebugInfo(self, msg): if self.showDebug: self.setInfo('<span style="color:blue">%s</span>' % msg) QCoreApplication.processEvents()
def setConsoleInfo(self, msg): if self.showDebug: self.setCommand('<span style="color:darkgray">%s</span>' % msg) QCoreApplication.processEvents()
class TestQgsNetworkContentFetcher(unittest.TestCase): @classmethod def setUpClass(cls): # Bring up a simple HTTP server os.chdir(unitTestDataPath() + '') handler = http.server.SimpleHTTPRequestHandler cls.httpd = socketserver.TCPServer(('localhost', 0), handler) cls.port = cls.httpd.server_address[1] cls.httpd_thread = threading.Thread(target=cls.httpd.serve_forever) cls.httpd_thread.setDaemon(True) cls.httpd_thread.start() def __init__(self, methodName): """Run once on class initialization.""" unittest.TestCase.__init__(self, methodName) self.loaded = False self.app = QCoreApplication([]) def contentLoaded(self): self.loaded = True def testFetchEmptyUrl(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl()) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() != QNetworkReply.NoError def testFetchBadUrl(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://x')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() != QNetworkReply.NoError def testFetchUrlContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html def testDoubleFetch(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://www.qgis.org/')) # double fetch - this should happen before previous request finishes fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/qgis_local_server/index.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert 'QGIS' in html def testFetchEncodedContent(self): fetcher = QgsNetworkContentFetcher() self.loaded = False fetcher.fetchContent(QUrl('http://localhost:' + str(TestQgsNetworkContentFetcher.port) + '/encoded_html.html')) fetcher.finished.connect(self.contentLoaded) while not self.loaded: self.app.processEvents() r = fetcher.reply() assert r.error() == QNetworkReply.NoError, r.error() html = fetcher.contentAsString() assert unichr(6040) in html