本文整理汇总了Python中qgis.PyQt.QtCore.QFile.write方法的典型用法代码示例。如果您正苦于以下问题:Python QFile.write方法的具体用法?Python QFile.write怎么用?Python QFile.write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类qgis.PyQt.QtCore.QFile
的用法示例。
在下文中一共展示了QFile.write方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createSimpleMemorial
# 需要导入模块: from qgis.PyQt.QtCore import QFile [as 别名]
# 或者: from qgis.PyQt.QtCore.QFile import write [as 别名]
def createSimpleMemorial(self):
tempDoc = QDomDocument()
simple = QFile(self.simpleMemorial)
simple.open(QIODevice.ReadOnly)
loaded = tempDoc.setContent(simple)
simple.close()
element = tempDoc.documentElement()
nodes = element.elementsByTagName("table")
table = nodes.item(0).toElement()
tr = tempDoc.createElement("tr")
tr.appendChild(self.createCellElement(tempDoc, u"MEMORIAL DESCRITIVO SINTÉTICO", 7, 0))
table.appendChild(tr)
tr = tempDoc.createElement("tr")
tr.appendChild(self.createCellElement(tempDoc, u"VÉRTICE", 0, 2))
tr.appendChild(self.createCellElement(tempDoc, "COORDENADAS", 2, 0))
tr.appendChild(self.createCellElement(tempDoc, "LADO", 0, 2))
tr.appendChild(self.createCellElement(tempDoc, "AZIMUTES", 2, 0))
tr.appendChild(self.createCellElement(tempDoc, u"DISTÂNCIA", 0, 0))
table.appendChild(tr)
tr = tempDoc.createElement("tr")
tr.appendChild(self.createCellElement(tempDoc, "E", 0, 0))
tr.appendChild(self.createCellElement(tempDoc, "N", 0, 0))
tr.appendChild(self.createCellElement(tempDoc, "PLANO", 0, 0))
tr.appendChild(self.createCellElement(tempDoc, "REAL", 0, 0))
tr.appendChild(self.createCellElement(tempDoc, "(m)", 0, 0))
table.appendChild(tr)
convergence = float(self.convergenciaEdit.text())
rowCount = self.tableWidget.rowCount()
for i in range(0,rowCount):
lineElement = tempDoc.createElement("tr")
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,0).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,1).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,2).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,3).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,4).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,5).text(), 0, 0))
lineElement.appendChild(self.createCellElement(tempDoc, self.tableWidget.item(i,6).text(), 0, 0))
table.appendChild(lineElement)
simple = open(self.simpleMemorial, "w", encoding='utf-8')
simple.write(tempDoc.toString())
simple.close()
示例2: requestFinished
# 需要导入模块: from qgis.PyQt.QtCore import QFile [as 别名]
# 或者: from qgis.PyQt.QtCore.QFile import write [as 别名]
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)
示例3: FileDownloader
# 需要导入模块: from qgis.PyQt.QtCore import QFile [as 别名]
# 或者: from qgis.PyQt.QtCore.QFile import write [as 别名]
class FileDownloader():
"""The blueprint for downloading file from url."""
def __init__(self, url, output_path, progress_dialog=None):
"""Constructor of the class.
.. versionchanged:: 3.3 removed manager parameter.
:param url: URL of file.
:type url: str
:param output_path: Output path.
:type output_path: str
:param progress_dialog: Progress dialog widget.
:type progress_dialog: QWidget
"""
# noinspection PyArgumentList
self.manager = QgsNetworkAccessManager.instance()
self.url = QUrl(url)
self.output_path = output_path
self.progress_dialog = progress_dialog
if self.progress_dialog:
self.prefix_text = self.progress_dialog.labelText()
self.output_file = None
self.reply = None
self.downloaded_file_buffer = None
self.finished_flag = False
def download(self):
"""Downloading the file.
:returns: True if success, otherwise returns a tuple with format like
this (QNetworkReply.NetworkError, error_message)
:raises: IOError - when cannot create output_path
"""
# Prepare output path
self.output_file = QFile(self.output_path)
if not self.output_file.open(QFile.WriteOnly):
raise IOError(self.output_file.errorString())
# Prepare downloaded buffer
self.downloaded_file_buffer = QByteArray()
# Request the url
request = QNetworkRequest(self.url)
self.reply = self.manager.get(request)
self.reply.readyRead.connect(self.get_buffer)
self.reply.finished.connect(self.write_data)
self.manager.requestTimedOut.connect(self.request_timeout)
if self.progress_dialog:
# progress bar
def progress_event(received, total):
"""Update progress.
:param received: Data received so far.
:type received: int
:param total: Total expected data.
:type total: int
"""
# noinspection PyArgumentList
QgsApplication.processEvents()
self.progress_dialog.adjustSize()
human_received = humanize_file_size(received)
human_total = humanize_file_size(total)
label_text = tr("%s : %s of %s" % (
self.prefix_text, human_received, human_total))
self.progress_dialog.setLabelText(label_text)
self.progress_dialog.setMaximum(total)
self.progress_dialog.setValue(received)
# cancel
def cancel_action():
"""Cancel download."""
self.reply.abort()
self.reply.deleteLater()
self.reply.downloadProgress.connect(progress_event)
self.progress_dialog.canceled.connect(cancel_action)
# Wait until finished
# On Windows 32bit AND QGIS 2.2, self.reply.isFinished() always
# returns False even after finished slot is called. So, that's why we
# are adding self.finished_flag (see #864)
while not self.reply.isFinished() and not self.finished_flag:
# noinspection PyArgumentList
QgsApplication.processEvents()
result = self.reply.error()
try:
http_code = int(self.reply.attribute(
QNetworkRequest.HttpStatusCodeAttribute))
#.........这里部分代码省略.........
示例4: QgsPluginInstallerInstallingDialog
# 需要导入模块: from qgis.PyQt.QtCore import QFile [as 别名]
# 或者: from qgis.PyQt.QtCore.QFile import write [as 别名]
#.........这里部分代码省略.........
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()
示例5: _download_file
# 需要导入模块: from qgis.PyQt.QtCore import QFile [as 别名]
# 或者: from qgis.PyQt.QtCore.QFile import write [as 别名]
def _download_file(self, url, out_path):
reply = self.__sync_request(url)
local_file = QFile(out_path)
local_file.open(QIODevice.WriteOnly)
local_file.write(reply)
local_file.close()