Skip to content
Permalink
Browse files

[processing] ressurect 'add from file' action

  • Loading branch information
alexbruy committed Feb 5, 2018
1 parent b747a6b commit 7115e044e3ce24bf419e5868abae9a0e7fd34835
Showing with 24 additions and 37 deletions.
  1. +24 −37 python/plugins/processing/script/AddScriptFromFileAction.py
@@ -26,55 +26,42 @@
__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()

0 comments on commit 7115e04

Please sign in to comment.
You can’t perform that action at this time.