2014-04-20 16:07:04 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
EditScriptAction.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'April 2014'
|
|
|
|
__copyright__ = '(C) 201, Victor Olaya'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
2018-01-29 19:13:49 +02:00
|
|
|
import shutil
|
2015-05-18 19:51:26 +03:00
|
|
|
|
2018-03-10 12:23:15 +01:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication
|
2018-01-29 19:13:49 +02:00
|
|
|
from qgis.PyQt.QtWidgets import QFileDialog
|
2015-05-18 19:51:26 +03:00
|
|
|
|
2018-02-05 22:11:34 -04:00
|
|
|
from qgis.core import Qgis, QgsApplication, QgsMessageLog, QgsSettings
|
2017-01-09 12:34:46 +10:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
from processing.gui.ToolboxAction import ToolboxAction
|
2018-01-29 15:50:16 +02:00
|
|
|
|
2018-01-29 16:46:48 +02:00
|
|
|
from processing.script import ScriptUtils
|
2014-04-20 16:07:04 +02:00
|
|
|
|
2015-05-18 19:51:26 +03:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
class AddScriptFromFileAction(ToolboxAction):
|
|
|
|
|
|
|
|
def __init__(self):
|
2018-03-10 12:23:15 +01:00
|
|
|
self.name = QCoreApplication.translate("AddScriptFromFileAction", "Add Script from File…")
|
2018-01-29 19:13:49 +02:00
|
|
|
self.group = self.tr("Tools")
|
2014-05-02 10:32:37 +02:00
|
|
|
|
2014-04-20 16:07:04 +02:00
|
|
|
def execute(self):
|
2017-03-03 20:39:17 +01:00
|
|
|
settings = QgsSettings()
|
2018-01-29 19:13:49 +02:00
|
|
|
lastDir = settings.value("processing/lastScriptsDir", "")
|
|
|
|
files, _ = QFileDialog.getOpenFileNames(self.toolbox,
|
2018-01-30 08:12:01 +02:00
|
|
|
self.tr("Add script(s)"),
|
2018-01-29 19:13:49 +02:00
|
|
|
lastDir,
|
|
|
|
self.tr("Script files (*.py *.PY)"))
|
|
|
|
if files:
|
|
|
|
settings.setValue("processing/lastScriptsDir", os.path.dirname(files[0]))
|
|
|
|
|
|
|
|
valid = 0
|
|
|
|
for f in files:
|
2016-11-08 13:04:31 +01:00
|
|
|
try:
|
2018-01-29 19:13:49 +02:00
|
|
|
shutil.copy(f, ScriptUtils.scriptsFolders()[0])
|
|
|
|
valid += 1
|
|
|
|
except OSError as e:
|
2018-02-18 15:49:19 +01:00
|
|
|
QgsMessageLog.logMessage(self.tr("Could not copy script '{}'\n{}").format(f, str(e)),
|
2018-01-29 19:13:49 +02:00
|
|
|
"Processing",
|
2018-02-05 22:11:34 -04:00
|
|
|
Qgis.Warning)
|
2018-01-29 19:13:49 +02:00
|
|
|
|
|
|
|
if valid > 0:
|
|
|
|
QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
|