Skip to content

Commit

Permalink
make a bit more defensive when book titles are changing
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Oct 20, 2022
1 parent 6873d27 commit 4512d9d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/kobuddy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,14 +307,15 @@ def _reg(dct, key, book):
dct[key] = books

@staticmethod
def _get(dct, key) -> Optional[Book]:
def _get(dct, key, *, allow_multiple: bool=False) -> Optional[Book]:
bb = dct.get(key, [])
if len(bb) == 0:
return None
elif len(bb) == 1:
if len(bb) == 1:
return bb[0]
else:
raise RuntimeError(f"Multiple items for {key}: {bb}")
if allow_multiple:
return bb[-1]
raise RuntimeError(f"Multiple items for {key}: {bb}")

def all(self) -> List[Book]:
bset = set()
Expand All @@ -329,10 +330,11 @@ def add(self, book: Book):
Books._reg(self.title2books, book.title, book)

def by_content_id(self, cid: ContentId) -> Optional[Book]:
return Books._get(self.cid2books, cid)
# sometimes title might get updated.. so it's possible to have same contentid with multiple titles
return Books._get(self.cid2books, cid, allow_multiple=True)

def by_isbn(self, isbn: str) -> Optional[Book]:
return Books._get(self.isbn2books, isbn)
return Books._get(self.isbn2books, isbn, allow_multiple=True)

def by_title(self, title: str) -> Optional[Book]:
return Books._get(self.title2books, title)
Expand Down

0 comments on commit 4512d9d

Please sign in to comment.