Skip to content

Commit

Permalink
🔀 Merge pull request #106 from davep/handle-unknown-collections
Browse files Browse the repository at this point in the history
Handle unknown collections
  • Loading branch information
davep authored Jan 15, 2025
2 parents 6fd6e01 + f892255 commit 233d6e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
8 changes: 8 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Braindrop ChangeLog

## Unreleased

**Released: WiP**

- Made the code that loads a group's collections more defensive, hopefully
to fix a crash one person was reporting.
([#106](https://github.com/davep/braindrop/pull/106))

## v0.6.0

**Released: 2025-01-14**
Expand Down
40 changes: 29 additions & 11 deletions src/braindrop/app/data/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ def collection_size(self, collection: Collection) -> int:
# can work it out).
return collection.count or len(self.in_collection(collection))

class UnknonwCollection(Exception):
"""Exception raised if we encounter a collection ID we don't know about."""

def collection(self, identity: int) -> Collection:
"""Get a collection from its ID.
Expand All @@ -187,12 +190,20 @@ def collection(self, identity: int) -> Collection:
Returns:
The collection with that identity.
Raises:
UnknownCollection: When a collection isn't known.
"""
return (
SpecialCollection(identity)()
if identity in SpecialCollection
else self._collections[identity]
)
try:
return (
SpecialCollection(identity)()
if identity in SpecialCollection
else self._collections[identity]
)
except KeyError:
raise self.UnknonwCollection(
f"Unknown collection identity: {identity}"
) from None

@property
def collections(self) -> list[Collection]:
Expand All @@ -215,16 +226,23 @@ def collections_within(self, group: Group) -> list[Collection]:
Notes:
The returned list is a flat list of *all* the collections within
the group; no specific order is guaranteed.
The Raindrop API has been known to apparently include IDs for
collections, within a group, where the collection no longer
exists. With this in mind any unknown collections are pruned.
"""

def _collections(collection_ids: Iterable[int]) -> Iterator[Collection]:
for collection in collection_ids:
yield self.collection(collection)
yield from _collections(
candidate.identity
for candidate in self.collections
if candidate.parent == collection
)
try:
yield self.collection(collection)
yield from _collections(
candidate.identity
for candidate in self.collections
if candidate.parent == collection
)
except self.UnknonwCollection:
pass

return list(_collections(group.collections))

Expand Down
15 changes: 12 additions & 3 deletions src/braindrop/app/widgets/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,18 @@ def _main_navigation(self) -> None:
Title(f"{group.title} ({len(self.data.collections_within(group))})")
)
for collection in group.collections:
self._add_children_for(
self._add_collection(self.data.collection(collection))
)
try:
self._add_children_for(
self._add_collection(self.data.collection(collection))
)
except self.data.UnknonwCollection:
# It seems that the Raindrop API can sometimes say
# there's a collection ID in a group where the
# collection ID isn't in the actual collections the
# API gives us. So here we just ignore it.
#
# https://github.com/davep/braindrop/issues/104
pass

@staticmethod
def _by_name(tags: list[TagCount]) -> list[TagCount]:
Expand Down

0 comments on commit 233d6e1

Please sign in to comment.