Skip to content

Commit

Permalink
Use decorator for exception handling in add_node
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit74 committed Feb 29, 2024
1 parent a29fb68 commit 1111b36
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/kolibri2zim/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ def read_from_zip(ark, member):
return ark.open(member).read()


def wrap_failure_details(func):
def wrapper(self, item):
try:
node_id, kind = item
return func(self, item)
except Exception as ex:
raise RuntimeError(f"Failed to process {kind} node {node_id}") from ex

return wrapper


class Kolibri2Zim:
def __init__(self, **kwargs):
for option in options:
Expand Down Expand Up @@ -191,26 +202,24 @@ def schedule_node(item):
if self.node_ids is None or node["id"] in self.node_ids:
schedule_node((node["id"], node["kind"]))

@wrap_failure_details
def add_node(self, item):
"""process a content node from the tuple in queue"""
try:
node_id, kind = item
# check if we have a handler for this {kind} of node
handler = getattr(self, f"add_{kind}_node", None)
node_id, kind = item
# check if we have a handler for this {kind} of node
handler = getattr(self, f"add_{kind}_node", None)

# debug espace
if self.only_topics and kind != "topic":
return
# debug espace
if self.only_topics and kind != "topic":
return

if handler:
# add thumbnail to zim if there's one for this node
thumbnail = self.db.get_node_thumbnail(node_id)
if thumbnail:
self.funnel_file(thumbnail["id"], thumbnail["ext"])
# fire the add_{kind}_node() method which will actually process it
handler(node_id)
except Exception as ex:
raise RuntimeError(f"Failed to process {kind} node {node_id}") from ex
if handler:
# add thumbnail to zim if there's one for this node
thumbnail = self.db.get_node_thumbnail(node_id)
if thumbnail:
self.funnel_file(thumbnail["id"], thumbnail["ext"])
# fire the add_{kind}_node() method which will actually process it
handler(node_id)

def funnel_file(self, fid, fext):
"""directly add a Kolibri file to the ZIM using same name"""
Expand Down

0 comments on commit 1111b36

Please sign in to comment.