DeleteScriptAction.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. ***************************************************************************
  3. DeleteScriptAction.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. from qgis.PyQt.QtCore import QCoreApplication
  22. from qgis.PyQt.QtWidgets import QMessageBox
  23. from qgis.core import QgsApplication, QgsProcessingAlgorithm
  24. from processing.gui.ContextAction import ContextAction
  25. from processing.script import ScriptUtils
  26. class DeleteScriptAction(ContextAction):
  27. def __init__(self):
  28. super().__init__()
  29. self.name = QCoreApplication.translate("DeleteScriptAction", "Delete Script…")
  30. def isEnabled(self):
  31. return isinstance(self.itemData, QgsProcessingAlgorithm) and self.itemData.provider().id() == "script"
  32. def execute(self):
  33. reply = QMessageBox.question(None,
  34. self.tr("Delete Script"),
  35. self.tr("Are you sure you want to delete this script?"),
  36. QMessageBox.Yes | QMessageBox.No,
  37. QMessageBox.No)
  38. if reply == QMessageBox.Yes:
  39. filePath = ScriptUtils.findAlgorithmSource(self.itemData.name())
  40. if filePath is not None:
  41. os.remove(filePath)
  42. QgsApplication.processingRegistry().providerById("script").refreshAlgorithms()
  43. else:
  44. QMessageBox.warning(None,
  45. self.tr("Delete Script"),
  46. self.tr("Can not find corresponding script file.")
  47. )