Skip to content

Commit

Permalink
Merge pull request #32 from harmtemolder/sync-to-koreader-enabled-wit…
Browse files Browse the repository at this point in the history
…h-extra-exception-catch

Enabling sync to koreader with exception catch for Permission issue to help with #28
  • Loading branch information
kyxap committed Aug 5, 2024
2 parents 126b28e + 3c86c38 commit aac66dc
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions action.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ def genesis(self):
# Right-click menu (already includes left-click action)

# TODO: Sync calibre to KOReader is disabled see more in #8
# self.create_menu_action(
# self.qaction.menu(),
# 'Sync missing to KOReader',
# 'Sync missing to KOReader',
# icon='edit-undo.png',
# description='If calibre has an entry in the "Raw sidecar column", '
# 'but KOReader does not have a sidecar file, push the '
# 'metadata from calibre to a new sidecar file.',
# triggered=self.sync_missing_sidecars_to_koreader
# )
self.create_menu_action(
self.qaction.menu(),
'Sync missing to KOReader',
'Sync missing to KOReader',
icon='edit-undo.png',
description='If calibre has an entry in the "Raw sidecar column", '
'but KOReader does not have a sidecar file, push the '
'metadata from calibre to a new sidecar file.',
triggered=self.sync_missing_sidecars_to_koreader
)

self.qaction.menu().addSeparator()

Expand Down Expand Up @@ -588,15 +588,17 @@ def push_metadata_to_koreader_sidecar(self, book_uuid, path):
if not book_id:
debug_print(f'could not find {book_uuid} in calibre’s library')
return "failure", {
'result': f"Could not find uuid {book_uuid} in Calibre's library."
'result': f"Could not find uuid {book_uuid} in Calibre's "
f"library."
}

# Get the current metadata for the book from the library
metadata = db.get_metadata(book_id)
sidecar_metadata = metadata.get(CONFIG["column_sidecar"])
if not sidecar_metadata:
return "no_metadata", {
'result': f'No KOReader metadata for book_id {book_id}, no need to push.'
'result': f'No KOReader metadata for book_id {book_id}, no '
f'need to push.'
}
sidecar_dict = json.loads(sidecar_metadata)
sidecar_lua = lua.encode(sidecar_dict)
Expand All @@ -611,6 +613,18 @@ def push_metadata_to_koreader_sidecar(self, book_uuid, path):
except FileExistsError:
# dir exists, so we're fine
pass
except PermissionError as perm_e:
return "failure", {
'result': f'Unable to create directory at: '
f'{path} due to {perm_e}',
'book_id': book_id,
}
except OSError as os_e:
return "failure", {
'result': f'Unexpectable exception is occurred, '
f'please report: {os_e}',
'book_id': book_id,
}

with open(path, "w", encoding="utf-8") as f:
debug_print(f"Writing to {path}")
Expand Down

0 comments on commit aac66dc

Please sign in to comment.