Ejemplo n.º 1
0
def test_same_file(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file)) as f1:
        with LocalFile(str(file), 'w') as f2:
            with pytest.raises(LocalFile.SameFile):
                f1.copy_to(f2)
Ejemplo n.º 2
0
def test_newline(tmpdir):
    file = tmpdir / '0.txt'

    with LocalFile(str(file), 'w') as f1:
        assert f1.newline == '\n'
        assert f1.newline_str == '\n'

    with LocalFile(str(file), 'wb') as f2:
        assert f2.newline == 10
        assert f2.newline_str == b'\n'
Ejemplo n.º 3
0
def test_copy_local(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    file2 = tmpdir / '1.txt'
    with LocalFile(str(file)) as f1:
        with LocalFile(str(file2), 'w') as f2:
            f1.copy_to(f2)

    assert file2.read_text(encoding='utf-8') == txt
Ejemplo n.º 4
0
def test_no_buffer(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file), 'rb') as f1:
        with pytest.raises(AttributeError):
            f1.buffer

    with LocalFile(str(file), 'wb') as f2:
        with pytest.raises(AttributeError):
            f2.buffer
Ejemplo n.º 5
0
def test_buffer_eq(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file)) as f1:
        with f1.buffer as buffer:
            assert buffer == f1.buffer
            assert buffer != f1
            with LocalFile(str(file)) as f2:
                assert buffer == f2.buffer
            with LocalFile(str(tmpdir / '1.txt'), 'w') as f3:
                assert buffer != f3.buffer
Ejemplo n.º 6
0
def test_call_bytes(tmpdir):
    file = tmpdir / '0.txt'

    with LocalFile(str(file), 'wb') as f:
        f.input(bin.splitlines(keepends=False))

    assert file.read_text(encoding='utf-8') == txt
Ejemplo n.º 7
0
def test_write_buffer(tmpdir):
    file = tmpdir / '0.txt'

    with LocalFile(str(file), 'w') as f:
        f.buffer.write(bin)

    assert file.read_text(encoding='utf-8') == txt
Ejemplo n.º 8
0
def test_from_stdin(tmpdir, monkeypatch):
    monkeypatch.setattr('sys.stdin', StringIO(txt))
    file = tmpdir / '0.txt'

    with StdIn() as f1:
        with LocalFile(str(file), 'w') as f2:
            f1.copy_to(f2)

    assert file.read_text(encoding='utf-8') == txt
Ejemplo n.º 9
0
def download_cmd():
    import argparse
    from stream.io.local import LocalFile
    parser = argparse.ArgumentParser()
    parser.add_argument('url', type=str)
    parser.add_argument('file', type=str)
    args = parser.parse_args()

    with HttpDownloadFile(args.url) as f1:
        with LocalFile(args.file, 'wb') as f2:
            f1.copy_to(f2)
Ejemplo n.º 10
0
def test_to_stderr(tmpdir, capsys):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file)) as f1:
        with StdErr() as f2:
            f1.copy_to(f2)

    captured = capsys.readouterr()
    assert captured.out == ''
    assert captured.err == txt
Ejemplo n.º 11
0
def test_un_gzip(tmpdir):
    file = str(tmpdir / '0.txt.gz')
    with gzip.open(file, mode='wb') as f:
        f.write(b"""#123

456
""")

    assert tuple(LocalFile(file, mode='rb').stream
                 | un_gzip) == (
                     b'#123\n',
                     b'\n',
                     b'456\n',
                 )
Ejemplo n.º 12
0
def download_cmd():
    import argparse
    from stream.io.local import LocalFile
    parser = argparse.ArgumentParser()
    parser.add_argument('bucket')
    parser.add_argument('key')
    parser.add_argument('file')
    args = parser.parse_args()

    with S3ReadFile(
            args.bucket,
            args.key,
            lines=False,
    ) as f1:
        with LocalFile(args.file, 'wb') as f2:
            f1.copy_to(f2)
Ejemplo n.º 13
0
def download_cmd():
    import argparse
    from stream.io.local import LocalFile
    parser = argparse.ArgumentParser()
    parser.add_argument('url', type=str, nargs='?')
    parser.add_argument('--host', '-H', type=str)
    parser.add_argument('--path', '-P', type=str)
    parser.add_argument('file', type=str)
    args = parser.parse_args()

    if args.url:
        host, path = FtpReadFile.split_url(args.url)
    else:
        host = args.host
        path = args.path

    with FtpReadFile(
            host,
            path,
    ) as f1:
        with LocalFile(args.file, 'wb') as f2:
            f1.dump_to_buffer(f2)
Ejemplo n.º 14
0
def test_next(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file)) as f:
        assert next(f) == '123\n'
Ejemplo n.º 15
0
def test_read_buffer(tmpdir):
    file = tmpdir / '0.txt'
    file.write_text(txt, encoding='utf-8')

    with LocalFile(str(file)) as f:
        assert f.buffer.read() == bin