def download(self, sql): apiUrl = "http://{}.cartodb.com/api/v2/sql?api_key={}&format=spatialite&q={}".format( self.cartodbUser, self.apiKey, sql ) url = QUrl(apiUrl) request = self._getRequest(url) def finished(reply): tempdir = tempfile.tempdir if tempdir is None: tempdir = tempfile.mkdtemp() tf = tempfile.NamedTemporaryFile(delete=False) sqlite = QFile(tf.name) tf.close() if sqlite.open(QIODevice.WriteOnly): sqlite.write(reply.readAll()) sqlite.close() self.fetchContent.emit(tf.name) else: self.error.emit("Error saving downloaded file") manager = QNetworkAccessManager() manager.finished.connect(finished) reply = manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def __sync_request(self, url): _url = QUrl(url) _request = QNetworkRequest(_url) self.__replies.append(_request) QgsNetworkAccessManager.instance().sslErrors.connect( self.__supress_ssl_errors) _reply = QgsNetworkAccessManager.instance().get(_request) # wait loop = QEventLoop() _reply.finished.connect(loop.quit) loop.exec_() _reply.finished.disconnect(loop.quit) QgsNetworkAccessManager.instance().sslErrors.disconnect( self.__supress_ssl_errors) loop = None error = _reply.error() if error != QNetworkReply.NoError: raise Exception(error) result_code = _reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) result = _reply.readAll() self.__replies.append(_reply) _reply.deleteLater() if result_code in [301, 302, 307]: redirect_url = _reply.attribute( QNetworkRequest.RedirectionTargetAttribute) return self.__sync_request(redirect_url) else: return result
def getUserTables(self, page=1, per_page=20, shared="yes", returnDict=True): self.returnDict = returnDict payload = { "tag_name": "", "q": "", "page": page, "type": "", "exclude_shared": "false", "per_page": per_page, "tags": "", "shared": shared, "locked": "false", "only_liked": "false", "order": "name", "types": "table", } url = QUrl(self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def _processPendingEvents(): """Process pending application events.""" # Create an event loop to run in. Otherwise, we need to use the # QApplication main loop, which may already be running and therefore # unusable. qe = QEventLoop() # Create a single-shot timer. Could use QTimer.singleShot(), # but can't cancel this / disconnect it. timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(qe.quit) timer.start(1) # Wait for an emitted signal. qe.exec_() # Clean up: don't allow the timer to call qe.quit after this # function exits, which would produce "interesting" behavior. timer.stop() # Stopping the timer may not cancel timeout signals in the # event queue. Disconnect the signal to be sure that loop # will never receive a timeout after the function exits. timer.timeout.disconnect(qe.quit)
def __sync_request(self, url): _url = QUrl(url) _request = QNetworkRequest(_url) self.__replies.append(_request) QgsNetworkAccessManager.instance().sslErrors.connect(self.__supress_ssl_errors) _reply = QgsNetworkAccessManager.instance().get(_request) # wait loop = QEventLoop() _reply.finished.connect(loop.quit) loop.exec_() _reply.finished.disconnect(loop.quit) QgsNetworkAccessManager.instance().sslErrors.disconnect(self.__supress_ssl_errors) loop = None error = _reply.error() if error != QNetworkReply.NoError: raise Exception(error) result_code = _reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) result = _reply.readAll() self.__replies.append(_reply) _reply.deleteLater() if result_code in [301, 302, 307]: redirect_url = _reply.attribute(QNetworkRequest.RedirectionTargetAttribute) return self.__sync_request(redirect_url) else: return result
def quit(self): loop = QEventLoop() reply = self._make_request('quit') reply.finished.connect(loop.quit) loop.exec_() QThread.quit(self)
def getUserTables(self, page=1, per_page=20, shared='yes', returnDict=True): self.returnDict = returnDict payload = { 'tag_name': '', 'q': '', 'page': page, 'type': '', 'exclude_shared': 'false', 'per_page': per_page, 'tags': '', 'shared': shared, 'locked': 'false', 'only_liked': 'false', 'order': 'name', 'types': 'table' } url = QUrl( self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def get(self, url=None, script=None, key=None): """Load given url in webkit and return html when loaded """ self.base_url = self.base_url or url # set base URL if not set html = self.cache.get(key) if html: if self.debug: print 'load cache', key self.setHtml(html, QUrl(self.base_url)) elif url: self.load(QUrl(url)) elif script: self.js(script) loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) timer.start(self.timeout * 1000) loop.exec_() # delay here until download finished or timeout if timer.isActive(): # downloaded successfully timer.stop() html = self.current_html() if key: self.cache[key] = html self.inject_jquery() else: # didn't download in time print 'Download timeout' html = '' return html
def cbUserData(self, data): if 'error' in data: # TODO Create image for error self.nameLB.setText( "<html><head/><body><p><span style=\" text-decoration: underline; color:red;\">error</span></p></body></html>" ) self.error.emit(data['error']) return self.currentUserData = data self.settings.setValue('/CartoDBPlugin/selected', self.currentUser) manager = QNetworkAccessManager() manager.finished.connect(self.returnAvatar) if 's3.amazonaws.com' in data['avatar_url']: imageUrl = QUrl(data['avatar_url']) else: imageUrl = QUrl('https:' + data['avatar_url']) request = QNetworkRequest(imageUrl) request.setRawHeader('User-Agent', 'QGIS 2.x') reply = manager.get(request) loop = QEventLoop() reply.finished.connect(loop.exit) loop.exec_()
def getUserTables(self, page=1, per_page=20, shared='yes', returnDict=True): self.returnDict = returnDict payload = { 'tag_name': '', 'q': '', 'page': page, 'type': '', 'exclude_shared': 'false', 'per_page': per_page, 'tags': '', 'shared': shared, 'locked': 'false', 'only_liked': 'false', 'order': 'name', 'types': 'table' } url = QUrl(self.apiUrl + "viz?api_key={}&{}".format(self.apiKey, urllib.urlencode(payload))) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def OeQ_wait_for_file(filepath,timeout=10000): """Block loop until signal emitted, or timeout (ms) elapses.""" from PyQt4.QtCore import QEventLoop,QTimer from os.path import isfile import platform import os if platform.system() == 'Darwin': return True if isfile(filepath): return True loop = QEventLoop() timer=QTimer() file_result = [True] # def check_file(testpath): if isfile(testpath): timer.stop() loop.quit() # timer.timeout.connect(lambda: check_file(filepath)) timer.setSingleShot(False) timer.start(500) # def timed_out(file_result_flag): file_result_flag[0]=False loop.quit() # if timeout >500: timer.singleShot(timeout,lambda: timed_out(file_result)) loop.exec_() return file_result[0]
def f2(): future = ac.start(em2.g, lambda x: x, QThread.currentThread()) # The doneSignal won't be processed without an event loop. A # thread pool doesn't create one, so make our own to run ``g``. qe = QEventLoop() future._signalInvoker.doneSignal.connect(qe.exit) qe.exec_()
def download(self, sql): apiUrl = 'http://{}.{}/api/v2/sql?api_key={}&format=spatialite&q={}'.format( self.cartodbUser, self.hostname, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) def finished(reply): tempdir = tempfile.tempdir if tempdir is None: tempdir = tempfile.mkdtemp() tf = tempfile.NamedTemporaryFile(delete=False) sqlite = QFile(tf.name) tf.close() if (sqlite.open(QIODevice.WriteOnly)): sqlite.write(reply.readAll()) sqlite.close() self.fetchContent.emit(tf.name) else: self.error.emit('Error saving downloaded file') manager = QNetworkAccessManager() manager.finished.connect(finished) reply = manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def OeQ_wait_for_renderer(timeout=10000): """Block loop until signal emitted, or timeout (ms) elapses.""" from PyQt4.QtCore import QEventLoop,QTimer #print 'wait for renderer...', # function call if action timed out def timed_out(render_result_flag): render_result_flag[0]=False loop.quit() #define loop loop = QEventLoop() #define Timer timer=QTimer() #define render_result render_result = [True] OeQ_wait(2) #check wether Canvas is drawing if iface.mapCanvas().isDrawing(): #connect mapCanvasRefreshed and renderComplete events to quit loop iface.mapCanvas().renderComplete.connect(loop.quit) iface.mapCanvas().mapCanvasRefreshed.connect(loop.quit) # if a timeout is defined and other than 0 if bool(timeout): # start timer timer.singleShot(timeout,lambda: timed_out(render_result)) #and jump into the loop loop.exec_() #disconnect after loop has quit iface.mapCanvas().mapCanvasRefreshed.disconnect(loop.quit) iface.mapCanvas().renderComplete.disconnect(loop.quit) #print 'Done' return render_result[0]
def waitForSignal(signal, message="", timeout=0): """Waits (max timeout msecs if given) for a signal to be emitted. It the waiting lasts more than 2 seconds, a progress dialog is displayed with the message. Returns True if the signal was emitted. Return False if the wait timed out or the dialog was canceled by the user. """ loop = QEventLoop() dlg = QProgressDialog(minimum=0, maximum=0, labelText=message) dlg.setWindowTitle(appinfo.appname) dlg.setWindowModality(Qt.ApplicationModal) QTimer.singleShot(2000, dlg.show) dlg.canceled.connect(loop.quit) if timeout: QTimer.singleShot(timeout, dlg.cancel) stop = lambda: loop.quit() signal.connect(stop) loop.exec_() signal.disconnect(stop) dlg.hide() dlg.deleteLater() return not dlg.wasCanceled()
def waitForSignal(signal, message="", timeout=0): """Waits (max timeout msecs if given) for a signal to be emitted. It the waiting lasts more than 2 seconds, a progress dialog is displayed with the message. Returns True if the signal was emitted. Return False if the wait timed out or the dialog was canceled by the user. """ loop = QEventLoop() dlg = QProgressDialog(minimum=0, maximum=0, labelText=message) dlg.setWindowTitle(info.appname) dlg.setWindowModality(Qt.ApplicationModal) QTimer.singleShot(2000, dlg.show) dlg.canceled.connect(loop.quit) if timeout: QTimer.singleShot(timeout, dlg.cancel) stop = lambda: loop.quit() signal.connect(stop) loop.exec_() signal.disconnect(stop) dlg.hide() dlg.deleteLater() return not dlg.wasCanceled()
class CartoDBLayerWorker(QObject): finished = pyqtSignal(CartoDBLayer) error = pyqtSignal(Exception, basestring) def __init__(self, iface, tableName, owner=None, dlg=None, sql=None, filterByExtent=False, readonly=False, multiuser=False): QObject.__init__(self) self.iface = iface self.owner = owner self.tableName = tableName self.readonly = readonly self.multiuser = multiuser self.dlg = dlg self.sql = sql self.filterByExtent = filterByExtent self.loop = None def load(self): worker = CartoDBPluginWorker(self, 'loadLayer') worker.error.connect(self.workerError) self.loop = QEventLoop() worker.finished.connect(self.workerFinished) worker.start() self.loop.exec_() @pyqtSlot(str) def _loadData(self, spatiaLite): layer = CartoDBLayer(self.iface, self.tableName, self.dlg.currentUser, self.dlg.currentApiKey, self.owner, self.sql, spatiaLite=spatiaLite, readonly=self.readonly, multiuser=self.multiuser) self.finished.emit(layer) @pyqtSlot() def loadLayer(self): if self.sql is None: sql = 'SELECT * FROM ' + ((self.owner + '.') if self.owner != self.dlg.currentUser else '') + self.tableName if self.filterByExtent: extent = self.iface.mapCanvas().extent() sql = sql + " WHERE ST_Intersects(ST_GeometryFromText('{}', 4326), the_geom)".format(extent.asWktPolygon()) else: sql = self.sql cartoDBApi = CartoDBApi(self.dlg.currentUser, self.dlg.currentApiKey) cartoDBApi.fetchContent.connect(self._loadData) cartoDBApi.download(sql) # cartoDBApi.getDataFromTable(sql, False) # geoJSON = self._loadData() """ On worker has finished """ def workerFinished(self, ret): QgsMessageLog.logMessage('Task finished:\n' + str(ret), 'CartoDB Plugin', QgsMessageLog.INFO) self.loop.exit() """ On worker error """ def workerError(self, e, exception_string): QgsMessageLog.logMessage('Worker thread raised an exception: {} - {}\n'.format(exception_string, str(e)), 'CartoDB Plugin', QgsMessageLog.CRITICAL)
def DownLoadOverQT (dlURL, LokFileName): # QGIS3: funktioniert unter QGIS 3.x recht zuverlässig auch auf "eingeschränktem Rechner" # - nutzt die Proxyeinstellungen von QGIS # - funktioniert in QGIS selbst auch über HTTPS (es wird durch QGIS eiun Abfragefenster geöffnet) # - bei extrem großen Dateien (z.B. 500MBYte) crasht es bei ReadAll() # QGIS2: funktioniert unter QGIS 2.x innerhalb von QGIS aktuell recht zuverlässig auch auf "eingeschränktem Rechner" # außerhalb hängt sich der Code auf "eingeschräktem Rechner" auf und bringt dann auch kein Ergebnis # Normalrechner funktioniert es def WriteFile(LokFileName, content): # 1. Ziel löschen, wenn existent if path.exists(LokFileName): remove (LokFileName) out=open(LokFileName,'wb') out.write(content) out.close() def onfinish(): WriteFile(LokFileName,reply.readAll()); loop.quit() # 2. Download request = QNetworkRequest() request.setUrl(QUrl(dlURL)) manager = QgsNetworkAccessManager.instance() reply = manager.get(request) reply.setParent(None) loop = QEventLoop() reply.finished.connect(onfinish) loop.exec_() # Wiederholung bei redirekt (13.08.18 Thüringen leitet an HTTPS weiter) status=reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) if (status==301): redirectUrl = reply.attribute(request.RedirectionTargetAttribute) request = QNetworkRequest() request.setUrl(redirectUrl) manager = QgsNetworkAccessManager.instance() reply = manager.get(request) reply.setParent(None) loop = QEventLoop() reply.finished.connect(onfinish) loop.exec_() if path.exists(LokFileName): return path.getsize(LokFileName), reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) else: return None, reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) reply.deleteLater()
def exec_(self): self.show() e = QEventLoop() self.eventLoop = e e.exec_() self.eventLoop = None
def _createWebAppCompiled(n): from pubsub import pub def endWriteWebAppListener(success, reason): from pubsub import pub pub.unsubscribe(endWriteWebAppListener, topics.endWriteWebApp) loop.exit() loop = QEventLoop() pub.subscribe(endWriteWebAppListener , topics.endWriteWebApp) _createWebApp(n, False) loop.exec_(flags = QEventLoop.ExcludeUserInputEvents)
def quit(self): if self.io_conn and self.io_conn.state() == QTcpSocket.ConnectedState: loop = QEventLoop() self.io_conn.disconnected.connect(loop.quit) self.io_conn.disconnectFromHost() self.log.debug("Entered disconnect state...") if self.io_conn.state() == QTcpSocket.ConnectedState: loop.exec_() self.log.info("Done") Connection.quit(self)
def request(self, url): ''' prepare the request and return the result of the reply ''' request = QNetworkRequest(QUrl(url)) reply = self.manager.get(request) reply.deleteLater() evloop = QEventLoop() reply.finished.connect(evloop.quit) evloop.exec_(QEventLoop.ExcludeUserInputEvents) return unicode(reply.readAll())
class _RunnableAsyncCall(_RunnableTask): def run(self): self.eventLoop = QEventLoop() self.eventLoop.processEvents() QObject.connect(self._call, SIGNAL("finished(QString)"), lambda str: self.eventLoop.quit()) QMetaObject.invokeMethod( self._call, "moveToAndInit", Qt.QueuedConnection, Q_ARG("PyQt_PyObject", QThread.currentThread())) self.eventLoop.processEvents() self.eventLoop.exec_()
def updateViz(self, viz, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "viz/{}?api_key={}".format(viz["id"], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(viz)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def checkUploadStatus(self, id, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "imports/{}/?api_key={}".format(id, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
class Thumbnailer(QObject): compatibleType = re.compile( "(JPEG|JPG|jpg|jpeg|GIF|gif|bmp|BMP|png|PNG|pbm|PBM|pgm|PGM|ppm|PPM|xpm|XPM|xbm|XBM|TIFF|tiff|video).*", re.IGNORECASE) def __init__(self): QObject.__init__(self) self.thumbnailManager = ThumbnailManager() self.thumbnailManager.register(self) self.requests = set() @staticmethod def isThumbnailable(node): if Thumbnailer.compatibleType.search(str(node.dataType())) != None: return True return False def generate(self, node, iconSize=128, percent=10, frames=1, blocking=False): if blocking: self.blockingLoop = QEventLoop() self.blockingLoop.connect(self, SIGNAL("ThumbnailUpdate"), self.blockingUpdate) config = ScaleConfig(node, iconSize, percent, frames) self.requests.add(config) pixmap = self.thumbnailManager.generate(config) if pixmap: return pixmap if blocking: self.blockingLoop.exec_() return self.pixmap def blockingUpdate(self, node, pixmap): self.pixmap = pixmap self.blockingLoop.quit() def requestRemove(self, config): try: self.requests.remove(config) except: pass def request(self, config): if config in self.requests: return True else: return False def unregister(self): self.thumbnailManager.unregister(self)
def getUserDetails(self, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "users/{}/?api_key={}".format(self.cartodbUser, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def _wait_signal(signal, timeout=None): """Block loop until signal emitted, or timeout (ms) elapses.""" # http://jdreaver.com/posts/2014-07-03-waiting-for-signals-pyside-pyqt.html loop = QEventLoop() signal.connect(loop.quit) yield if timeout is not None: QTimer.singleShot(timeout, loop.quit) loop.exec_()
def getLayersMap(self, mapId, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers?api_key={}".format(mapId, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def updateLayerInMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers/{}?api_key={}".format(mapId, layer["id"], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def updateLayerInMap(self, mapId, layer, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "maps/{}/layers/{}?api_key={}".format( mapId, layer['id'], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(layer)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
class _RunnableAsyncCall(_RunnableTask): def run(self): self.eventLoop = QEventLoop() self.eventLoop.processEvents() QObject.connect(self._call, SIGNAL("finished(QString)"), lambda str: self.eventLoop.quit()) QMetaObject.invokeMethod(self._call, "moveToAndInit", Qt.QueuedConnection, Q_ARG("PyQt_PyObject", QThread.currentThread())) self.eventLoop.processEvents() self.eventLoop.exec_()
def getDataFromTable(self, sql, returnDict=True): self.returnDict = returnDict apiUrl = 'http://{}.cartodb.com/api/v2/sql?api_key={}&format=GeoJSON&q={}'.format(self.cartodbUser, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def updateViz(self, viz, returnDict=True): self.returnDict = returnDict url = QUrl(self.apiUrl + "viz/{}?api_key={}".format(viz['id'], self.apiKey)) request = self._getRequest(url) reply = self.manager.put(request, json.dumps(viz)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def get(self, url, html=None, headers=None, data=None): """Load given url in webkit and return html when loaded url: the URL to load html: optional HTML to set instead of downloading headers: the headers to attach to the request data: the data to POST """ if isinstance(url, basestring): # convert string to Qt's URL object url = QUrl(url) if html: # load pre downloaded HTML self.setContent(html, baseUrl=url) return html t1 = time() loop = QEventLoop() self.loadFinished.connect(loop.quit) # need to make network request request = QNetworkRequest(url) if headers: # add headers to request when defined for header, value in headers: request.setRawHeader(header, value) self.page().networkAccessManager().main_url = url request.setOriginatingObject(self) if data: # POST request super(Browser, self).load(request, QNetworkAccessManager.PostOperation, data) else: # GET request super(Browser, self).load(request) # set a timeout on the download loop timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) timer.start(self.timeout * 1000) loop.exec_() # delay here until download finished or timeout if timer.isActive(): # downloaded successfully timer.stop() parsed_html = self.current_html() self.wait(self.delay - (time() - t1)) else: # did not download in time common.logger.debug('Timed out: {}'.format(url.toString())) parsed_html = '' return parsed_html
def getDataFromTable(self, sql, returnDict=True): self.returnDict = returnDict apiUrl = 'http://{}.{}/api/v2/sql?api_key={}&format=GeoJSON&q={}'.format( self.cartodbUser, self.hostname, self.apiKey, sql) url = QUrl(apiUrl) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def getUserDetails(self, returnDict=True): self.returnDict = returnDict url = QUrl( self.apiUrl + "users/{}/?api_key={}".format(self.cartodbUser, self.apiKey)) request = self._getRequest(url) reply = self.manager.get(request) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def disconnect(self): if self.tcp_connection and self.tcp_connection.state( ) == QTcpSocket.ConnectedState: loop = QEventLoop() self.tcp_connection.disconnected.connect(loop.quit) self.tcp_connection.disconnectFromHost() self.log.debug("Entering disconnect state...") if self.tcp_connection.state() == QTcpSocket.ConnectedState: loop.exec_() self.log.debug("Done waiting, closing server...") if self.tcp_server.isListening(): self.tcp_server.close() self.log.info("Server closed")
def testServerSDKErrors(self): dst = os.path.join(tempFolderInTempFolder("webappbuilder"), "webapp") shutil.copytree( os.path.join(os.path.dirname(__file__), "data", "wrong_app"), dst) pub.subscribe(endAppSDKificationListener, utils.topics.endAppSDKification) try: global _loop _loop = QEventLoop() appSDKification(dst, Progress()) _loop.exec_(flags=QEventLoop.ExcludeUserInputEvents) self.assertTrue(_correctResponse) except Exception as e: self.fail(str(e))
def _download_qgis(packageUrl, handle): from qgis.core import QgsNetworkAccessManager request = QNetworkRequest(QUrl(packageUrl)) reply = QgsNetworkAccessManager.instance().get(request) evloop = QEventLoop() reply.finished.connect(evloop.quit) evloop.exec_(QEventLoop.ExcludeUserInputEvents) content_type = reply.rawHeader('Content-Type') if bytearray(content_type) == bytearray('text/plain; charset=utf-8'): handle.write(bytearray(reply.readAll())) else: ret_code = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) raise Exception('Failed to download %s\n\nThe HTTP status code was %d.\n\nPlease check your network settings' % (packageUrl, ret_code))
class Thumbnailer(QObject): compatibleType = re.compile( "(JPEG|JPG|jpg|jpeg|GIF|gif|bmp|BMP|png|PNG|pbm|PBM|pgm|PGM|ppm|PPM|xpm|XPM|xbm|XBM|TIFF|tiff|video).*", re.IGNORECASE, ) def __init__(self): QObject.__init__(self) self.thumbnailManager = ThumbnailManager() self.thumbnailManager.register(self) self.requests = set() @staticmethod def isThumbnailable(node): if Thumbnailer.compatibleType.search(str(node.dataType())) != None: return True return False def generate(self, node, iconSize=128, percent=10, frames=1, blocking=False): if blocking: self.blockingLoop = QEventLoop() self.blockingLoop.connect(self, SIGNAL("ThumbnailUpdate"), self.blockingUpdate) config = ScaleConfig(node, iconSize, percent, frames) self.requests.add(config) pixmap = self.thumbnailManager.generate(config) if pixmap: return pixmap if blocking: self.blockingLoop.exec_() return self.pixmap def blockingUpdate(self, node, pixmap): self.pixmap = pixmap self.blockingLoop.quit() def requestRemove(self, config): try: self.requests.remove(config) except: pass def request(self, config): if config in self.requests: return True else: return False def unregister(self): self.thumbnailManager.unregister(self)
def __init__(self, path): self.data = [] if os.path.exists(path): with open(path) as f: self.data = f.readlines() elif path.startswith("http"): url = QUrl(path) loop = QEventLoop() manager = QNetworkAccessManager() request = QNetworkRequest(url) reply = manager.get(request) reply.finished.connect(loop.exit) loop.exec_() if reply.error() == QNetworkReply.NoError: self.data = reply.readAll().data().decode('utf-8').split("\n")
def url2image(url, file_name=None): """Function at the specified url downloads the page and stores it QImage object and returns it. If you pass file_name, then the function will save the picture in the file. Функция по указанному url загружает страницу и сохраняет ее объект QImage и возвращает его. Если передать file_name, тогда функция сохранит в файл картинку. """ # Нужно создавать только один раз global qApp if qApp is None: qApp = QApplication([]) # Пришлось добавить из-за того, что картинки не прогружались load_pyqt4_plugins() # TODO: прятать вертикальный и горизонтальный ползунки # Загрузка url и ожидание ее view = QWebView() view.setPage(WebPage()) # view.show() view.load(QUrl(url)) loop = QEventLoop() view.loadFinished.connect(loop.quit) loop.exec_() # Запрашиваем через javascript размеры страницы сайта width = view.page().mainFrame().evaluateJavaScript( "window.document.body.scrollWidth") height = view.page().mainFrame().evaluateJavaScript( "window.document.body.scrollHeight") # Устанавливаем границы документа view.page().setViewportSize(QSize(width, height)) img = QImage(width, height, QImage.Format_ARGB32) painter = QPainter(img) painter.setRenderHint(QPainter.HighQualityAntialiasing) view.page().mainFrame().render(painter) painter.end() if file_name: img.save(file_name) # qApp.exec() return img
def begin_acquiring(self): if self.tcp_connection: self.log.info("Starting acquisition") self.started = True try: ser = serial.Serial(SERIAL_PORT, timeout=1) except serial.serialutil.SerialException: self.log.exception( "Could not open serial port, ending acquisition...") self.reception_finished.emit() return self.log.debug("Serial port open: {0}".format(ser.portstr)) datalogger = "DL{0}".format(self.dl) self.log.debug( "Sending '{0}' through serial port".format(datalogger)) ser.write(datalogger) # DEBUG #txt = "Acquiring SMIGOL data! (by {0})\n" #self.tcp_connection.write(txt.format(self.tcp_port)) start_time = time.time() buff = '' while not buff.endswith(DL_END_MARK): data = ser.read(BUFFER_SIZE) if data: #self.log.debug("Rx: {0}".format([c for c in data])) self.tcp_connection.write(data) buff = buff[-BUFFER_SIZE:] + data # wait loop = QEventLoop() QTimer.singleShot(0, loop.quit) loop.exec_() txt = "Acquisition finished ({0} seconds)" self.log.info(txt.format(time.time() - start_time)) # DEBUG #self.tcp_connection.write("Finished!\n".format(self.tcp_port)) else: self.log.error( "Tried to start acquiring before establishing a connection") self.reception_finished.emit()
def download_file(self, url, path_file): """ download function :param url: url path of file :param path_file: path to save file :return: """ self.file_install = path_file self.url = url loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(lambda: loop.exit(1)) timer.start(100000) # 10 second time-out # req = QNetworkRequest(QUrl('https://www.python.org/')) req = QNetworkRequest(QUrl(url)) result = self.manager.get(req) result.finished.connect(lambda: self.fin_req(loop, result)) self.print_('fetching request...', self.dbg) if loop.exec_() == 0: timer.stop() self.print_( '{} is received: {}'.format(os.path.basename(path_file), result.readAll().count()), self.dbg) else: self.print_('request timed-out')
def upload(self, filePath, returnDict=True): self.returnDict = returnDict file = QFile(filePath) file.open(QFile.ReadOnly) url = QUrl(self.apiUrl + "imports/?api_key={}".format(self.apiKey)) files = {"file": file} multipart = self._createMultipart(files=files) request = QNetworkRequest(url) request.setHeader(QNetworkRequest.ContentTypeHeader, "multipart/form-data; boundary=%s" % multipart.boundary()) request.setRawHeader("User-Agent", "QGISCartoDB 0.2.x") reply = self.manager.post(request, multipart) loop = QEventLoop() reply.uploadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def wait_signal(signal, timeout=1): """Block loop until signal emitted, or timeout (s) elapses.""" loop = QEventLoop() signal.connect(loop.quit) yield timed_out = [] if timeout is not None: def quit_with_error(): timed_out.append(1) loop.quit() QTimer.singleShot(timeout * 1000, quit_with_error) loop.exec_() if timed_out: assert False, "Timeout while waiting for %s" % signal
def fetchFiles(self, urls): self.logT("TileLayer.fetchFiles() starts") # create a QEventLoop object that belongs to the current thread (if ver. > 2.1, it is render thread) eventLoop = QEventLoop() self.logT("Create event loop: " + str(eventLoop)) # DEBUG QObject.connect(self, SIGNAL("allRepliesFinished()"), eventLoop.quit) # create a timer to watch whether rendering is stopped watchTimer = QTimer() watchTimer.timeout.connect(eventLoop.quit) # send a fetch request to the main thread self.emit(SIGNAL("fetchRequest(QStringList)"), urls) # wait for the fetch to finish tick = 0 interval = 500 timeoutTick = self.plugin.downloadTimeout * 1000 / interval watchTimer.start(interval) while tick < timeoutTick: # run event loop for 0.5 seconds at maximum eventLoop.exec_() if debug_mode: qDebug("watchTimerTick: %d" % tick) qDebug("unfinished downloads: %d" % self.downloader.unfinishedCount()) if self.downloader.unfinishedCount( ) == 0 or self.renderContext.renderingStopped(): break tick += 1 watchTimer.stop() if tick == timeoutTick and self.downloader.unfinishedCount() > 0: self.log("fetchFiles timeout") # self.showBarMessage("fetchFiles timeout", duration=5) #DEBUG self.downloader.abort() self.downloader.errorStatus = Downloader.TIMEOUT_ERROR files = self.downloader.fetchedFiles watchTimer.timeout.disconnect(eventLoop.quit) # QObject.disconnect(self, SIGNAL("allRepliesFinished()"), eventLoop.quit) self.logT("TileLayer.fetchFiles() ends") return files
def upload(self, filePath, returnDict=True): self.returnDict = returnDict file = QFile(filePath) file.open(QFile.ReadOnly) apiUrl = 'https://{}.cartodb.com/api/v1/imports/?api_key={}'.format(self.cartodbUser, self.apiKey) url = QUrl(apiUrl) files = {'file': file} multipart = self._createMultipart(files=files) request = QNetworkRequest(url) request.setHeader(QNetworkRequest.ContentTypeHeader, 'multipart/form-data; boundary=%s' % multipart.boundary()) request.setRawHeader('User-Agent', 'QGIS 2.x') reply = self.manager.post(request, multipart) loop = QEventLoop() reply.uploadProgress.connect(self.progressCB) reply.error.connect(self.error) reply.finished.connect(loop.exit) loop.exec_()
def get(self, url=None, script=None, num_retries=1, jquery=False): """Load given url in webkit and return html when loaded script is some javasript to exexute that will change the loaded page (eg form submission) num_retries is how many times to try downloading this URL or executing this script jquery is whether to inject JQuery into the document """ t1 = time() self.base_url = self.base_url or url # set base URL if not set #html = self.cache.get(key, {}).get('value') #if html: # self.debug('Load cache ' + key) # self.setHtml(html, QUrl(self.base_url)) #else: if 1: loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) if url: self.load(QUrl(url)) elif script: self.js(script) timer.start(self.timeout * 1000) loop.exec_() # delay here until download finished or timeout if timer.isActive(): # downloaded successfully timer.stop() parsed_html = self.current_html() #if key: # self.cache[key] = html self.wait(self.delay - (time() - t1)) else: # didn't download in time if num_retries > 0: common.logger.debug('Timeout - retrying') parsed_html = self.get(url, script=script, num_retries=num_retries - 1, jquery=jquery) else: common.logger.debug('Timed out') parsed_html = '' return parsed_html
def open(self, url, timeout=60): """Wait for download to complete and return result""" loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) self.load(QUrl(url)) timer.start(timeout * 1000) loop.exec_() # delay here until download finised if timer.isActive(): #downloaded successfully timer.stop() return self.html() else: #time out print('Request timed out:', url)
def fetchFiles(self, urls, renderContext): downloader = Downloader(None, self.maxConnections, self.cacheExpiry, self.userAgent) downloader.moveToThread(QgsApplication.instance().thread()) downloader.timer.moveToThread(QgsApplication.instance().thread()) self.logT("TileLayer.fetchFiles() starts") # create a QEventLoop object that belongs to the current worker thread eventLoop = QEventLoop() downloader.allRepliesFinished.connect(eventLoop.quit) if self.iface: # for download progress downloader.replyFinished.connect(self.networkReplyFinished) self.downloader = downloader # create a timer to watch whether rendering is stopped watchTimer = QTimer() watchTimer.timeout.connect(eventLoop.quit) # fetch files QMetaObject.invokeMethod(self.downloader, "fetchFilesAsync", Qt.QueuedConnection, Q_ARG(list, urls), Q_ARG(int, self.plugin.downloadTimeout)) # wait for the fetch to finish tick = 0 interval = 500 timeoutTick = self.plugin.downloadTimeout * 1000 / interval watchTimer.start(interval) while tick < timeoutTick: # run event loop for 0.5 seconds at maximum eventLoop.exec_() if downloader.unfinishedCount() == 0 or renderContext.renderingStopped(): break tick += 1 watchTimer.stop() if downloader.unfinishedCount() > 0: downloader.abort(False) if tick == timeoutTick: downloader.errorStatus = Downloader.TIMEOUT_ERROR self.log("fetchFiles(): timeout") # watchTimer.timeout.disconnect(eventLoop.quit) # downloader.allRepliesFinished.disconnect(eventLoop.quit) self.logT("TileLayer.fetchFiles() ends") return downloader.fetchedFiles
def get(self, url=None, html=None, script=None, num_retries=1, jquery=False): """Load given url in webkit and return html when loaded url: the URL to load html: optional HTML to set instead of downloading script: some javasript to exexute that will change the loaded page (eg form submission) num_retries: how many times to try downloading this URL or executing this script jquery: whether to inject JQuery library into the document """ t1 = time() self.base_url = self.base_url or url # set base URL if not set loop = QEventLoop() timer = QTimer() timer.setSingleShot(True) timer.timeout.connect(loop.quit) self.loadFinished.connect(loop.quit) if url: if html: self.setHtml(html, QUrl(url)) else: self.load(QUrl(url)) elif script: self.js(script) timer.start(self.timeout * 1000) loop.exec_() # delay here until download finished or timeout if timer.isActive(): # downloaded successfully timer.stop() parsed_html = self.current_html() self.wait(self.delay - (time() - t1)) else: # did not download in time if num_retries > 0: common.logger.debug('Timeout - retrying') parsed_html = self.get(url, script=script, num_retries=num_retries-1, jquery=jquery) else: common.logger.debug('Timed out') parsed_html = '' return parsed_html
def loadResource(self, type, name): ret = QVariant() name.setFragment(QString()) if type == QTextDocument.HtmlResource: loop = QEventLoop() self.connect(self.http, SIGNAL("done(bool)"), loop, SLOT("quit()")) self.http.get(name.toString()) loop.exec_(QEventLoop.AllEvents|QEventLoop.WaitForMoreEvents) data = QVariant(QString(self.html)) if data.toString().trimmed().isEmpty(): fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) else: fileName = QFileInfo( name.toLocalFile()).fileName() data = QTextBrowser.loadResource(self, type, QUrl(fileName)) return data
def fetchFiles(self, urls): self.logT("TileLayer.fetchFiles() starts") # create a QEventLoop object that belongs to the current thread (if ver. > 2.1, it is render thread) eventLoop = QEventLoop() self.logT("Create event loop: " + str(eventLoop)) # DEBUG QObject.connect(self, SIGNAL("allRepliesFinished()"), eventLoop.quit) # create a timer to watch whether rendering is stopped watchTimer = QTimer() watchTimer.timeout.connect(eventLoop.quit) # send a fetch request to the main thread self.emit(SIGNAL("fetchRequest(QStringList)"), urls) # wait for the fetch to finish tick = 0 interval = 500 timeoutTick = self.downloadTimeout / interval watchTimer.start(interval) while tick < timeoutTick: # run event loop for 0.5 seconds at maximum eventLoop.exec_() if debug_mode: qDebug("watchTimerTick: %d" % tick) qDebug("unfinished downloads: %d" % self.downloader.unfinishedCount()) if self.downloader.unfinishedCount() == 0 or self.renderContext.renderingStopped(): break tick += 1 watchTimer.stop() if tick == timeoutTick and self.downloader.unfinishedCount() > 0: self.log("fetchFiles timeout") # self.showBarMessage("fetchFiles timeout", duration=5) #DEBUG self.downloader.abort() self.downloader.errorStatus = Downloader.TIMEOUT_ERROR files = self.downloader.fetchedFiles watchTimer.timeout.disconnect(eventLoop.quit) # QObject.disconnect(self, SIGNAL("allRepliesFinished()"), eventLoop.quit) self.logT("TileLayer.fetchFiles() ends") return files
def createVizFromTable(self, table, name, description="", returnDict=True): self.returnDict = returnDict payload = { "type": "derived", "name": name, "title": name, "description": description, "tags": ["QGISCartoDB"], "tables": [table], } url = QUrl(self.apiUrl + "viz/?api_key={}".format(self.apiKey)) request = self._getRequest(url) reply = self.manager.post(request, json.dumps(payload)) loop = QEventLoop() reply.downloadProgress.connect(self.progressCB) reply.error.connect(self._error) reply.finished.connect(loop.exit) loop.exec_()
def test_access_without_credentials(self): loop = QEventLoop() proxy = get_network_proxy() manager = QNetworkAccessManager() manager.setProxy(proxy) manager.finished.connect(loop.exit) reply = manager.get(QNetworkRequest(QUrl('http://aws.amazon.com/'))) loop.exec_(flags=QEventLoop.ExcludeUserInputEvents) if reply.isFinished(): self.assertEquals(self.server.log.getvalue(), '407 Proxy Authentication Required\n\n') else: if reply.isRunning(): self.failUnless(False, msg='The request has timed out.') else: self.failUnless(False, msg='A Network error occurred.')