mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 10:44:09 -04:00
Add basic tests for export/import of notes
This commit is contained in:
parent
721d13a809
commit
2076fa282d
@ -487,6 +487,6 @@ class Notes:
|
|||||||
st = os.stat(html_file_path)
|
st = os.stat(html_file_path)
|
||||||
doc, searchable_text, resources = import_note(html_file_path, add_resource)
|
doc, searchable_text, resources = import_note(html_file_path, add_resource)
|
||||||
return self.set_note(
|
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
|
ctime=st.st_ctime, mtime=st.st_mtime
|
||||||
)
|
)
|
||||||
|
@ -28,6 +28,7 @@ def export_note(note_data: dict, resources: dict[str, tuple[str, str]], dest_dir
|
|||||||
shutil.copy2(path, d)
|
shutil.copy2(path, d)
|
||||||
root = parse_html(note_data['doc'])
|
root = parse_html(note_data['doc'])
|
||||||
for img in root.xpath('//img[@src]'):
|
for img in root.xpath('//img[@src]'):
|
||||||
|
img.attrib.pop('pre-import-src', None)
|
||||||
try:
|
try:
|
||||||
purl = urlparse(img.get('src'))
|
purl = urlparse(img.get('src'))
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -58,7 +59,7 @@ def import_note(path_to_html_file: str, add_resource) -> dict:
|
|||||||
src = img.attrib.pop('src')
|
src = img.attrib.pop('src')
|
||||||
img.set('pre-import-src', src)
|
img.set('pre-import-src', src)
|
||||||
try:
|
try:
|
||||||
purl = urlparse(img.get('src'))
|
purl = urlparse(src)
|
||||||
except Exception:
|
except Exception:
|
||||||
continue
|
continue
|
||||||
if purl.scheme in ('', 'file'):
|
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):
|
if not os.path.isabs(path):
|
||||||
path = os.path.join(basedir, path)
|
path = os.path.join(basedir, path)
|
||||||
q = os.path.normcase(get_long_path_name(os.path.abspath(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))
|
rhash = add_resource(make_long_path_useable(path), os.path.basename(path))
|
||||||
scheme, digest = rhash.split(':', 1)
|
scheme, digest = rhash.split(':', 1)
|
||||||
img.set('src', f'{RESOURCE_URL_SCHEME}://{scheme}/{digest}')
|
img.set('src', f'{RESOURCE_URL_SCHEME}://{scheme}/{digest}')
|
||||||
|
@ -2,9 +2,13 @@
|
|||||||
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPLv3 Copyright: 2023, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
|
|
||||||
|
|
||||||
import os, time
|
import os
|
||||||
|
import shutil
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
|
||||||
from calibre.db.tests.base import BaseTest
|
from calibre.db.tests.base import BaseTest
|
||||||
|
from calibre.utils.resources import get_image_path
|
||||||
|
|
||||||
|
|
||||||
def test_notes_api(self: 'NotesTest'):
|
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.ae(cache.notes_for('#tags', tag_id), '')
|
||||||
self.assertIsNone(cache.get_notes_resource(h1))
|
self.assertIsNone(cache.get_notes_resource(h1))
|
||||||
self.assertIsNone(cache.get_notes_resource(h2))
|
self.assertIsNone(cache.get_notes_resource(h2))
|
||||||
|
# test exim of note
|
||||||
|
doc = '<p>test simple exim <img src="r1.png"> of note with resources <img src="r2.png">. Works or not</p>'
|
||||||
|
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('<p>test simple exim <img', cache.notes_for('authors', author_id))
|
||||||
|
edir = os.path.join(tdir, 'e')
|
||||||
|
os.mkdir(edir)
|
||||||
|
index_name = cache.export_note('authors', author_id, edir)
|
||||||
|
with open(os.path.join(edir, index_name)) as f:
|
||||||
|
self.assertIn(doc, f.read())
|
||||||
|
for x in ('r1.png', 'r2.png'):
|
||||||
|
with open(os.path.join(idir, x), 'rb') as a, open(os.path.join(edir, x), 'rb') as b:
|
||||||
|
self.assertEqual(a.read(), b.read())
|
||||||
|
|
||||||
|
|
||||||
def test_fts(self: 'NotesTest'):
|
def test_fts(self: 'NotesTest'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user