Skip to content


[processing] ressurect 'add from file' action
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Feb 5, 2018
1 parent b747a6b commit 7115e04
Showing 1 changed file with 24 additions and 37 deletions.
61 changes: 24 additions & 37 deletions python/plugins/processing/script/
Expand Up @@ -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): ='Add script from file') ='Tools')

def getIcon(self):
return QgsApplication.getThemeIcon("/processingScript.svg") ="Add script from file") ="Tools")

def execute(self):
settings = QgsSettings()
lastDir = settings.value('Processing/lastScriptsDir', '')
filenames, selected_filter = QFileDialog.getOpenFileNames(self.toolbox,'Script files'),
lastDir,'Script files (*.py *.PY)'))
if filenames:
validAlgs = 0
wrongAlgs = []
for filename in filenames:
lastDir = settings.value("processing/lastScriptsDir", "")
files, _ = QFileDialog.getOpenFileNames(self.toolbox,"Script file"),
lastDir,"Script files (*.py *.PY)"))
if files:
settings.setValue("processing/lastScriptsDir", os.path.dirname(files[0]))

valid = 0
for f in files:
script = ScriptAlgorithm(filename)
destFilename = os.path.join(ScriptUtils.scriptsFolders()[0], os.path.basename(filename))
with open(destFilename, 'w') as f:
validAlgs += 1
except WrongScriptException:
if validAlgs:
if wrongAlgs:
QMessageBox.warning(self.toolbox,'Error reading scripts'),'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)),

if valid > 0:

0 comments on commit 7115e04

Please sign in to comment.