Commits

Virgil Dupras  committed c0042bb

Added Job.check_if_cancelled().

  • Participants
  • Parent commits baf6dbd
  • Tags 1.0.2

Comments (0)

Files changed (4)

File docs/job.rst

     .. method:: add_progress(progress=1, desc='')
 
         Add ``progress`` units of progress to the current work unit. Send description ``desc`` to the callback.
-
+    
+    .. method:: check_if_cancelled()
+    
+        Raises JobCancelled if the user has cancelled the job like add_progress would normally do,
+        but it doesn't change the current progress. Use this if you're doing work with an
+        indeterminate amount of work units and want to make the task cancellable.
+    
     .. method:: iter_with_progress(sequence, desc_format=None, every=1)
 
         Yields each element of ``sequence`` by first calling :meth:``start_job`` on self with progress units equal to the length of ``sequence``. At each yields, :meth:``add_progress`` is called. If ``desc_format`` is not None, a new description is generated from the format at every ``every`` element. For example, such a format could be "Processed %d elements out of %d". If you have a lot of elements in ``sequence``, it might be a good idea to make ``every`` higher so that description formatting doesn't affect your process performance too much.

File jobprogress/job.py

 # Created By: Virgil Dupras
 # Created On: 2004/12/20
-# $Id
 # Copyright 2011 Hardcoded Software (http://www.hardcoded.net)
 
 # This software is licensed under the "BSD" License as described in the "LICENSE" file, 
         
         The parameter is a int in the 0-100 range.
         """
-        passed_progress = self._passed_jobs * self._currmax
-        current_progress = self._current_job * self._progress
-        total_progress = self._jobcount * self._currmax
-        progress = ((passed_progress + current_progress) * 100) // total_progress
+        if self._current_job:
+            passed_progress = self._passed_jobs * self._currmax
+            current_progress = self._current_job * self._progress
+            total_progress = self._jobcount * self._currmax
+            progress = ((passed_progress + current_progress) * 100) // total_progress
+        else:
+            progress = -1 # indeterminate
         # It's possible that callback doesn't support a desc arg
         result = self._callback(progress, desc) if desc else self._callback(progress)
         if not result:
     def add_progress(self, progress=1, desc=''):
         self.set_progress(self._progress + progress, desc)
     
+    def check_if_cancelled(self):
+        self._do_update('')
+    
     def iter_with_progress(self, sequence, desc_format=None, every=1):
         ''' Iterate through sequence while automatically adding progress.
         '''
     def add_progress(self, *args, **kwargs):
         pass
     
+    def check_if_cancelled(self):
+        pass
+    
     def iter_with_progress(self, sequence, *args, **kwargs):
         return iter(sequence)
     
 
 setup(
     name='jobprogress',
-    version='1.0.1',
+    version='1.0.2',
     author='Hardcoded Software',
     author_email='hsoft@hardcoded.net',
     packages=['jobprogress'],

File tests/job_test.py

                 self.assertEqual(self.lastdesc, 'Processed %d items of 5' % (i - 1))
         self.assertEqual(self.lastdesc, 'Processed 5 items of 5')
     
+    def test_check_if_cancelled(self):
+        # check_if_cancelled() checks if the job is cancelled and raises JobCancelled if it is.
+        j = job.Job(1, self.callback)
+        j.check_if_cancelled() # nothing happens
+        self.assertEqual(self.lastprogress, -1) # When no job have been started, we send a progress of -1
+        self.cancel = True
+        self.assertRaises(job.JobCancelled, j.check_if_cancelled)