original_docstring: Download a file from a URL, possibly displaying a progress bar.
  
      Saves the output to the file named by `path`.  If the URL cannot be
      downloaded or the file cannot be written, an IOError is raised.
  
      Normally, if the standard error output is a terminal, then a
      progress bar is displayed to keep the user entertained.  Specify
      `verbose=True` or `verbose=False` to control this behavior.
  
      
docstring: Download a file from a URL, possibly displaying a progress bar.
  Saves the output to the file named by `path`.  If the URL cannot be
  downloaded or the file cannot be written, an IOError is raised.
  
  Normally, if the standard error output is a terminal, then a
  progress bar is displayed to keep the user entertained.  Specify
  `verbose=True` or `verbose=False` to control this behavior.
docstring_tokens: Download a file from a URL possibly displaying a progress bar . Saves the output to the file named by ` path ` . If the URL cannot be downloaded or the file cannot be written an IOError is raised . Normally if the standard error output is a terminal then a progress bar is displayed to keep the user entertained . Specify ` verbose = True ` or ` verbose = False ` to control this behavior .
code: def download(url, path, verbose=None, blocksize=128*1024):
      """Download a file from a URL, possibly displaying a progress bar.
  
      Saves the output to the file named by `path`.  If the URL cannot be
      downloaded or the file cannot be written, an IOError is raised.
  
      Normally, if the standard error output is a terminal, then a
      progress bar is displayed to keep the user entertained.  Specify
      `verbose=True` or `verbose=False` to control this behavior.
  
      """
      tempname = path + '.download'
      try:
          connection = urlopen(url)
      except Exception as e:
          e2 = IOError('cannot get {0} because {1}'.format(url, e))
          e2.__cause__ = None
          raise e2
      if verbose is None:
          verbose = sys.stderr.isatty()
  
      bar = None
      if verbose:
          if _running_IDLE:
              print('Downloading {0} ...'.format(os.path.basename(path)),
                    file=sys.stderr)
          else:
              bar = ProgressBar(path)
              content_length = int(connection.headers.get('content-length', -1))
  
      # Python open() provides no way to achieve O_CREAT without also
      # truncating the file, which would ruin the work of another process
      # that is trying to download the same file at the same time.  So:
  
      flags = getattr(os, 'O_BINARY', 0) | os.O_CREAT | os.O_RDWR
      fd = os.open(tempname, flags, 0o666)
      with os.fdopen(fd, 'wb') as w:
          try:
              if lockf is not None:
                  fd = w.fileno()
                  lockf(fd, LOCK_EX)           # only one download at a time
                  if os.path.exists(path): # did someone else finish first?
                      if os.path.exists(tempname):
                          os.unlink(tempname)
                      return
              w.seek(0)
              length = 0
              while True:
                  data = connection.read(blocksize)
                  if not data:
                      break
                  w.write(data)
                  length += len(data)
                  if bar is not None:
                      bar.report(length, content_length)
              w.flush()
              if lockf is not None:
                  # On Unix, rename while still protected by the lock.
                  try:
                      os.rename(tempname, path)
                  except Exception as e:
                      raise IOError('error renaming {0} to {1} - {2}'.format(
                          tempname, path, e))
          except Exception as e:
              raise IOError('error getting {0} - {1}'.format(url, e))
          finally:
              if lockf is not None:
                  lockf(fd, LOCK_UN)
      if lockf is None:
          # On Windows, rename here because the file needs to be closed first.
          try:
              _replace(tempname, path)
          except Exception as e:
              raise IOError('error renaming {0} to {1} - {2}'.format(
                  tempname, path, e))
code_tokens: def download ( url , path , verbose = None , blocksize = 128 * 1024 ) : tempname = path + '.download' try : connection = urlopen ( url ) except Exception as e : e2 = IOError ( 'cannot get {0} because {1}' . format ( url , e ) ) e2 . __cause__ = None raise e2 if verbose is None : verbose = sys . stderr . isatty ( ) bar = None if verbose : if _running_IDLE : print ( 'Downloading {0} ...' . format ( os . path . basename ( path ) ) , file = sys . stderr ) else : bar = ProgressBar ( path ) content_length = int ( connection . headers . get ( 'content-length' , - 1 ) ) flags = getattr ( os , 'O_BINARY' , 0 ) | os . O_CREAT | os . O_RDWR fd = os . open ( tempname , flags , 0o666 ) with os . fdopen ( fd , 'wb' ) as w : try : if lockf is not None : fd = w . fileno ( ) lockf ( fd , LOCK_EX ) if os . path . exists ( path ) : if os . path . exists ( tempname ) : os . unlink ( tempname ) return w . seek ( 0 ) length = 0 while True : data = connection . read ( blocksize ) if not data : break w . write ( data ) length += len ( data ) if bar is not None : bar . report ( length , content_length ) w . flush ( ) if lockf is not None : try : os . rename ( tempname , path ) except Exception as e : raise IOError ( 'error renaming {0} to {1} - {2}' . format ( tempname , path , e ) ) except Exception as e : raise IOError ( 'error getting {0} - {1}' . format ( url , e ) ) finally : if lockf is not None : lockf ( fd , LOCK_UN ) if lockf is None : try : _replace ( tempname , path ) except Exception as e : raise IOError ( 'error renaming {0} to {1} - {2}' . format ( tempname , path , e ) )
short_docstring: Download a file from a URL, possibly displaying a progress bar.
short_docstring_tokens: Download a file from a URL possibly displaying a progress bar .
comment: """Download a file from a URL, possibly displaying a progress bar.
  
      Saves the output to the file named by `path`.  If the URL cannot be
      downloaded or the file cannot be written, an IOError is raised.
  
      Normally, if the standard error output is a terminal, then a
      progress bar is displayed to keep the user entertained.  Specify
      `verbose=True` or `verbose=False` to control this behavior.
  
      """$SEP$# Python open() provides no way to achieve O_CREAT without also$SEP$# truncating the file, which would ruin the work of another process$SEP$# that is trying to download the same file at the same time.  So:$SEP$# only one download at a time$SEP$# did someone else finish first?$SEP$# On Unix, rename while still protected by the lock.$SEP$# On Windows, rename here because the file needs to be closed first.