Exemplo n.º 1
0
def download_to_tempfile(url, file_name=None, extension=None):
    """
    Downloads a URL contents to a tempfile.  This is useful for testing downloads.
    It will download the contents of a URL to a tempfile, which you then can 
    open and use to validate the downloaded contents.
    
    Args:
        url (str) : URL of the contents to download.
    
    Kwargs:
        file_name (str): Name of file.
        extension (str): Extension to use.
    
    Return:
        str - Returns path to the temp file.

    """
    
    if not file_name:
        file_name = generate_timestamped_string("wtf_temp_file")
    
    if extension:
        file_path = temp_path(file_name + extension)
    else:
        file_path = temp_path(file_name)
    
    webFile = urllib.urlopen(url)
    localFile = open(file_path, 'w')
    localFile.write(webFile.read())
    webFile.close()
    localFile.close()

    return file_path
Exemplo n.º 2
0
def temp_path(file_name=None):
    """
    Gets a temp path.

    Kwargs:
        file_name (str) : if file name is specified, it gets appended to the temp dir.

    Usage::

        temp_file_path = temp_path("myfile")
        copyfile("myfile", temp_file_path) # copies 'myfile' to '/tmp/myfile'

    """

    if file_name is None:
        file_name = generate_timestamped_string("wtf_temp_file")

    return os.path.join(tempfile.gettempdir(), file_name)
Exemplo n.º 3
0
 def test_generateTimeStampedString(self):
     ts_string = generate_timestamped_string("TEST", 5)
     self.assertTrue(
         len(re.findall(r'^\d{4}-\d{2}-\d{2}_\d{2}\.\d{2}\.\d{2}_TEST_.{5}$', ts_string)) == 1)