Skip to content

Commit

Permalink
feat: add resolve method
Browse files Browse the repository at this point in the history
 - converts path to absolute and removes relative parts
  • Loading branch information
justindujardin committed Apr 8, 2020
1 parent 6de4667 commit 7cebc69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
21 changes: 12 additions & 9 deletions gcspath/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from io import DEFAULT_BUFFER_SIZE
from pathlib import Path, PurePath, _Accessor, _PosixFlavour
from typing import Generator, Iterable, List, Optional, Union

import os
import smart_open
from google.api_core import exceptions as gcs_errors
from google.cloud import storage
Expand Down Expand Up @@ -160,6 +160,9 @@ def owner(self, path: "GCSPath") -> Optional[str]:
blob: Optional[ClientBlob] = self.get_blob(path)
return blob.owner if blob is not None else None

def resolve(self, path: "GCSPath", strict=False):
return GCSPath(os.path.abspath(str(path)))

def rename(self, path: "GCSPath", target: "GCSPath"):
bucket: ClientBucket = self.client.get_bucket(path)
target_bucket: ClientBucket = self.client.get_bucket(target)
Expand Down Expand Up @@ -283,14 +286,6 @@ def lstat(self):
message = self._NOT_SUPPORTED_MESSAGE.format(method=self.lstat.__qualname__)
raise NotImplementedError(message)

def resolve(self, strict=False):
"""
resolve method is unsupported on GCS service
GCS don't have this file system action concept
"""
message = self._NOT_SUPPORTED_MESSAGE.format(method=self.resolve.__qualname__)
raise NotImplementedError(message)

def symlink_to(self, *args, **kwargs):
"""
symlink_to method is unsupported on GCS service
Expand Down Expand Up @@ -431,6 +426,14 @@ def owner(self):
raise FileNotFoundError(str(self))
return self._accessor.owner(self)

def resolve(self):
"""
Return a copy of this path. All paths are absolute in buckets, so no
transformation is applied.
"""
self._absolute_path_validation()
return self._accessor.resolve(self)

def rename(self, target):
"""
Renames this file or Bucket / key prefix / key to the given target.
Expand Down
6 changes: 0 additions & 6 deletions tests/test_not_supported.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ def test_lstat():
path.lstat()


def test_resolve():
path = GCSPath("/fake-bucket/fake-key")
with pytest.raises(NotImplementedError):
path.resolve()


def test_symlink_to():
path = GCSPath("/fake-bucket/fake-key")
with pytest.raises(NotImplementedError):
Expand Down
8 changes: 8 additions & 0 deletions tests/test_path_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ def test_stat(with_adapter):
assert GCSPath("/test-bucket").stat() is None


@pytest.mark.parametrize("adapter", TEST_ADAPTERS)
def test_resolve(with_adapter):
path = GCSPath("/fake-bucket/fake-key")
assert path.resolve() == path
path = GCSPath("/fake-bucket/dir/../fake-key")
assert path.resolve() == GCSPath("/fake-bucket/fake-key")


@pytest.mark.parametrize("adapter", TEST_ADAPTERS)
def test_exists(with_adapter):
path = GCSPath("./fake-key")
Expand Down

0 comments on commit 7cebc69

Please sign in to comment.