Пример #1
0
def test_request_save_sources():

    # Get test data
    fname = "images/chelsea.png"
    filename = get_remote_file(fname, test_dir)
    with open(filename, "rb") as f:
        bytes = f.read()
    assert len(bytes) > 0

    # Prepare destinations
    fname2 = fname + ".out"
    filename2 = os.path.join(test_dir, fname2)
    zipfilename2 = os.path.join(test_dir, "test2.zip")
    file2 = None

    # Write an image into many different destinations
    # Do once via file and ones via local filename
    for file_or_filename in range(2):
        # Clear destinations
        for xx in (filename2, zipfilename2):
            if os.path.isfile(xx):
                os.remove(xx)
        file2 = BytesIO()
        # Write to four destinations
        for uri in (
                filename2,
                os.path.join(zipfilename2, fname2),
                file2,
                imageio.RETURN_BYTES,  # This one last to fill `res`
        ):
            R = Request(uri, "wi")
            if file_or_filename == 0:
                R.get_file().write(bytes)  # via file
            else:
                with open(R.get_local_filename(), "wb") as f:
                    f.write(bytes)  # via local
            R.finish()
            res = R.get_result()
        # Test four results
        with open(filename2, "rb") as f:
            assert f.read() == bytes
        with ZipFile(zipfilename2, "r") as zf:
            assert zf.open(fname2).read() == bytes
        assert file2.getvalue() == bytes
        assert res == bytes
Пример #2
0
def test_request_save_sources():

    # Get test data
    fname = "images/chelsea.png"
    filename = get_remote_file(fname, test_dir)
    with open(filename, "rb") as f:
        bytes = f.read()
    assert len(bytes) > 0

    # Prepare destinations
    fname2 = fname + ".out"
    filename2 = os.path.join(test_dir, fname2)
    zipfilename2 = os.path.join(test_dir, "test2.zip")
    file2 = None

    # Write an image into many different destinations
    # Do once via file and ones via local filename
    for file_or_filename in range(2):
        # Clear destinations
        for xx in (filename2, zipfilename2):
            if os.path.isfile(xx):
                os.remove(xx)
        file2 = BytesIO()
        # Write to four destinations
        for uri in (
            filename2,
            os.path.join(zipfilename2, fname2),
            file2,
            imageio.RETURN_BYTES,  # This one last to fill `res`
        ):
            R = Request(uri, "wi")
            if file_or_filename == 0:
                R.get_file().write(bytes)  # via file
            else:
                with open(R.get_local_filename(), "wb") as f:
                    f.write(bytes)  # via local
            R.finish()
            res = R.get_result()
        # Test four results
        with open(filename2, "rb") as f:
            assert f.read() == bytes
        with ZipFile(zipfilename2, "r") as zf:
            assert zf.open(fname2).read() == bytes
        assert file2.getvalue() == bytes
        assert res == bytes
Пример #3
0
def test_request_read_sources():

    # Make an image available in many ways
    fname = "images/chelsea.png"
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, "rb").read()
    #
    burl = "https://raw.githubusercontent.com/imageio/imageio-binaries/master/"
    zipfilename = os.path.join(test_dir, "test1.zip")
    with ZipFile(zipfilename, "w") as zf:
        zf.writestr(fname, bytes)

    has_inet = os.getenv("IMAGEIO_NO_INTERNET", "") not in ("1", "yes", "true")

    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for file_or_filename in range(2):
        for check_first_bytes in range(2):

            # Define uris to test. Define inside loop, since we need fresh files
            uris = [
                filename,
                os.path.join(zipfilename, fname),
                bytes,
                memoryview(bytes),
                open(filename, "rb"),
            ]
            if has_inet:
                uris.append(burl + fname)

            for uri in uris:
                R = Request(uri, "ri")
                if check_first_bytes:
                    first_bytes = R.firstbytes
                if file_or_filename == 0:
                    all_bytes = R.get_file().read()
                else:
                    with open(R.get_local_filename(), "rb") as f:
                        all_bytes = f.read()
                R.finish()
                assert bytes == all_bytes
                if check_first_bytes:
                    assert len(first_bytes) > 0
                    assert all_bytes.startswith(first_bytes)
Пример #4
0
def test_request_read_sources():

    # Make an image available in many ways
    fname = "images/chelsea.png"
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, "rb").read()
    #
    burl = "https://raw.githubusercontent.com/imageio/imageio-binaries/master/"
    zipfilename = os.path.join(test_dir, "test1.zip")
    with ZipFile(zipfilename, "w") as zf:
        zf.writestr(fname, bytes)

    has_inet = os.getenv("IMAGEIO_NO_INTERNET", "") not in ("1", "yes", "true")

    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for file_or_filename in range(2):
        for check_first_bytes in range(2):

            # Define uris to test. Define inside loop, since we need fresh files
            uris = [
                filename,
                os.path.join(zipfilename, fname),
                bytes,
                memoryview(bytes),
                open(filename, "rb"),
            ]
            if has_inet:
                uris.append(burl + fname)

            for uri in uris:
                R = Request(uri, "ri")
                if check_first_bytes:
                    first_bytes = R.firstbytes
                if file_or_filename == 0:
                    all_bytes = R.get_file().read()
                else:
                    with open(R.get_local_filename(), "rb") as f:
                        all_bytes = f.read()
                R.finish()
                assert bytes == all_bytes
                if check_first_bytes:
                    assert len(first_bytes) > 0
                    assert all_bytes.startswith(first_bytes)
Пример #5
0
def test_request_read_sources(test_images, tmp_userdir):

    # Make an image available in many ways
    fname = "chelsea.png"
    filename = test_images / fname
    bytes = open(str(filename), "rb").read()
    #
    burl = "https://raw.githubusercontent.com/imageio/test_images/master/"
    zipfilename = tmp_userdir / "test1.zip"
    with ZipFile(zipfilename, "w") as zf:
        zf.writestr(fname, bytes)

    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for file_or_filename in range(2):
        for check_first_bytes in range(2):

            # Define uris to test. Define inside loop, since we need fresh files
            uris = [
                filename,
                os.path.join(zipfilename, fname),
                bytes,
                memoryview(bytes),
                open(filename, "rb"),
            ]
            uris.append(burl + fname)

            for uri in uris:
                R = Request(uri, "ri")
                if check_first_bytes:
                    first_bytes = R.firstbytes
                if file_or_filename == 0:
                    all_bytes = R.get_file().read()
                else:
                    with open(R.get_local_filename(), "rb") as f:
                        all_bytes = f.read()
                R.finish()
                assert bytes == all_bytes
                if check_first_bytes:
                    assert len(first_bytes) > 0
                    assert all_bytes.startswith(first_bytes)
Пример #6
0
def test_request_read_sources():

    # Make an image available in many ways
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    burl = 'https://raw.githubusercontent.com/imageio/imageio-binaries/master/'
    z = ZipFile(os.path.join(test_dir, 'test.zip'), 'w')
    z.writestr(fname, bytes)
    z.close()

    has_inet = os.getenv('IMAGEIO_NO_INTERNET', '') not in ('1', 'yes', 'true')

    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for X in range(2):

        # Define uris to test. Define inside loop, since we need fresh files
        uris = [
            filename,
            os.path.join(test_dir, 'test.zip', fname), bytes,
            open(filename, 'rb')
        ]
        if has_inet:
            uris.append(burl + fname)

        for uri in uris:
            R = Request(uri, 'ri')
            first_bytes = R.firstbytes
            if X == 0:
                all_bytes = R.get_file().read()
            else:
                f = open(R.get_local_filename(), 'rb')
                all_bytes = f.read()
            R.finish()
            # Test
            assert len(first_bytes) > 0
            assert all_bytes.startswith(first_bytes)
            assert bytes == all_bytes
Пример #7
0
def test_request_read_sources():
    
    # Make an image available in many ways
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    burl = 'https://raw.githubusercontent.com/imageio/imageio-binaries/master/'
    z = ZipFile(os.path.join(test_dir, 'test.zip'), 'w')
    z.writestr(fname, bytes)
    z.close()
    
    has_inet = os.getenv('IMAGEIO_NO_INTERNET', '') not in ('1', 'yes', 'true')
    
    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for X in range(2):
        
        # Define uris to test. Define inside loop, since we need fresh files
        uris = [filename,
                os.path.join(test_dir, 'test.zip', fname),
                bytes,
                open(filename, 'rb')]
        if has_inet:
            uris.append(burl + fname)
        
        for uri in uris:
            R = Request(uri, 'ri')
            first_bytes = R.firstbytes
            if X == 0:
                all_bytes = R.get_file().read()
            else:
                f = open(R.get_local_filename(), 'rb')
                all_bytes = f.read()
            R.finish()
            # Test
            assert len(first_bytes) > 0
            assert all_bytes.startswith(first_bytes)
            assert bytes == all_bytes
Пример #8
0
def test_request_save_sources():

    # Prepare desinations
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    fname2 = fname + '.out'
    filename2 = os.path.join(test_dir, fname2)
    zipfilename2 = os.path.join(test_dir, 'test.zip')
    file2 = BytesIO()

    # Write an image into many different destinations
    # Do once via file and ones via local filename
    for i in range(2):
        # Clear
        for xx in (filename2, zipfilename2):
            if os.path.isfile(xx):
                os.remove(xx)
        # Write to three destinations
        for uri in (
                filename2,
                os.path.join(zipfilename2, fname2),
                file2,
                imageio.RETURN_BYTES  # This one last to fill `res`
        ):
            R = Request(uri, 'wi')
            if i == 0:
                R.get_file().write(bytes)  # via file
            else:
                open(R.get_local_filename(), 'wb').write(bytes)  # via local
            R.finish()
            res = R.get_result()
        # Test three results
        assert open(filename2, 'rb').read() == bytes
        assert ZipFile(zipfilename2, 'r').open(fname2).read() == bytes
        assert res == bytes
Пример #9
0
def test_request_save_sources():
    
    # Prepare desinations
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    fname2 = fname + '.out'
    filename2 = os.path.join(test_dir, fname2)
    zipfilename2 = os.path.join(test_dir, 'test.zip')
    file2 = BytesIO()
    
    # Write an image into many different destinations
    # Do once via file and ones via local filename
    for i in range(2):
        # Clear
        for xx in (filename2, zipfilename2):
            if os.path.isfile(xx):
                os.remove(xx)
        # Write to three destinations
        for uri in (filename2, 
                    os.path.join(zipfilename2, fname2),
                    file2,
                    imageio.RETURN_BYTES  # This one last to fill `res`
                    ):
            R = Request(uri, 'wi')
            if i == 0:
                R.get_file().write(bytes)  # via file
            else:
                open(R.get_local_filename(), 'wb').write(bytes)  # via local
            R.finish()
            res = R.get_result()
        # Test three results
        assert open(filename2, 'rb').read() == bytes
        assert ZipFile(zipfilename2, 'r').open(fname2).read() == bytes
        assert res == bytes
Пример #10
0
def test_request_read_sources():
    
    # Make an image available in many ways
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    burl = 'https://raw.githubusercontent.com/imageio/imageio-binaries/master/'
    z = ZipFile(os.path.join(test_dir, 'test.zip'), 'w')
    z.writestr(fname, bytes)
    z.close()
    file = open(filename, 'rb')
    
    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for uri in (filename, 
                os.path.join(test_dir, 'test.zip', fname),
                bytes,
                file,
                burl + fname):
        # Init
        firsbytes_list, bytes_list = [], []
        # Via file
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        bytes_list.append(R.get_file().read())
        R.finish()
        # Via local filename
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        f = open(R.get_local_filename(), 'rb')
        bytes_list.append(f.read())
        R.finish()
        # Test repeated
        if uri == filename:
            bytes_list.append(R.get_file().read())
            f = open(R.get_local_filename(), 'rb')
            bytes_list.append(f.read())
            R.finish()
        # Test
        for i in range(len(firsbytes_list)):
            assert len(firsbytes_list[i]) > 0
            assert bytes.startswith(firsbytes_list[i])
        for i in range(len(bytes_list)):
            assert bytes == bytes_list[i]
Пример #11
0
def test_request_read_sources():
    
    # Make an image available in many ways
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    #
    burl = 'https://raw.githubusercontent.com/imageio/imageio-binaries/master/'
    z = ZipFile(os.path.join(test_dir, 'test.zip'), 'w')
    z.writestr(fname, bytes)
    z.close()
    file = open(filename, 'rb')
    
    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for uri in (filename, 
                os.path.join(test_dir, 'test.zip', fname),
                bytes,
                file,
                burl + fname):
        # Init
        firsbytes_list, bytes_list = [], []
        # Via file
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        bytes_list.append(R.get_file().read())
        R.finish()
        # Via local filename
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        f = open(R.get_local_filename(), 'rb')
        bytes_list.append(f.read())
        R.finish()
        # Test repeated
        if uri == filename:
            bytes_list.append(R.get_file().read())
            f = open(R.get_local_filename(), 'rb')
            bytes_list.append(f.read())
            R.finish()
        # Test
        for i in range(len(firsbytes_list)):
            assert len(firsbytes_list[i]) > 0
            assert bytes.startswith(firsbytes_list[i])
        for i in range(len(bytes_list)):
            assert bytes == bytes_list[i]
Пример #12
0
def test_request():
    """ Test request object """
    
    # Check uri-type, this is not a public property, so we test the private
    R = Request('http://example.com', 'ri')
    assert R._uri_type == core.request.URI_HTTP
    R = Request('ftp://example.com', 'ri')
    assert R._uri_type == core.request.URI_FTP
    R = Request('file://example.com', 'wi')
    assert R._uri_type == core.request.URI_FILENAME
    R = Request('<video0>', 'rI')
    assert R._uri_type == core.request.URI_BYTES
    R = Request(imageio.RETURN_BYTES, 'wi')
    assert R._uri_type == core.request.URI_BYTES
    #
    fname = get_remote_file('images/chelsea.png', test_dir)
    R = Request(fname, 'ri')
    assert R._uri_type == core.request.URI_FILENAME
    R = Request('/file/that/does/not/exist', 'wi')
    assert R._uri_type == core.request.URI_FILENAME  # Too short to be bytes
    R = Request(b'x'*600, 'ri')
    assert R._uri_type == core.request.URI_BYTES
    R = Request(sys.stdin, 'ri')
    assert R._uri_type == core.request.URI_FILE
    R = Request(sys.stdout, 'wi')
    assert R._uri_type == core.request.URI_FILE
    # exapand user dir
    R = Request('~/foo', 'wi')
    assert R.filename == os.path.expanduser('~/foo')
    # zip file
    R = Request('/foo/bar.zip/spam.png', 'wi')
    assert R._uri_type == core.request.URI_ZIPPED
    
    # Test failing inits
    raises(ValueError, Request, '/some/file', None)  # mode must be str
    raises(ValueError, Request, '/some/file', 3)  # mode must be str
    raises(ValueError, Request, '/some/file', '')  # mode must be len 2
    raises(ValueError, Request, '/some/file', 'r')  # mode must be len 2
    raises(ValueError, Request, '/some/file', 'rii')  # mode must be len 2
    raises(ValueError, Request, '/some/file', 'xi')  # mode[0] must be in rw
    raises(ValueError, Request, '/some/file', 'rx')  # mode[1] must be in iIvV?
    #
    raises(IOError, Request, ['invalid', 'uri'] * 10, 'ri')  # invalid uri
    raises(IOError, Request, 4, 'ri')  # invalid uri
    raises(IOError, Request, '/does/not/exist', 'ri')  # reading nonexistent
    raises(IOError, Request, '/does/not/exist.zip/spam.png', 'ri')  # dito
    raises(IOError, Request, 'http://example.com', 'wi')  # no writing here
    
    # Test auto-download
    R = Request('chelsea.png', 'ri')
    assert R.filename == get_remote_file('images/chelsea.png')
    R = Request('chelsea.zip/chelsea.png', 'ri')
    assert R._filename_zip[0] == get_remote_file('images/chelsea.zip')
    assert R.filename == get_remote_file('images/chelsea.zip') + '/chelsea.png'
    
    # Make an image available in many ways
    burl = 'https://raw.githubusercontent.com/imageio/imageio-binaries/master/'
    fname = 'images/chelsea.png'
    filename = get_remote_file(fname, test_dir)
    bytes = open(filename, 'rb').read()
    z = ZipFile(os.path.join(test_dir, 'test.zip'), 'w')
    z.writestr(fname, bytes)
    z.close()
    file = open(filename, 'rb')
    
    # Read that image from these different sources. Read data from file
    # and from local file (the two main plugin-facing functions)
    for uri in (filename, 
                os.path.join(test_dir, 'test.zip', fname),
                bytes,
                file,
                burl + fname):
        # Init
        firsbytes_list, bytes_list = [], []
        # Via file
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        bytes_list.append(R.get_file().read())
        R.finish()
        # Via local filename
        R = Request(uri, 'ri')
        firsbytes_list.append(R.firstbytes)
        f = open(R.get_local_filename(), 'rb')
        bytes_list.append(f.read())
        R.finish()
        # Test repeated
        if uri == filename:
            bytes_list.append(R.get_file().read())
            f = open(R.get_local_filename(), 'rb')
            bytes_list.append(f.read())
            R.finish()
        # Test
        for i in range(len(firsbytes_list)):
            assert len(firsbytes_list[i]) > 0
            assert bytes.startswith(firsbytes_list[i])
        for i in range(len(bytes_list)):
            assert bytes == bytes_list[i]
    
    # Prepare desinations
    fname2 = fname + '.out'
    filename2 = os.path.join(test_dir, fname2)
    zipfilename2 = os.path.join(test_dir, 'test.zip')
    file2 = BytesIO()
    
    # Write an image into many different destinations
    # Do once via file and ones via local filename
    for i in range(2):
        # Clear
        for xx in (filename2, zipfilename2):
            if os.path.isfile(xx):
                os.remove(xx)
        # Write to three destinations
        for uri in (filename2, 
                    os.path.join(zipfilename2, fname2),
                    file2,
                    imageio.RETURN_BYTES  # This one last to fill `res`
                    ):
            R = Request(uri, 'wi')
            if i == 0:
                R.get_file().write(bytes)  # via file
            else:
                open(R.get_local_filename(), 'wb').write(bytes)  # via local
            R.finish()
            res = R.get_result()
        # Test three results
        assert open(filename2, 'rb').read() == bytes
        assert ZipFile(zipfilename2, 'r').open(fname2).read() == bytes
        assert res == bytes