Exemple #1
0
def copy(source: str, dest: str):
    """Copy the contents of the origin URL into the dest url.

    Depending on the type of resource this can be much more performant than
    >>> with tio.open(source) as reader, tio.open(dest) as writer:
            writer.write(reader.read())
    """
    source_auth = credentials.authenticate(source)
    dest_auth = credentials.authenticate(dest)
    copier = COPIER_REGISTRY.get_handler(source_auth.scheme + "+" +
                                         dest_auth.scheme)
    copier.copy(source_auth, dest_auth)
Exemple #2
0
def _open_reader(url: str, mode: str,
                 **kwargs) -> ContextManager[protocols.Reader]:
    """Open a url and return a reader."""
    authenticated = authenticate(url)
    return STREAM_HANDLER_REGISTRY.open_stream_reader(authenticated,
                                                      mode,
                                                      extras=kwargs)
Exemple #3
0
def test_authenticate(mocker):

    injector = credentials.CredentialsInjector()
    injector.register_credentials(urls.URL("ftp://*****:*****@google.com"))
    mock_cred = mocker.patch(
        "tentaclio.credentials.api.load_credentials_injector")
    mock_cred.return_value = injector
    authenticated = credentials.authenticate("ftp://google.com/myfile.csv")
    assert authenticated.username == "user"
    assert authenticated.password == "pass"
def test_authenticated_api_calls(fixture_client, fixture_df):
    with tentaclio.open(
            f"postgresql://hostname{fixture_client.url.path}::{TEST_TABLE_NAME}",
            mode="w") as writer:
        fixture_df.to_csv(writer, index=False)

    with clients.PostgresClient(
            credentials.authenticate(
                f"postgresql://hostname{fixture_client.url.path}")) as client:
        retrieved_df = client.get_df(f"select * from {TEST_TABLE_NAME}")

    assert retrieved_df.equals(fixture_df)
Exemple #5
0
def test_list_folders():
    data = "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn"
    cli = tentaclio.clients.SFTPClient(authenticate("sftp://hostname"))

    with cli:
        cli.conn.makedirs("upload/folder1/folder2")

    with tentaclio.open("sftp://hostname/upload/folder1/data.txt", mode="w") as f:
        f.write(data)

    ls = list(tentaclio.listdir("sftp://hostname/upload/folder1"))
    assert any("upload/folder1/data.txt" in entry for entry in ls)
    assert any("upload/folder1/folder2" in entry for entry in ls)
Exemple #6
0
def scandir(url: str) -> Iterable[DirEntry]:
    """Scan a directory-like url returning its entries.

    Return an iterator of tentaclio.fs.DirEntry objects corresponding
    to the entries in the directory given by url. The entries
    are yielded in arbitrary order,
    and the special entries '.' and '..' are not included.

    More info:
    https://docs.python.org/3/library/os.html#os.scandir
    """
    authenticated = credentials.authenticate(url)
    return SCANNER_REGISTRY.get_handler(
        authenticated.scheme).scandir(authenticated)
Exemple #7
0
def remove(url: str):
    """Delete the resource identified by the url."""
    authenticated = credentials.authenticate(url)
    REMOVER_REGISTRY.get_handler(authenticated.scheme).remove(authenticated)
Exemple #8
0
def db(url: str, **kwargs) -> Db:
    """Create an authenticated db client."""
    authenticated = credentials.authenticate(url)
    factory = DB_REGISTRY.get_handler(authenticated.scheme)
    return factory(authenticated, **kwargs)