| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -434,9 +434,12 @@ Get a file:: | |||
| 434 | 434 | # get the base64 encoded content | |
| 435 | 435 | print(f.content) | |
| 436 | 436 | ||
| 437 | - # get the decoded content | ||
| 437 | + # get the decoded content as bytes | ||
| 438 | 438 | print(f.decode()) | |
| 439 | 439 | ||
| 440 | + # get the decoded content as a string | ||
| 441 | + print(f.decode("utf-8")) | ||
| 442 | + | ||
| 440 | 443 | Get file details from headers, without fetching its entire content:: | |
| 441 | 444 | ||
| 442 | 445 | headers = project.files.head('README.rst', ref='main') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,6 +7,7 @@ | |||
| 7 | 7 | Iterator, | |
| 8 | 8 | List, | |
| 9 | 9 | Optional, | |
| 10 | + overload, | ||
| 10 | 11 | TYPE_CHECKING, | |
| 11 | 12 | Union, | |
| 12 | 13 | ) | |
@@ -41,13 +42,30 @@ class ProjectFile(SaveMixin, ObjectDeleteMixin, RESTObject): | |||
| 41 | 42 | file_path: str | |
| 42 | 43 | manager: "ProjectFileManager" | |
| 43 | 44 | ||
| 45 | + @overload | ||
| 44 | 46 | def decode(self) -> bytes: | |
| 47 | + ... | ||
| 48 | + | ||
| 49 | + @overload | ||
| 50 | + def decode(self, encoding: None) -> bytes: | ||
| 51 | + ... | ||
| 52 | + | ||
| 53 | + @overload | ||
| 54 | + def decode(self, encoding: str) -> str: | ||
| 55 | + ... | ||
| 56 | + | ||
| 57 | + def decode(self, encoding: Optional[str] = None) -> Union[bytes, str]: | ||
| 45 | 58 | """Returns the decoded content of the file. | |
| 46 | 59 | ||
| 47 | 60 | Returns: | |
| 48 | - The decoded content. | ||
| 61 | + The decoded content as bytes. | ||
| 62 | + The decoded content as string if a valid encoding is provided. | ||
| 49 | 63 | """ | |
| 50 | - return base64.b64decode(self.content) | ||
| 64 | + decoded_bytes = base64.b64decode(self.content) | ||
| 65 | + | ||
| 66 | + if encoding is not None: | ||
| 67 | + return decoded_bytes.decode(encoding) | ||
| 68 | + return decoded_bytes | ||
| 51 | 69 | ||
| 52 | 70 | # NOTE(jlvillal): Signature doesn't match SaveMixin.save() so ignore | |
| 53 | 71 | # type error | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -36,9 +36,9 @@ def test_repository_files(project): | |||
| 36 | 36 | } | |
| 37 | 37 | ) | |
| 38 | 38 | readme = project.files.get(file_path="README.rst", ref="main") | |
| 39 | - # The first decode() is the ProjectFile method, the second one is the bytes | ||
| 40 | - # object method | ||
| 41 | - assert readme.decode().decode() == "Initial content" | ||
| 39 | + | ||
| 40 | + assert readme.decode() == b"Initial content" | ||
| 41 | + assert readme.decode("utf-8") == "Initial content" | ||
| 42 | 42 | ||
| 43 | 43 | headers = project.files.head("README.rst", ref="main") | |
| 44 | 44 | assert headers["X-Gitlab-File-Path"] == "README.rst" | |
| Back | FazBrowse Home | New Git URL |
0 commit comments