Skip to content

Commit

Permalink
added 'raise_if_exists' parameter to Attachment.download()
Browse files Browse the repository at this point in the history
Signed-off-by: flashdagger <flashdagger@googlemail.com>
  • Loading branch information
flashdagger committed Mar 13, 2024
1 parent 1394a64 commit 91689e5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
12 changes: 11 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ History
0.3.0 (tbd)
-----------
* **breaking changes**

* none yet

* **added features**

* added ``raise_if_exists`` parameter to :meth:`articles.Attachment.download` method

* **fixes**

* none yet

0.2.0 (2024-03-10)
-----------
------------------
* **breaking changes**

* :class:`client.Client` now uses ``http_auth=(username, password)`` as authentication parameter
Expand Down
8 changes: 6 additions & 2 deletions tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ def test_create_article_via_ticket(ticket_pair, rclient):
assert downloaded_file == Path(tmpdir, filename_binary)
assert downloaded_file.read_bytes() == content_binary

downloaded_file = text_attachment.download(tmpdir)
assert downloaded_file == Path(tmpdir, filename_text)
destination_file = Path(tmpdir, f"{filename_text}.bkp")
downloaded_file = text_attachment.download(destination_file)
assert downloaded_file == destination_file
assert downloaded_file.read_text() == content_text

with pytest.raises(FileExistsError):
text_attachment.download(downloaded_file, raise_if_exists=True)
8 changes: 7 additions & 1 deletion zammadoo/articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,23 @@ def _response(self) -> requests.Response:

return response

def download(self, path: "PathType" = ".") -> "Path":
def download(self, path: "PathType" = ".", raise_if_exists=False) -> "Path":
"""
Downloads the attachment file to the filesystem.
:raises: :exc:`FileExistsError`
:param path: optional download location (directory or full file path)
:param raise_if_exists: raises :exc:`FileExistsError` if destination file exists
:return: the path of the downloaded attachment file
"""
filepath = Path(path)
if filepath.is_dir():
filepath = filepath / self.filename

if raise_if_exists and filepath.exists():
raise FileExistsError(f"File already exists: {str(filepath)!r}")

with filepath.open("wb") as fd:
for chunk in self.iter_bytes():
fd.write(chunk)
Expand Down

0 comments on commit 91689e5

Please sign in to comment.