def removeCert(certFile): certFile = certFile.replace("'", "") file = QFile(certFile) # set permission to allow removing on Win. # On linux and Mac if file is set with QFile::>ReadUser # does not create problem removing certs if not file.setPermissions(QFile.WriteOwner): raise Exception( 'Cannot change permissions on {}: error code: {}'.format( file.fileName(), file.error())) if not file.remove(): raise Exception('Cannot remove {}: error code: {}'.format( file.fileName(), file.error()))
def removeCert(certFile): certFile = certFile.replace("'", "") file = QFile(certFile) # set permission to allow removing on Win. # On linux and Mac if file is set with QFile::>ReadUser # does not create problem removing certs if not file.setPermissions(QFile.WriteOwner): raise Exception('Cannot change permissions on {}: error code: {}'.format(file.fileName(), file.error())) if not file.remove(): raise Exception('Cannot remove {}: error code: {}'.format(file.fileName(), file.error()))
def requestFinished(self): if self.reply.error() != QNetworkReply.NoError: QApplication.restoreOverrideCursor() iface.messageBar().pushMessage( "Lessons could not be installed:\n", self.reply.errorString(), QgsMessageBar.WARNING) self.reply.deleteLater() return f = QFile(tempFilenameInTempFolder(os.path.basename(self.url).split(".")[0])) f.open(QFile.WriteOnly) f.write(self.reply.readAll()) f.close() self.reply.deleteLater() from lessons import installLessonsFromZipFile installLessonsFromZipFile(f.fileName()) QApplication.restoreOverrideCursor() iface.messageBar().pushMessage( "Completed", "Lessons were correctly installed", QgsMessageBar.INFO)
class InstanceUUIDExtractor(): """ Class constructor """ def __init__(self, path): """ Initatlize class variables """ self.file_path = path self.file = None self.new_list = [] self.doc = QDomDocument() self.node = QDomNode() def set_file_path(self, path): """ Update the path based on the new file being read :param path: :return: """ self.file_path = path def unset_path(self): """Clear the current document path""" self.file_path = None def set_document(self): """ :return: """ self.file = QFile(self.file_path) if self.file.open(QIODevice.ReadOnly): self.doc.setContent(self.file) self.file.close() def update_document(self): '''Update the current instance by clearing the document in the cache ''' self.doc.clear() self.set_document() def on_file_passed(self): """ Pass the raw file to an xml document object and format the its filename to GeoODK standards :return: """ try: self.set_document() self.read_uuid_element() self.doc.clear() self.file.close() self.rename_file() except: pass def read_uuid_element(self): """ get the uuid element and text from the xml document from the mobile divice """ node = self.doc.elementsByTagName("meta") for i in range(node.count()): node = node.item(i).firstChild().toElement() self.node = node.text() return self.node def document_entities(self, profile): """ Get entities in the document :return: """ self.set_document() node_list = [] nodes = self.doc.elementsByTagName(profile) node = nodes.item(0).childNodes() if node: for j in range(node.count()): node_val = node.item(j) node_list.append(node_val.nodeName()) return node_list def profile_entity_nodes(self, profile): ''' Fetch and return QDomNodeList for entities of a profile :rtype: QDomNodeList ''' self.set_document() nodes = self.doc.elementsByTagName(profile) return nodes.item(0).childNodes() def document_entities_with_data(self, profile, selected_entities): """ Get entities in the dom document matching user selected entities :rtype: OrderedDict """ instance_data = OrderedDict() self.set_document() nodes = self.doc.elementsByTagName(profile) entity_nodes = nodes.item(0).childNodes() for attrs in range(entity_nodes.count()): if entity_nodes.item(attrs).nodeName() in selected_entities: name_entity = entity_nodes.item(attrs).nodeName() attr_nodes = self.doc.elementsByTagName(name_entity) instance_data[attr_nodes] = name_entity return instance_data def attribute_data_from_nodelist(self, args_list): """ process nodelist data before Importing attribute data into db """ repeat_instance_data = OrderedDict() attribute_data = OrderedDict() for attr_nodes, entity in args_list.items(): '''The assuption is that there are repeated entities from mobile sub forms. handle them separately''' if attr_nodes.count() > 1: for i in range(attr_nodes.count()): attrib_node = attr_nodes.at(i).childNodes() attr_list = OrderedDict() for j in range(attrib_node.count()): field_name = attrib_node.at(j).nodeName() field_value = attrib_node.at(j).toElement().text() attr_list[field_name] = field_value repeat_instance_data['{}'.format(i) + entity] = attr_list else: '''Entities must appear onces in the form''' node_list_var = OrderedDict() attr_node = attr_nodes.at(0).childNodes() for j in range(attr_node.count()): field_name = attr_node.at(j).nodeName() field_value = attr_node.at(j).toElement().text() node_list_var[field_name] = field_value attribute_data[entity] = node_list_var return attribute_data, repeat_instance_data def read_attribute_data_from_node(self, node, entity_name): """Read attribute data from a node item""" node_list_var = OrderedDict() attributes = OrderedDict() attr_node = node.at(0).childNodes() for j in range(attr_node.count()): field_name = attr_node.at(j).nodeName() field_value = attr_node.at(j).toElement().text() node_list_var[field_name] = field_value attributes[entity_name] = node_list_var return attributes def str_definition(self, instance=None): """ Check if the instance file has entry social tenure :return: """ if instance: self.set_file_path(instance) self.set_document() attributes = {} nodes = self.doc.elementsByTagName('social_tenure') entity_nodes = nodes.item(0).childNodes() if entity_nodes: for j in range(entity_nodes.count()): node_val = entity_nodes.item(j).toElement() attributes[node_val.nodeName()] = node_val.text().rstrip() return attributes def has_str_captured_in_instance(self, instance): """ Bool if the str inclusion is required based on whether is captured or not :return: """ count = len(self.str_definition(instance)) return True if count > 1 else False def entity_atrributes(self): """ Get collected data from the entity in the document :return: """ pass def uuid_element(self): """ Format the guuid from the file :return: """ return self.node.replace(":", "") def rename_file(self): """ Remane the existing instance file with Guuid from the mobile divice to conform with GeoODk naming convention :return: """ if isinstance(self.file, QFile): dir_n, file_n = os.path.split(self.file_path) os.chdir(dir_n) if not file_n.startswith(UUID): new_file_name = self.uuid_element() + ".xml" isrenamed = self.file.setFileName(new_file_name) os.rename(file_n, new_file_name) self.new_list.append(new_file_name) return isrenamed else: self.new_list.append(self.file.fileName()) self.file.close() else: return def file_list(self): """ check through the list of document to ensure they are complete file path """ complete_file = [] for fi in self.new_list: if os.path.isfile(fi): complete_file.append(fi) else: continue return complete_file def close_document(self): '''Close all the open documents and unset current paths''' self.file_path = None self.doc.clear() self.new_list = None
class MultipartFile(QFile): """Wrapper class to support reading a file in chunks and post each part separately.""" def __init__(self, filename): super().__init__(filename) self._file = QFile(filename) self._bytes_left = 0 self._file.readyRead.connect(self.on_readyRead) self._file.aboutToClose.connect(self.on_aboutToClose) self._file.bytesWritten.connect(self.on_bytesWritten) self._file.channelBytesWritten.connect(self.on_channelBytesWritten) self._file.channelReadyRead.connect(self.on_channelReadyRead) self._file.readChannelFinished.connect(self.on_readChannelFinished) def set_part_size(self, size): self._bytes_left = size self._original_size = size def commitTransaction(self): QgsMessageLog.logMessage('commitTransaction') return self._file.commitTransaction() def currentReadChannel(self): QgsMessageLog.logMessage('currentReadChannel') return self._file.currentReadChannel() def currentWriteChannel(self): QgsMessageLog.logMessage('currentWriteChannel') return self._file.currentWriteChannel() def errorString(self): QgsMessageLog.logMessage('errorString') return self._file.errorString() def getChar(self): if self._bytes_left > 0: QgsMessageLog.logMessage('getChar') self._bytes_left = self._bytes_left - 1 return self._file.getChar() else: QgsMessageLog.logMessage('getChar(no bytes left)') return None def isOpen(self): QgsMessageLog.logMessage('isOpen') return self._file.isOpen() def isReadable(self): QgsMessageLog.logMessage('isReadable') return self._file.isReadable() def isTextModeEnabled(self): QgsMessageLog.logMessage('isTextModeEnabled') return self._file.isTextModeEnabled() def isTransactionStarted(self): QgsMessageLog.logMessage('isTransactionStarted') return self._file.isTransactionStarted() def isWritable(self): QgsMessageLog.logMessage('isWritable') return self._file.isWritable() def openMode(self): QgsMessageLog.logMessage('openMode') return self._file.openMode() def peek(self, maxlen): QgsMessageLog.logMessage('peek {0} {1}'.format(maxlen, self._bytes_left)) if maxlen > self._bytes_left: maxlen = self._bytes_left return self._file.peek(maxlen) def read(self, max_size): QgsMessageLog.logMessage('read {0}'.format(max_size)) return self._file.read(max_size) def readAll(self): QgsMessageLog.logMessage('readAll') data = self._file.read(self._bytes_left) self._bytes_left = self._bytes_left - len(data) return data def readChannelCount(self): QgsMessageLog.logMessage('readChannelCount') return self._file.readChannelCount() def readLine(self): QgsMessageLog.logMessage('readLine') return None def rollbackTransaction(self): QgsMessageLog.logMessage('rollbackTransaction') return self._file.rollbackTransaction() def setCurrentReadChannel(self, channel): QgsMessageLog.logMessage('setCurrentReadChannel {0}'.format(channel)) return self._file.setCurrentReadChannel(channel) def setErrorString(self, errorString): QgsMessageLog.logMessage('setErrorString {0}'.format(errorString)) return self._file.setErrorString(errorString) def setOpenMode(self, mode): QgsMessageLog.logMessage('setOpenMode {0}'.format(mode)) return self._file.setOpenMode(mode) def setTextModeEnabled(self, enabled): QgsMessageLog.logMessage('setTextModeEnabled {0}'.format(enabled)) return self._file.setTextModeenabled(enabled) def skip(self, maxSize): QgsMessageLog.logMessage('skip {0} {1}'.format(maxSize, self._bytes_left)) if maxSize > self._bytes_left: maxSize = self._bytes_left return self._file.skip(maxSize) def startTransaction(self): QgsMessageLog.logMessage('startTransaction') return self._file.startTransaction() def ungetChar(self, c): QgsMessageLog.logMessage('ungetChar {0}'.format(c)) return None def writeChannelCount(self): QgsMessageLog.logMessage('writeChannelCount') return self._file.writeChannelCount() def atEnd(self): QgsMessageLog.logMessage('atEnd') if self._bytes_left == 0: return True else: return self._file.atEnd() def bytesAvailable(self): QgsMessageLog.logMessage('bytesAvailable') real_bytes_available = self._file.bytesAvailable() if real_bytes_available > self._bytes_left: return self._bytes_left else: return real_bytes_available def bytesToWrite(self): QgsMessageLog.logMessage('bytesToWrite') return self._file.bytesToWrite() def canReadLine(self): QgsMessageLog.logMessage('canReadLine') return self._file.canReadLine() def close(self): QgsMessageLog.logMessage('close') return self._file.close() def isSequential(self): QgsMessageLog.logMessage('isSequential') return self._file.isSequential() def open(self, mode): QgsMessageLog.logMessage('open') return self._file.open(mode) def pos(self): QgsMessageLog.logMessage('pos') return self._original_size - self._bytes_left def readData(self, max_size): if max_size > self._bytes_left: max_size = self._bytes_left self._bytes_left = self._bytes_left - max_size if self._bytes_left == 0: self.readChannelFinished.emit() QgsMessageLog.logMessage('readData {0}'.format(max_size)) return self._file.readData(max_size) def reset(self): QgsMessageLog.logMessage('readData') return True def seek(self, pos): QgsMessageLog.logMessage('seek {0}'.format(pos)) return True def size(self): file_size = self._file.size() if file_size < self._bytes_left: QgsMessageLog.logMessage('size(file_size) {0}'.format(file_size)) return file_size else: QgsMessageLog.logMessage('size(bytes_left) {0}'.format(self._bytes_left)) return self._bytes_left def waitForReadyRead(self, msecs): QgsMessageLog.logMessage('waitForReadyRead {0}'.format(msecs)) return self._file.waitForReadyRead(msecs) def on_aboutToClose(self): QgsMessageLog.logMessage('on_aboutToClose') self.aboutToClose.emit() def on_bytesWritten(self, bytes_written): QgsMessageLog.logMessage('on_bytesWritten {0}'.format(bytes_written)) self.bytesWritten.emit(bytes_written) def on_channelBytesWritten(self, channel, bytes_written): QgsMessageLog.logMessage('on_channelBytesWritten {0} {1}'.format(channel, bytes_written)) self.channelBytesWritten.emit(channel, bytes_written) def on_channelReadyRead(self, channel): QgsMessageLog.logMessage('on_channelReadyRead {0}'.format(channel)) if self._bytes_left > 0: self.channelReadyRead.emit(channel) def on_readChannelFinished(self): QgsMessageLog.logMessage('on_readChannelFinished') self.readChannelFinished.emit() def on_readyRead(self): QgsMessageLog.logMessage('on_readyRead') if self._bytes_left > 0: self.readyRead.emit() def flush(self): QgsMessageLog.logMessage('flush') return self._file.flush() def fileName(self): QgsMessageLog.logMessage('filename') return self._file.fileName() def exists(self): QgsMessageLog.logMessage('exists') return self._file.exists() def decodeName(self, localFileName): QgsMessageLog.logMessage('decodeName {0}'.format(localFileName)) return self._file.decodeName(localFileName)
class QgsPluginInstallerInstallingDialog( QDialog, Ui_QgsPluginInstallerInstallingDialogBase): # ----------------------------------------- # def __init__(self, parent, plugin): QDialog.__init__(self, parent) self.setupUi(self) self.plugin = plugin self.mResult = "" self.progressBar.setRange(0, 0) self.progressBar.setFormat("%p%") self.labelName.setText(plugin["name"]) self.buttonBox.clicked.connect(self.abort) url = QUrl(plugin["download_url"]) fileName = plugin["filename"] tmpDir = QDir.tempPath() tmpPath = QDir.cleanPath(tmpDir + "/" + fileName) self.file = QFile(tmpPath) self.request = QNetworkRequest(url) authcfg = repositories.all()[plugin["zip_repository"]]["authcfg"] if authcfg and isinstance(authcfg, basestring): if not QgsAuthManager.instance().updateNetworkRequest( self.request, authcfg.strip()): self.mResult = self.tr( "Update of network request with authentication " "credentials FAILED for configuration '{0}'").format( authcfg) self.request = None if self.request is not None: self.reply = QgsNetworkAccessManager.instance().get(self.request) self.reply.downloadProgress.connect(self.readProgress) self.reply.finished.connect(self.requestFinished) self.stateChanged(4) def exec_(self): if self.request is None: return QDialog.Rejected QDialog.exec_(self) # ----------------------------------------- # def result(self): return self.mResult # ----------------------------------------- # def stateChanged(self, state): messages = [ self.tr("Installing..."), self.tr("Resolving host name..."), self.tr("Connecting..."), self.tr("Host connected. Sending request..."), self.tr("Downloading data..."), self.tr("Idle"), self.tr("Closing connection..."), self.tr("Error") ] self.labelState.setText(messages[state]) # ----------------------------------------- # def readProgress(self, done, total): if total > 0: self.progressBar.setMaximum(total) self.progressBar.setValue(done) # ----------------------------------------- # def requestFinished(self): reply = self.sender() self.buttonBox.setEnabled(False) if reply.error() != QNetworkReply.NoError: self.mResult = reply.errorString() if reply.error() == QNetworkReply.OperationCanceledError: self.mResult += "<br/><br/>" + QCoreApplication.translate( "QgsPluginInstaller", "If you haven't cancelled the download manually, it might be caused by a timeout. In this case consider increasing the connection timeout value in QGIS options." ) self.reject() reply.deleteLater() return self.file.open(QFile.WriteOnly) self.file.write(reply.readAll()) self.file.close() self.stateChanged(0) reply.deleteLater() pluginDir = qgis.utils.home_plugin_path tmpPath = self.file.fileName() # make sure that the parent directory exists if not QDir(pluginDir).exists(): QDir().mkpath(pluginDir) # if the target directory already exists as a link, remove the link without resolving: QFile(pluginDir + unicode(QDir.separator()) + self.plugin["id"]).remove() try: unzip( unicode(tmpPath), unicode(pluginDir) ) # test extract. If fails, then exception will be raised and no removing occurs # removing old plugin files if exist removeDir(QDir.cleanPath( pluginDir + "/" + self.plugin["id"])) # remove old plugin if exists unzip(unicode(tmpPath), unicode(pluginDir)) # final extract. except: self.mResult = self.tr( "Failed to unzip the plugin package. Probably it's broken or missing from the repository. You may also want to make sure that you have write permission to the plugin directory:" ) + "\n" + pluginDir self.reject() return try: # cleaning: removing the temporary zip file QFile(tmpPath).remove() except: pass self.close() # ----------------------------------------- # def abort(self): if self.reply.isRunning(): self.reply.finished.disconnect() self.reply.abort() del self.reply self.mResult = self.tr("Aborted by user") self.reject()
class QgsPluginInstallerInstallingDialog(QDialog, Ui_QgsPluginInstallerInstallingDialogBase): # ----------------------------------------- # def __init__(self, parent, plugin): QDialog.__init__(self, parent) self.setupUi(self) self.plugin = plugin self.mResult = "" self.progressBar.setRange(0, 0) self.progressBar.setFormat("%p%") self.labelName.setText(plugin["name"]) self.buttonBox.clicked.connect(self.abort) self.url = QUrl(plugin["download_url"]) self.redirectionCounter = 0 fileName = plugin["filename"] tmpDir = QDir.tempPath() tmpPath = QDir.cleanPath(tmpDir + "/" + fileName) self.file = QFile(tmpPath) self.requestDownloading() def requestDownloading(self): self.request = QNetworkRequest(self.url) self.request.setAttribute(QNetworkRequest.Attribute(QgsNetworkRequestParameters.AttributeInitiatorClass), "QgsPluginInstallerInstallingDialog") authcfg = repositories.all()[self.plugin["zip_repository"]]["authcfg"] if authcfg and isinstance(authcfg, str): if not QgsApplication.authManager().updateNetworkRequest( self.request, authcfg.strip()): self.mResult = self.tr( "Update of network request with authentication " "credentials FAILED for configuration '{0}'").format(authcfg) self.request = None if self.request is not None: self.reply = QgsNetworkAccessManager.instance().get(self.request) self.reply.downloadProgress.connect(self.readProgress) self.reply.finished.connect(self.requestFinished) self.stateChanged(4) def exec_(self): if self.request is None: return QDialog.Rejected QDialog.exec_(self) # ----------------------------------------- # def result(self): return self.mResult # ----------------------------------------- # def stateChanged(self, state): messages = [ QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Installing…"), QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Resolving host name…"), QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Connecting…"), QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Host connected. Sending request…"), QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Downloading data…"), self.tr("Idle"), QCoreApplication.translate('QgsPluginInstallerInstallingDialog', "Closing connection…"), self.tr("Error") ] self.labelState.setText(messages[state]) # ----------------------------------------- # def readProgress(self, done, total): if total > 0: self.progressBar.setMaximum(total) self.progressBar.setValue(done) # ----------------------------------------- # def requestFinished(self): reply = self.sender() self.buttonBox.setEnabled(False) if reply.error() != QNetworkReply.NoError: self.mResult = reply.errorString() if reply.error() == QNetworkReply.OperationCanceledError: self.mResult += "<br/><br/>" + QCoreApplication.translate("QgsPluginInstaller", "If you haven't canceled the download manually, it might be caused by a timeout. In this case consider increasing the connection timeout value in QGIS options.") self.reject() reply.deleteLater() return elif reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) in (301, 302): redirectionUrl = reply.attribute(QNetworkRequest.RedirectionTargetAttribute) self.redirectionCounter += 1 if self.redirectionCounter > 4: self.mResult = QCoreApplication.translate("QgsPluginInstaller", "Too many redirections") self.reject() reply.deleteLater() return else: if redirectionUrl.isRelative(): redirectionUrl = reply.url().resolved(redirectionUrl) # Fire a new request and exit immediately in order to quietly destroy the old one self.url = redirectionUrl self.requestDownloading() reply.deleteLater() return self.file.open(QFile.WriteOnly) self.file.write(reply.readAll()) self.file.close() self.stateChanged(0) reply.deleteLater() pluginDir = qgis.utils.home_plugin_path tmpPath = self.file.fileName() # make sure that the parent directory exists if not QDir(pluginDir).exists(): QDir().mkpath(pluginDir) # if the target directory already exists as a link, remove the link without resolving: QFile(pluginDir + str(QDir.separator()) + self.plugin["id"]).remove() try: unzip(str(tmpPath), str(pluginDir)) # test extract. If fails, then exception will be raised and no removing occurs # removing old plugin files if exist removeDir(QDir.cleanPath(pluginDir + "/" + self.plugin["id"])) # remove old plugin if exists unzip(str(tmpPath), str(pluginDir)) # final extract. except: self.mResult = self.tr("Failed to unzip the plugin package. Probably it's broken or missing from the repository. You may also want to make sure that you have write permission to the plugin directory:") + "\n" + pluginDir self.reject() return try: # cleaning: removing the temporary zip file QFile(tmpPath).remove() except: pass self.close() # ----------------------------------------- # def abort(self): if self.reply.isRunning(): self.reply.finished.disconnect() self.reply.abort() del self.reply self.mResult = self.tr("Aborted by user") self.reject()