Example #1
0
    def testTaskFromFunctionWithFlags(self):
        """ test creating task from function with flags"""

        task = QgsTask.fromFunction('test task', run, 20, flags=QgsTask.Flags())
        self.assertFalse(task.canCancel())
        task2 = QgsTask.fromFunction('test task', run, 20, flags=QgsTask.CanCancel)
        self.assertTrue(task2.canCancel())
Example #2
0
    def testTaskFromFunctionWithSubTaskCompletedIsCalledOnce(self):  # spellok
        """ test that when a parent task has subtasks it does emit taskCompleted only once"""

        self.finished = 0
        self.completed = 0

        def _on_finished(e):
            self.finished += 1

        def _on_completed():
            self.completed += 1

        task = QgsTask.fromFunction('test task', run_no_result, on_finished=_on_finished)
        task.taskCompleted.connect(_on_completed)
        spy = QSignalSpy(task.taskCompleted)
        sub_task_1 = QgsTask.fromFunction('test subtask 1', run_no_result, on_finished=_on_finished)
        sub_task_2 = QgsTask.fromFunction('test subtask 2', run_no_result, on_finished=_on_finished)
        task.addSubTask(sub_task_1, [], QgsTask.ParentDependsOnSubTask)
        task.addSubTask(sub_task_2, [], QgsTask.ParentDependsOnSubTask)

        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            QCoreApplication.processEvents()
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        self.assertEqual(self.completed, 1)
        self.assertEqual(self.finished, 3)
        self.assertEqual(len(spy), 1)
Example #3
0
    def __init__(self, settings):
        QgsTask.__init__(self, 'QConsolidate')

        self.settings = settings

        self.project = None
        self.projectFile = None

        self.baseDirectory = self.settings['output']
        self.dataDirectory = os.path.join(self.baseDirectory, LAYERS_DIRECTORY)

        self.error = ''
Example #4
0
    def testTaskFromFunctionWithKwargs(self):
        """ test creating task from function using kwargs """

        task = QgsTask.fromFunction('test task3', run_with_kwargs, result=5, password=1)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass

        self.assertEqual(task.returned_values, 5)
        self.assertFalse(task.exception)
        self.assertEqual(task.status(), QgsTask.Complete)
Example #5
0
    def testTaskFromFunction(self):
        """ test creating task from function """

        task = QgsTask.fromFunction('test task', run, 20)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass

        self.assertEqual(task.returned_values, 20)
        self.assertFalse(task.exception)
        self.assertEqual(task.status(), QgsTask.Complete)

        # try a task which cancels itself
        bad_task = QgsTask.fromFunction('test task2', run, None)
        QgsApplication.taskManager().addTask(bad_task)
        while bad_task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass

        self.assertFalse(bad_task.returned_values)
        self.assertTrue(bad_task.exception)
        self.assertEqual(bad_task.status(), QgsTask.Terminated)
Example #6
0
    def testTaskFromFunctionFinishedWithVal(self):
        """ test that task from function can have callback finished function and is passed result values"""
        task = QgsTask.fromFunction('test task', run_single_val_result, on_finished=finished_single_value_result)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        # check that the finished function was called
        self.assertEqual(task.returned_values, (5))
        self.assertFalse(task.exception)
        self.assertEqual(finished_single_value_result.value, 5)
Example #7
0
    def testTaskFromFunctionFinishedFail(self):
        """ test that task from function which fails calls finished with exception"""
        task = QgsTask.fromFunction('test task', run_fail, on_finished=finished_fail)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        # check that the finished function was called
        self.assertTrue(task.exception)
        self.assertTrue(finished_fail.finished_exception)
        self.assertEqual(task.exception, finished_fail.finished_exception)
Example #8
0
    def testTaskFromFunctionFinished(self):
        """ test that task from function can have callback finished function"""
        task = QgsTask.fromFunction('test task', run_no_result, on_finished=finished_no_val)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        # check that the finished function was called
        self.assertFalse(task.returned_values)
        self.assertFalse(task.exception)
        self.assertTrue(finished_no_val.called)
Example #9
0
    def testTaskFromFunctionCanceledWhileQueued(self):
        """ test that task from finished is called with exception when task is terminated while queued"""
        task = QgsTask.fromFunction('test task', run_no_result, on_finished=finished_fail)
        task.hold()
        QgsApplication.taskManager().addTask(task)
        task.cancel()
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        # check that the finished function was called
        self.assertTrue(task.exception)
        self.assertTrue(finished_fail.finished_exception)
        self.assertEqual(task.exception, finished_fail.finished_exception)
Example #10
0
    def testTaskFromFunctionIsCancellable(self):
        """ test that task from function can check canceled status """
        bad_task = QgsTask.fromFunction('test task4', cancellable)
        QgsApplication.taskManager().addTask(bad_task)
        while bad_task.status() != QgsTask.Running:
            pass

        bad_task.cancel()
        while bad_task.status() == QgsTask.Running:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        self.assertEqual(bad_task.status(), QgsTask.Terminated)
        self.assertTrue(bad_task.exception)
Example #11
0
    def testTaskFromFunctionCanSetProgress(self):
        """ test that task from function can set progress """
        task = QgsTask.fromFunction('test task5', progress_function)
        QgsApplication.taskManager().addTask(task)
        while task.status() != QgsTask.Running:
            pass

        # wait a fraction so that setProgress gets a chance to be called
        sleep(0.001)
        self.assertEqual(task.progress(), 50)
        self.assertFalse(task.exception)

        task.cancel()
        while task.status() == QgsTask.Running:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()
  def runTask(self, dataRun):
    def finished(exception, dataResult):
      def endEditable():
        self.layerPolygon.commitChanges()
        self.layerPolygon.updateExtents()
        if self.isIniEditable:
          self.layerPolygon.startEditing()

      def setRenderLayer():
        fileStyle = os.path.join( os.path.dirname( __file__ ), "gimpselectionfeature_with_expression.qml" )
        self.layerPolygon.loadNamedStyle( fileStyle )
        self.mapCanvasEffects.zoom( self.layerPolygon, dataResult['geomBBox'] )
      
      self.msgBar.clearWidgets()
      if exception:
        self.msgBar.pushMessage( self.nameModulus, str( exception ), Qgis.Critical )
        return
      self.msgBar.pushMessage( self.nameModulus, dataResult['message'], dataResult['level'] )
      if 'getFeatures' == dataResult['process']:
        endEditable()
        imgs =  filter( lambda f: os.path.exists( f ), ( self.pathfileImage, self.pathfileImageSelect ) )
        [ os.remove( item ) for item in imgs ]
        if dataResult['isOk']:
          setRenderLayer()
          self.setEnabledWidgetTransfer( True, True )
        else:
          self.setEnabledWidgetTransfer( True )
      else:
        self.setEnabledWidgetTransfer( True )

    def run(task, data):
      self.worker.setData( task, data )
      return self.worker.run()

    self.setEnabledWidgetTransfer( False )
    task = QgsTask.fromFunction('GimpSelectionFeature Task', run, dataRun, on_finished=finished )
    if not self.layerPolygon is None:
      task.setDependentLayers( [ self.layerPolygon ] )
    self.taskManager.addTask( task )
Example #13
0
    def testTaskFromFunctionFinishedWithMultipleValues(self):
        """ test that task from function can have callback finished function and is passed multiple result values"""
        result_value = None
        result_statement = None

        def finished_multiple_value_result(e, results):
            nonlocal result_value
            nonlocal result_statement
            assert e is None
            result_value = results[0]
            result_statement = results[1]

        task = QgsTask.fromFunction('test task', run_multiple_val_result, on_finished=finished_multiple_value_result)
        QgsApplication.taskManager().addTask(task)
        while task.status() not in [QgsTask.Complete, QgsTask.Terminated]:
            pass
        while QgsApplication.taskManager().countActiveTasks() > 0:
            QCoreApplication.processEvents()

        # check that the finished function was called
        self.assertEqual(task.returned_values, (5, 'whoo'))
        self.assertFalse(task.exception)
        self.assertEqual(result_value, 5)
        self.assertEqual(result_statement, 'whoo')