示例#1
0
def getFromURL(url):
    """
    Get a remote file and save in a temporary directory.
    Return the path to the local file.
    """
    tmpdir = tempfile.mkdtemp()
    localfilepath = os.path.join(tmpdir, os.path.basename(url))
    srblib.urlretrieve(url, localfilepath)
    logging.debug('  getFromURL("%s") --> %s' % (url, localfilepath))
    return localfilepath
示例#2
0
def getFromURL(url):
    """
    Get a remote file and save in a temporary directory.
    Return the path to the local file.
    """
    tmpdir = tempfile.mkdtemp()
    localfilepath = os.path.join(tmpdir, os.path.basename(url))
    srblib.urlretrieve(url, localfilepath)
    logging.debug('  getFromURL("%s") --> %s' % (url, localfilepath))
    return localfilepath
示例#3
0
def hdf5_to_png(input_url, output_url):
    """
    Converts a movie in FACETS HDF5 format to a 'zipped png' format, i.e. a
    zipped folder containing one png file for each frame of the movie, plus a
    'frames' text file listing the png files in the order they appear in the
    movie, and a 'parameters' text file giving the frame_duration parameter.
    """
    if not use_hdf5:
        raise Exception("PyTables not installed.")
    tmpdir_list = []
    assert output_url[-3:] == 'zip', "output_url must point to a zip file"
    assert output_url[0:7] == 'file://', "for now, must be a file:// url"
    input_protocol = input_url.split('://')[0]
    # if remote file, create local copy
    if input_protocol != 'file':
        tmp_dir1 = tempfile.mkdtemp()
        tmpdir_list.append(tmp_dir1)
        srblib.urlretrieve(input_url, tmp_dir1 + "/tmp.h5")
        input_url = "file://" + tmp_dir1 + "/tmp.h5"
    # process local hdf5 file
    h5file = file_extension.openHDF5File(input_url, 'r')
    if isinstance(h5file.root.Movie, movie.CMovie):
        # export the movie frames as PNG files to a temporary directory
        tmp_dir = tempfile.mkdtemp()
        tmpdir_list.append(tmp_dir)
        mov = h5file.root.Movie
        png_list = mov.export('png', 'file://' + tmp_dir)
        # create and add movie frames to zip archive
        output_path = output_url.split('//')[1]
        zf = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED)
        container = os.path.basename(output_path)[:-4]  # remove .zip
        for pngfile in png_list:
            arcname = "%s/%s" % (container, os.path.basename(pngfile))
            zf.write(pngfile, arcname)
        # add 'parameters' and 'frames' files to the zip archive
        zf.writestr("%s/parameters" % container,
                    'frame_duration = %s' % mov.attrs.FRAME_DURATION)
        zf.writestr("%s/frames" % container,
                    '\n'.join([os.path.basename(p) for p in png_list]))
        zf.close()
    else:
        raise Exception("Specified node is not a Movie")
    h5file.close()
    # remove the temporary directories
    for tmpdir in tmpdir_list:
        shutil.rmtree(tmpdir)
示例#4
0
def hdf5_to_png(input_url, output_url):
    """
    Converts a movie in FACETS HDF5 format to a 'zipped png' format, i.e. a
    zipped folder containing one png file for each frame of the movie, plus a
    'frames' text file listing the png files in the order they appear in the
    movie, and a 'parameters' text file giving the frame_duration parameter.
    """
    if not use_hdf5:
        raise Exception("PyTables not installed.")
    tmpdir_list = []
    assert output_url[-3:] == "zip", "output_url must point to a zip file"
    assert output_url[0:7] == "file://", "for now, must be a file:// url"
    input_protocol = input_url.split("://")[0]
    # if remote file, create local copy
    if input_protocol != "file":
        tmp_dir1 = tempfile.mkdtemp()
        tmpdir_list.append(tmp_dir1)
        srblib.urlretrieve(input_url, tmp_dir1 + "/tmp.h5")
        input_url = "file://" + tmp_dir1 + "/tmp.h5"
    # process local hdf5 file
    h5file = file_extension.openHDF5File(input_url, "r")
    if isinstance(h5file.root.Movie, movie.CMovie):
        # export the movie frames as PNG files to a temporary directory
        tmp_dir = tempfile.mkdtemp()
        tmpdir_list.append(tmp_dir)
        mov = h5file.root.Movie
        png_list = mov.export("png", "file://" + tmp_dir)
        # create and add movie frames to zip archive
        output_path = output_url.split("//")[1]
        zf = zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED)
        container = os.path.basename(output_path)[:-4]  # remove .zip
        for pngfile in png_list:
            arcname = "%s/%s" % (container, os.path.basename(pngfile))
            zf.write(pngfile, arcname)
        # add 'parameters' and 'frames' files to the zip archive
        zf.writestr("%s/parameters" % container, "frame_duration = %s" % mov.attrs.FRAME_DURATION)
        zf.writestr("%s/frames" % container, "\n".join([os.path.basename(p) for p in png_list]))
        zf.close()
    else:
        raise Exception("Specified node is not a Movie")
    h5file.close()
    # remove the temporary directories
    for tmpdir in tmpdir_list:
        shutil.rmtree(tmpdir)