Logo Search packages:      
Sourcecode: jockey version File versions  Download package

def jockey::ui::AbstractUI::download_url (   self,
  url,
  filename = None,
  data = None 
)

Download an URL into a local file, and display a progress dialog.

If filename is not given, a temporary file will be created.

Additional POST data can be submitted for HTTP requests in the data
argument (see urllib2.urlopen).

Return (filename, headers) tuple, or (None, headers) if the user
cancelled the download.

Definition at line 537 of file ui.py.

00537                                                          :
        '''Download an URL into a local file, and display a progress dialog.
        
        If filename is not given, a temporary file will be created.

        Additional POST data can be submitted for HTTP requests in the data
        argument (see urllib2.urlopen).

        Return (filename, headers) tuple, or (None, headers) if the user
        cancelled the download.
        '''
        block_size = 8192
        current_size = 0
        try:
            f = urllib2.urlopen(url)
        except Exception, e:
            self.error_message(self._('Download error'), str(e))
            return (None, None)
        headers = f.info()

        if 'Content-Length' in headers:
            total_size = int(headers['Content-Length'])
        else:
            total_size = -1

        self.ui_download_start(url, total_size)

        if filename:
            tfp = open(filename, 'wb')
            result_filename = filename
        else:
            (fd, result_filename) = tempfile.mkstemp()
            tfp = os.fdopen(fd, 'wb')

        try:
            while current_size < total_size:
                block = f.read(block_size)
                tfp.write (block)
                current_size += len(block)
                # if True, user canceled download
                if self.ui_download_progress(current_size, total_size):
                    # if we created a temporary file, clean it up
                    if not filename:
                        os.unlink(result_filename)
                    result_filename = None
                    break
        finally:
            tfp.close()
            f.close()
            self.ui_download_finish()

        return (result_filename, headers)

    #
    # The following methods must be implemented in subclasses


Generated by  Doxygen 1.6.0   Back to index