From 2076fa282d9bd3b01e022c19dff9110e8fd546f9 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 16 Sep 2023 16:18:04 +0530 Subject: [PATCH] Add basic tests for export/import of notes --- src/calibre/db/notes/connect.py | 2 +- src/calibre/db/notes/exim.py | 5 +++-- src/calibre/db/tests/notes.py | 26 +++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/calibre/db/notes/connect.py b/src/calibre/db/notes/connect.py index 3f3820b87f..5211f7e97e 100644 --- a/src/calibre/db/notes/connect.py +++ b/src/calibre/db/notes/connect.py @@ -487,6 +487,6 @@ class Notes: st = os.stat(html_file_path) doc, searchable_text, resources = import_note(html_file_path, add_resource) return self.set_note( - conn, field_name, item_id, item_value, used_resource_hashes=resources, searchable_text=searchable_text, + conn, field_name, item_id, item_value, marked_up_text=doc, used_resource_hashes=resources, searchable_text=searchable_text, ctime=st.st_ctime, mtime=st.st_mtime ) diff --git a/src/calibre/db/notes/exim.py b/src/calibre/db/notes/exim.py index f912eb548f..a180b21462 100644 --- a/src/calibre/db/notes/exim.py +++ b/src/calibre/db/notes/exim.py @@ -28,6 +28,7 @@ def export_note(note_data: dict, resources: dict[str, tuple[str, str]], dest_dir shutil.copy2(path, d) root = parse_html(note_data['doc']) for img in root.xpath('//img[@src]'): + img.attrib.pop('pre-import-src', None) try: purl = urlparse(img.get('src')) except Exception: @@ -58,7 +59,7 @@ def import_note(path_to_html_file: str, add_resource) -> dict: src = img.attrib.pop('src') img.set('pre-import-src', src) try: - purl = urlparse(img.get('src')) + purl = urlparse(src) except Exception: continue if purl.scheme in ('', 'file'): @@ -66,7 +67,7 @@ def import_note(path_to_html_file: str, add_resource) -> dict: if not os.path.isabs(path): path = os.path.join(basedir, path) q = os.path.normcase(get_long_path_name(os.path.abspath(path))) - if q.startswith(basedir): + if q.startswith(basedir) and os.path.exists(make_long_path_useable(path)): rhash = add_resource(make_long_path_useable(path), os.path.basename(path)) scheme, digest = rhash.split(':', 1) img.set('src', f'{RESOURCE_URL_SCHEME}://{scheme}/{digest}') diff --git a/src/calibre/db/tests/notes.py b/src/calibre/db/tests/notes.py index 51593e153a..ee4df65f44 100644 --- a/src/calibre/db/tests/notes.py +++ b/src/calibre/db/tests/notes.py @@ -2,9 +2,13 @@ # License: GPLv3 Copyright: 2023, Kovid Goyal -import os, time +import os +import shutil +import tempfile +import time from calibre.db.tests.base import BaseTest +from calibre.utils.resources import get_image_path def test_notes_api(self: 'NotesTest'): @@ -119,6 +123,26 @@ def test_cache_api(self: 'NotesTest'): self.ae(cache.notes_for('#tags', tag_id), '') self.assertIsNone(cache.get_notes_resource(h1)) self.assertIsNone(cache.get_notes_resource(h2)) + # test exim of note + doc = '

test simple exim of note with resources . Works or not

' + with tempfile.TemporaryDirectory() as tdir: + idir = os.path.join(tdir, 'i') + os.mkdir(idir) + with open(os.path.join(idir, 'index.html'), 'w') as f: + f.write(doc) + shutil.copyfile(get_image_path('lt.png'), os.path.join(idir, 'r1.png')) + shutil.copyfile(get_image_path('library.png'), os.path.join(idir, 'r2.png')) + note_id = cache.import_note('authors', author_id, f.name) + self.assertGreater(note_id, 0) + self.assertIn('

test simple exim