8000 feat(files): allow decoding project files directly to string by nejch · Pull Request #2396 · python-gitlab/python-gitlab · GitHub
[go: up one dir, main page]

Skip to content

feat(files): allow decoding project files directly to string #2396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/gl_objects/projects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,12 @@ Get a file::
# get the base64 encoded content
8000 print(f.content)

# get the decoded content
# get the decoded content as bytes
print(f.decode())

# get the decoded content as a string
print(f.decode("utf-8"))

Get file details from headers, without fetching its entire content::

headers = project.files.head('README.rst', ref='main')
Expand Down
22 changes: 20 additions & 2 deletions gitlab/v4/objects/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Iterator,
List,
Optional,
overload,
TYPE_CHECKING,
Union,
)
Expand Down Expand Up @@ -41,13 +42,30 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject):
file_path: str
manager: "ProjectFileManager"

@overload
def decode(self) -> bytes:
...

@overload
def decode(self, encoding: None) -> bytes:
...

@overload
def decode(self, encoding: str) -> str:
...

def decode(self, encoding: Optional[str] = None) -> Union[bytes, str]:
"""Returns the decoded content of the file.

Returns:
The decoded content.
The decoded content as bytes.
The decoded content as string if a valid encoding is provided.
"""
return base64.b64decode(self.content)
decoded_bytes = base64.b64decode(self.content)

if encoding is not None:
return decoded_bytes.decode(encoding)
return decoded_bytes

# NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore
# type error
Expand Down
6 changes: 3 additions & 3 deletions tests/functional/api/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def test_repository_files(project):
}
)
readme = project.files.get(file_path="README.rst", ref="main")
# The first decode() is the ProjectFile method, the second one is the bytes
# object method
assert readme.decode().decode() == "Initial content"

assert readme.decode() == b"Initial content"
assert readme.decode("utf-8") == "Initial content"

headers = project.files.head("README.rst", ref="main")
assert headers["X-Gitlab-File-Path"] == "README.rst"
Expand Down
0