~facundo/encuentro/trunk

« back to all changes in this revision

Viewing changes to encuentro/network.py

  • Committer: Facundo Batista
  • Date: 2016-01-04 20:00:44 UTC
  • Revision ID: facundo@taniquetil.com.ar-20160104200044-89mzzvslalzv1bzs
Much more robust temp file cleaning and messages shown.

Show diffs side-by-side

added added

removed removed

Lines of Context:
165
165
                self.deferred.errback(err)
166
166
        QtCore.QTimer.singleShot(50, wrapper)
167
167
 
 
168
    def _clean(self, filename):
 
169
        """Remove a filename in a very safe way.
 
170
 
 
171
        Note: under Windows is tricky to remove files that may still be used.
 
172
        """
 
173
        if os.path.exists(filename):
 
174
            try:
 
175
                os.remove(filename)
 
176
            except Exception as err:
 
177
                self.log("Cleaning failed: %r", err)
 
178
            else:
 
179
                self.log("Cleaned ok")
 
180
 
168
181
 
169
182
class MiBrowser(Thread):
170
183
    """Threaded browser to do the download."""
332
345
                self.log("Cancelled! Quit browser, wait, and clean.")
333
346
                self.browser_quit.set()
334
347
                yield qinput.deferred_get()
335
 
                if os.path.exists(tempf):
336
 
                    os.remove(tempf)
337
 
                self.log("Cancelled! Cleaned up.")
 
348
                self._clean(tempf)
 
349
                self.log("Cancelled!")
338
350
                raise CancelledError()
339
351
 
340
352
            # special situations
618
630
 
619
631
        for i, url in enumerate(chunk_urls, 1):
620
632
            if self.cancelled:
621
 
                self.log("Cancelled! Cleaning...")
622
 
                if os.path.exists(tempf):
623
 
                    os.remove(tempf)
624
 
                self.log("Cancelled! Cleaned up.")
 
633
                self._clean(tempf)
 
634
                self.log("Cancelled!")
625
635
                raise CancelledError()
626
636
            if self.should_stop:
627
 
                if os.path.exists(tempf):
628
 
                    os.remove(tempf)
 
637
                self._clean(tempf)
629
638
                self.log("Stopped")
630
639
                return
631
640