Beispiel #1
0
def as_path(path: PathLike) -> ReadWritePath:
    """Create a generic `pathlib.Path`-like abstraction.

  Depending on the input (e.g. `gs://` url, `ResourcePath`,...), the
  system (Windows, Linux,...), the function will create the right pathlib-like
  abstraction.

  Args:
    path: Pathlike object.

  Returns:
    path: The `pathlib.Path`-like abstraction.
  """
    is_windows = os.name == 'nt'
    if isinstance(path, str):
        if is_windows and not path.startswith(gpath.URI_PREFIXES):
            return gpath.WindowsGPath(path)
        else:
            return gpath.PosixGPath(
                path)  # On linux, or for `gs://`, uses `GPath`
    elif isinstance(path, tuple(_PATHLIKE_CLS)):
        return path  # Forward resource path, gpath,... as-is  # pytype: disable=bad-return-type
    elif isinstance(path, os.PathLike):  # Other `os.fspath` compatible objects
        path_cls = gpath.WindowsGPath if is_windows else gpath.PosixGPath
        return path_cls(path)
    else:
        raise TypeError(f'Invalid path type: {path!r}')
def as_path(path: PathLike) -> ReadWritePath:
  """Create a generic `pathlib.Path`-like abstraction.

  Depending on the input (e.g. `gs://`, `github://`, `ResourcePath`,...), the
  system (Windows, Linux,...), the function will create the right pathlib-like
  abstraction.

  Args:
    path: Pathlike object.

  Returns:
    path: The `pathlib.Path`-like abstraction.
  """
  is_windows = os.name == 'nt'
  if isinstance(path, str):
    uri_splits = path.split('://', maxsplit=1)
    if len(uri_splits) > 1:  # str is URI (e.g. `gs://`, `github://`,...)
      # On windows, `PosixGPath` is created for `gs://` paths
      return _URI_PREFIXES_TO_CLS[uri_splits[0] + '://'](path)  # pytype: disable=bad-return-type
    elif is_windows:
      return gpath.WindowsGPath(path)
    else:
      return gpath.PosixGPath(path)
  elif isinstance(path, _PATHLIKE_CLS):
    return path  # Forward resource path, gpath,... as-is  # pytype: disable=bad-return-type
  elif isinstance(path, os.PathLike):  # Other `os.fspath` compatible objects
    path_cls = gpath.WindowsGPath if is_windows else gpath.PosixGPath
    return path_cls(path)
  else:
    raise TypeError(f'Invalid path type: {path!r}')
def test_repr_windows():
    path = gpathlib.WindowsGPath('C:\\Program Files\\Directory')
    assert isinstance(path, gpathlib.WindowsGPath)
    assert str(path) == 'C:\\Program Files\\Directory'
    assert os.fspath(path) == 'C:\\Program Files\\Directory'

    path = path.parent / 'other/file.json'
    assert isinstance(path, gpathlib.WindowsGPath)
    assert os.fspath(path) == 'C:\\Program Files\\other\\file.json'