mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
[processing] ressurect 'add from file' action
This commit is contained in:
parent
b747a6b50c
commit
7115e044e3
@ -26,55 +26,42 @@ __copyright__ = '(C) 201, Victor Olaya'
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import os
|
||||
import shutil
|
||||
|
||||
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
|
||||
from qgis.PyQt.QtCore import QFileInfo
|
||||
from qgis.PyQt.QtWidgets import QFileDialog
|
||||
|
||||
from qgis.core import QgsApplication, QgsSettings
|
||||
from qgis.core import QgsApplication, QgsMessageLog, QgsSettings
|
||||
|
||||
from processing.gui.ToolboxAction import ToolboxAction
|
||||
|
||||
from processing.script.ScriptAlgorithm import ScriptAlgorithm
|
||||
from processing.script.WrongScriptException import WrongScriptException
|
||||
from processing.script import ScriptUtils
|
||||
|
||||
pluginPath = os.path.split(os.path.dirname(__file__))[0]
|
||||
|
||||
|
||||
class AddScriptFromFileAction(ToolboxAction):
|
||||
|
||||
def __init__(self):
|
||||
self.name = self.tr('Add script from file')
|
||||
self.group = self.tr('Tools')
|
||||
|
||||
def getIcon(self):
|
||||
return QgsApplication.getThemeIcon("/processingScript.svg")
|
||||
self.name = self.tr("Add script from file")
|
||||
self.group = self.tr("Tools")
|
||||
|
||||
def execute(self):
|
||||
settings = QgsSettings()
|
||||
lastDir = settings.value('Processing/lastScriptsDir', '')
|
||||
filenames, selected_filter = QFileDialog.getOpenFileNames(self.toolbox,
|
||||
self.tr('Script files'),
|
||||
lastDir,
|
||||
self.tr('Script files (*.py *.PY)'))
|
||||
if filenames:
|
||||
validAlgs = 0
|
||||
wrongAlgs = []
|
||||
for filename in filenames:
|
||||
lastDir = settings.value("processing/lastScriptsDir", "")
|
||||
files, _ = QFileDialog.getOpenFileNames(self.toolbox,
|
||||
self.tr("Script file"),
|
||||
lastDir,
|
||||
self.tr("Script files (*.py *.PY)"))
|
||||
if files:
|
||||
settings.setValue("processing/lastScriptsDir", os.path.dirname(files[0]))
|
||||
|
||||
valid = 0
|
||||
for f in files:
|
||||
try:
|
||||
settings.setValue('Processing/lastScriptsDir',
|
||||
QFileInfo(filename).absoluteDir().absolutePath())
|
||||
script = ScriptAlgorithm(filename)
|
||||
destFilename = os.path.join(ScriptUtils.scriptsFolders()[0], os.path.basename(filename))
|
||||
with open(destFilename, 'w') as f:
|
||||
f.write(script.script)
|
||||
validAlgs += 1
|
||||
except WrongScriptException:
|
||||
wrongAlgs.append(os.path.basename(filename))
|
||||
if validAlgs:
|
||||
QgsApplication.processingRegistry().providerById('script').refreshAlgorithms()
|
||||
if wrongAlgs:
|
||||
QMessageBox.warning(self.toolbox,
|
||||
self.tr('Error reading scripts'),
|
||||
self.tr('The following files do not contain a valid script:\n') +
|
||||
"\n- ".join(wrongAlgs))
|
||||
shutil.copy(f, ScriptUtils.scriptsFolders()[0])
|
||||
valid += 1
|
||||
except OSError as e:
|
||||
QgsMessageLog.logMessage("Could not copy script '{}'\n{}".format(s, str(e)),
|
||||
"Processing",
|
||||
QgsMessageLog.WARNING)
|
||||
|
||||
if valid > 0:
|
||||
QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
|
||||
|
Loading…
x
Reference in New Issue
Block a user