Skip to content

Commit

Permalink
fix(torrent): get_files() should throw KeyError if files not fe…
Browse files Browse the repository at this point in the history
…tched (#446)

Co-authored-by: Trim21 <[email protected]>
  • Loading branch information
dechamps and trim21 authored Dec 13, 2024
1 parent 30c8afc commit 952e4e8
Show file tree
Hide file tree
Showing 3 changed files with 22 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
36 changes: 18 additions & 18 deletions transmission_rpc/torrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,25 @@ 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: list[Priority | None] = (
[Priority(v) for v in self.fields["priorities"]] if "priorities" in self.fields else [None] * len(files)
)
wanted: list[bool | None] = (
[bool(v) for v in self.fields["wanted"]] if "wanted" in self.fields else [None] * len(files)
)
return [
File(
selected=selected,
priority=priority,
size=file["length"],
name=file["name"],
completed=file["bytesCompleted"],
id=id,
)

return result
for id, file, priority, 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 952e4e8

Please sign in to comment.