Skip to content

Commit

Permalink
Docs: Replace Python SDK with new High Level SDK (#7123)
Browse files Browse the repository at this point in the history
* Docs: Replace Python SDK with new High Level SDK

* Fix client docs

* minor doc fixes

* Fix client docs
  • Loading branch information
N-o-Z authored Dec 10, 2023
1 parent 9c00264 commit 8d73a1b
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 202 deletions.
6 changes: 3 additions & 3 deletions clients/python-wrapper/lakefs/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class Branch(Reference):
Class representing a branch in lakeFS.
"""

def _get_commit(self):
def get_commit(self):
"""
For branches override the default _get_commit method to ensure we always fetch the latest head
"""
self._commit = None
return super()._get_commit()
return super().get_commit()

def create(self, source_reference_id: str | Reference, exist_ok: bool = False) -> Branch:
"""
Expand Down Expand Up @@ -137,7 +137,7 @@ def object(self, path: str) -> WriteableObject:

return WriteableObject(self.repo_id, self._id, path, client=self._client)

def uncommitted(self, max_amount: Optional[int], after: Optional[str] = None, prefix: Optional[str] = None,
def uncommitted(self, max_amount: Optional[int] = None, after: Optional[str] = None, prefix: Optional[str] = None,
**kwargs) -> Generator[Change]:
"""
Returns a diff generator of uncommitted changes on this branch
Expand Down
5 changes: 0 additions & 5 deletions clients/python-wrapper/lakefs/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
"""
lakeFS Client module
Handles authentication against the lakeFS server and wraps the underlying lakefs_sdk client.
The client module holds a DefaultClient which will attempt to initialize on module loading using
environment credentials.
In case no credentials exist, a call to init() will be required or a Client object must be created explicitly
"""

from __future__ import annotations
Expand Down
7 changes: 7 additions & 0 deletions clients/python-wrapper/lakefs/namedtuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ def __eq__(self, other):
return False

return True

def __str__(self):
fields = {}
for k, v in self.__dict__.items():
if k != "unknown" and k[0] != "_": # Filter internal and unknown fields
fields[k] = v
return str(fields)
2 changes: 1 addition & 1 deletion clients/python-wrapper/lakefs/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def __repr__(self):

def upload(self,
data: str | bytes,
mode: WriteModes = 'wb',
mode: WriteModes = 'w',
pre_sign: Optional[bool] = None,
content_type: Optional[str] = None,
metadata: Optional[dict[str, str]] = None) -> WriteableObject:
Expand Down
27 changes: 8 additions & 19 deletions clients/python-wrapper/lakefs/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,20 @@ def log(self, max_amount: Optional[int] = None, **kwargs) -> Generator[Commit]:
max_amount=max_amount, **kwargs):
yield Commit(**res.dict())

def _get_commit(self):
def get_commit(self) -> Commit:
"""
Returns the underlying commit referenced by this reference id
:raise NotFoundException: if this reference does not exist
:raise NotAuthorizedException: if user is not authorized to perform this operation
:raise ServerException: for any other errors
"""
if self._commit is None:
with api_exception_handler():
commit = self._client.sdk_client.commits_api.get_commit(self._repo_id, self._id)
self._commit = Commit(**commit.dict())
return self._commit

def metadata(self) -> dict[str, str]:
"""
Return commit metadata for this reference id
"""
return self._get_commit().metadata

def commit_message(self) -> str:
"""
Return commit message for this reference id
"""
return self._get_commit().message

def commit_id(self) -> str:
"""
Return commit id for this reference id
"""
return self._get_commit().id

def diff(self,
other_ref: str | Reference,
max_amount: Optional[int] = None,
Expand Down
3 changes: 3 additions & 0 deletions clients/python-wrapper/lakefs/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ def id(self) -> str:
def __repr__(self) -> str:
return f'Repository(id="{self.id}")'

def __str__(self):
return str(self.properties)


def repositories(client: Client = None,
prefix: Optional[str] = None,
Expand Down
12 changes: 6 additions & 6 deletions clients/python-wrapper/tests/integration/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_import_manager(setup_repo):
res = mgr.run()
assert res.error is None
assert res.completed
assert res.commit.id == branch.commit_id()
assert res.commit.id == branch.get_commit().id
assert res.commit.message == "my imported data"
assert res.commit.metadata.get("foo") == "bar"
assert res.ingested_objects == 0
Expand All @@ -59,7 +59,7 @@ def test_import_manager(setup_repo):

assert res.error is None
assert res.completed
assert res.commit.id == branch.commit_id()
assert res.commit.id == branch.get_commit().id
assert res.commit.message == mgr.commit_message
assert res.commit.metadata.get("foo") is None
assert res.ingested_objects == 4207
Expand All @@ -73,8 +73,8 @@ def test_import_manager_cancel(setup_repo):
clt, repo = setup_repo
skip_on_unsupported_blockstore(clt, "s3")
branch = repo.branch("import-branch").create("main")
expected_commit_id = branch.commit_id()
expected_commit_message = branch.commit_message()
expected_commit_id = branch.get_commit().id
expected_commit_message = branch.get_commit().message

mgr = branch.import_data(commit_message="my imported data", metadata={"foo": "bar"})
mgr.prefix(_IMPORT_PATH, "import/")
Expand All @@ -88,8 +88,8 @@ def test_import_manager_cancel(setup_repo):
mgr.cancel()

status = mgr.status()
assert branch.commit_id() == expected_commit_id
assert branch.commit_message() == expected_commit_message
assert branch.get_commit().id == expected_commit_id
assert branch.get_commit().message == expected_commit_message
assert not status.completed
assert "Canceled" in status.error.message
assert len(mgr.sources) == 1
6 changes: 3 additions & 3 deletions clients/python-wrapper/tests/integration/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_reference_diff(setup_branch_with_commits):
branch = setup_branch_with_commits

commits = list(branch.log(max_amount=2))
assert len(list(branch.diff(branch.commit_id()))) == 0
assert len(list(branch.diff(branch.get_commit().id))) == 0
changes = list(branch.diff(commits[0].id, type="two_dot"))
assert len(changes) == 0

Expand Down Expand Up @@ -51,11 +51,11 @@ def test_reference_merge_into(setup_branch_with_commits):
other_branch = repo.branch("test_reference_merge_into").create(main)
ref = repo.ref(commits[1].id)
ref.merge_into(other_branch, message="Merge1")
assert other_branch.commit_message() == "Merge1"
assert other_branch.get_commit().message == "Merge1"
assert list(other_branch.log(max_amount=2))[1].id == commits[1].id

branch.merge_into(other_branch.id, message="Merge2")
assert other_branch.commit_message() == "Merge2"
assert other_branch.get_commit().message == "Merge2"
assert list(other_branch.log(max_amount=3))[2].id == commits[0].id


Expand Down
12 changes: 6 additions & 6 deletions clients/python-wrapper/tests/integration/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def test_ref_sanity(setup_repo):
ref = repo.ref(ref_id)
assert ref.repo_id == repo.properties.id
assert ref.id == ref_id
assert ref.metadata() == {}
assert ref.commit_message() == "Repository created"
assert ref.get_commit().metadata == {}
assert ref.get_commit().message == "Repository created"


def test_tag_sanity(setup_repo):
Expand All @@ -70,14 +70,14 @@ def test_tag_sanity(setup_repo):

# expect not found
with expect_exception_context(NotFoundException):
tag.commit_message()
tag.get_commit()

commit = repo.commit("main")
res = tag.create(commit.id)
assert res == tag
assert tag.id == tag_name
assert tag.metadata() == commit.metadata()
assert tag.commit_message() == commit.commit_message()
assert tag.get_commit().metadata == commit.get_commit().metadata
assert tag.get_commit().message == commit.get_commit().message

# Create again
with expect_exception_context(ConflictException):
Expand All @@ -92,7 +92,7 @@ def test_tag_sanity(setup_repo):

# expect not found
with expect_exception_context(NotFoundException):
tag.metadata()
tag.get_commit()

# Delete twice
with expect_exception_context(NotFoundException):
Expand Down
Loading

0 comments on commit 8d73a1b

Please sign in to comment.