Skip to content

Commit

Permalink
fix(torrent): improve get_files() fields handling
Browse files Browse the repository at this point in the history
This commit fixes the following issues with `Torrent.get_files()`:

If Client.get_torrent() was called with "files" not in `arguments`, then
`get_files()` returns an empty list. This is surprising - it should
raise an error instead.

If Client.get_torrent() was called with arguments=["files"], get_files()
fails because it expects to find `properties` and `wanted`, which were
not requested. For efficiency we should not force users to request
fields they don't need, so leave these unset in this case.
  • Loading branch information
dechamps committed Jun 23, 2024
1 parent 85f55c4 commit 8f7c75a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
3 changes: 2 additions & 1 deletion tests/test_torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def test_attributes():

with pytest.raises(KeyError):
torrent.format_eta()
assert not torrent.get_files()
with pytest.raises(KeyError):
torrent.get_files()

data = {
"id": 1,
Expand Down
32 changes: 14 additions & 18 deletions transmission_rpc/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,21 @@ def get_files(self) -> list[File]:
print(file.id)
"""
result: list[File] = []
if "files" in self.fields:
files = self.fields["files"]
indices = range(len(files))
priorities = self.fields["priorities"]
wanted = self.fields["wanted"]
result.extend(
File(
selected=bool(raw_selected),
priority=Priority(raw_priority),
size=file["length"],
name=file["name"],
completed=file["bytesCompleted"],
id=id,
)
for id, file, raw_priority, raw_selected in zip(indices, files, priorities, wanted)
files = self.fields["files"]
indices = range(len(files))
priorities = self.fields.get("priorities", [None] * len(files))
wanted = self.fields.get("wanted", [None] * len(files))
return [
File(
selected=bool(raw_selected) if raw_selected is not None else None,
priority=Priority(raw_priority) if raw_priority is not None else None,
size=file["length"],
name=file["name"],
completed=file["bytesCompleted"],
id=id,
)

return result
for id, file, raw_priority, raw_selected in zip(indices, files, priorities, wanted)
]

@property
def file_stats(self) -> list[FileStat]:
Expand Down
4 changes: 2 additions & 2 deletions transmission_rpc/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ class File(NamedTuple):
completed: int
"""bytes completed"""

priority: Priority
priority: Priority | None
"""download priority"""

selected: bool
selected: bool | None
"""if selected for download"""

id: int
Expand Down

0 comments on commit 8f7c75a

Please sign in to comment.