Exemplo n.º 1
0
def test_as_path_registering():
    @generic_path.register_pathlike_cls('my_path://')
    class MyPath(gpath.PosixGPath):
        pass

    my_path = generic_path.as_path('my_path://abc')
    assert isinstance(my_path, MyPath)
    assert generic_path.as_path(my_path) is my_path
Exemplo n.º 2
0
def test_as_path_new():

    p0 = type_utils.ReadWritePath('some/path/to/xyz')
    p1 = type_utils.ReadOnlyPath('some/path/to/xyz')
    p2 = generic_path.as_path('some/path/to/xyz')
    assert p0 == p1
    assert p0 == p2
    assert type(p0) is type(p1)
    assert type(p0) is type(p2)
Exemplo n.º 3
0
def gcs_path(*relative_path: type_utils.PathLike) -> type_utils.ReadWritePath:
    """Returns the GCS URI path.

  Args:
    *relative_path: Eventual relative path in the bucket.

  Returns:
    path: The GCS uri.
  """
    return generic_path.as_path(GCS_ROOT_DIR).joinpath(*relative_path)
def test_windows_encoding():
  with mock.patch('os.name', 'nt'):
    assert os.name == 'nt'

    # On windows, paths should be `WindowsGPath`
    path = generic_path.as_path('c:/Program Files/text.txt')
    assert isinstance(path, gpath.WindowsGPath)

    path = generic_path.as_path(pathlib.PosixPath('some_dir/abc'))
    assert isinstance(path, gpath.WindowsGPath)

    # Other `GPath` and `gs://` should be `PosixPurePath`
    path = generic_path.as_path('gs://some_dir/abc')
    assert not isinstance(path, gpath.WindowsGPath)
    assert isinstance(path, gpath.PosixGPath)

    path = generic_path.as_path(gpath.PosixGPath('some_dir/abc'))
    assert not isinstance(path, gpath.WindowsGPath)
    assert isinstance(path, gpath.PosixGPath)
def test_resource_path():
    path = resource_utils.ResourcePath(make_zip_file())
    assert isinstance(path, os.PathLike)
    assert path.joinpath('b/c.txt').read_text() == 'content of c'
    sub_dirs = list(path.joinpath('b').iterdir())
    assert len(sub_dirs) == 3
    for p in sub_dirs:  # Childs should be `ResourcePath` instances
        assert isinstance(p, resource_utils.ResourcePath)

    # Forwarded to `as_path` keep the resource.
    path = generic_path.as_path(path)
    assert isinstance(path, resource_utils.ResourcePath)

    assert path.joinpath() == path
    assert path.joinpath('abc', 'def.txt').name == 'def.txt'
Exemplo n.º 6
0
def resource_path(package: Union[str, types.ModuleType]) -> ReadOnlyPath:
    """Returns `importlib.resources.files`."""
    path = importlib_resources.files(package)  # pytype: disable=module-attr
    if isinstance(path, pathlib.Path):
        # TODO(tfds): To ensure compatibility with zipfile.Path, we should ensure
        # that the returned `pathlib.Path` isn't missused. More specifically:
        # * `os.fspath` should only be called on files (not directories)
        # * `str(path)` should be forbidden (only `__format__` allowed).
        # In practice, it is trickier to do as `__fspath__` and `__str__` are
        # called internally.
        # Convert to `GPath` for consistency and compatibility with `MockFs`.
        return generic_path.as_path(path)
    elif isinstance(path, zipfile.Path):
        path = ResourcePath(path.root, path.at)
        return typing.cast(ReadOnlyPath, path)
    else:
        raise TypeError(f'Unknown resource path: {type(path)}: {path}')
Exemplo n.º 7
0
 def __new__(cls: Type[T], *args: PathLike) -> T:
     if cls in (ReadOnlyPath, ReadWritePath):
         from tensorflow_datasets.core.utils import generic_path  # pytype: disable=import-error  # pylint: disable=g-import-not-at-top
         return generic_path.as_path(*args)
     else:
         return super().__new__(cls, *args)