From ced49685221bb71d51c2ed532c7a81e410c866a3 Mon Sep 17 00:00:00 2001 From: Fernando Costa Bertoldi Date: Wed, 20 Nov 2024 13:40:00 -0300 Subject: [PATCH] Improve performance of the export bookmark service. Avoid temporary copies in memory of the Bookmark and Tag instances by returning an iterable from services.exporter.export_netscape_html. --- bookmarks/services/exporter.py | 44 +++++++++++++++----------------- bookmarks/tests/test_exporter.py | 8 +++--- bookmarks/views/settings.py | 10 +++++--- package-lock.json | 4 +-- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/bookmarks/services/exporter.py b/bookmarks/services/exporter.py index fe41bf49..f91dad97 100644 --- a/bookmarks/services/exporter.py +++ b/bookmarks/services/exporter.py @@ -1,33 +1,30 @@ import html -from typing import List +import itertools +from typing import Iterable from bookmarks.models import Bookmark -BookmarkDocument = List[str] +def export_netscape_html(bookmarks: Iterable[Bookmark]): + def _append_bookmarks(): + for bookmark in bookmarks: + yield from append_bookmark(bookmark) -def export_netscape_html(bookmarks: List[Bookmark]): - doc = [] - append_header(doc) - append_list_start(doc) - [append_bookmark(doc, bookmark) for bookmark in bookmarks] - append_list_end(doc) + return itertools.chain(append_header(), append_list_start(), _append_bookmarks(), append_list_end()) - return "\n\r".join(doc) +def append_header(): + yield "" + yield '' + yield "Bookmarks" + yield "

Bookmarks

" -def append_header(doc: BookmarkDocument): - doc.append("") - doc.append('') - doc.append("Bookmarks") - doc.append("

Bookmarks

") +def append_list_start(): + yield "

" -def append_list_start(doc: BookmarkDocument): - doc.append("

") - -def append_bookmark(doc: BookmarkDocument, bookmark: Bookmark): +def append_bookmark(bookmark: Bookmark): url = bookmark.url title = html.escape(bookmark.resolved_title or "") desc = html.escape(bookmark.resolved_description or "") @@ -42,13 +39,12 @@ def append_bookmark(doc: BookmarkDocument, bookmark: Bookmark): added = int(bookmark.date_added.timestamp()) modified = int(bookmark.date_modified.timestamp()) - doc.append( - f'

{title}' - ) + yield f'
{title}' + if desc: - doc.append(f"
{desc}") + yield f"
{desc}" -def append_list_end(doc: BookmarkDocument): - doc.append("

") +def append_list_end(): + yield "

" diff --git a/bookmarks/tests/test_exporter.py b/bookmarks/tests/test_exporter.py index 2056bc73..0f9f452a 100644 --- a/bookmarks/tests/test_exporter.py +++ b/bookmarks/tests/test_exporter.py @@ -74,7 +74,7 @@ def test_export_bookmarks(self): is_archived=True, ), ] - html = exporter.export_netscape_html(bookmarks) + html = "\r\n".join(exporter.export_netscape_html(bookmarks)) lines = [ '

Title 1', @@ -89,7 +89,7 @@ def test_export_bookmarks(self): '
Title 7', '
Title 8', ] - self.assertIn("\n\r".join(lines), html) + self.assertIn("\r\n".join(lines), html) def test_escape_html(self): bookmark = self.setup_bookmark( @@ -97,7 +97,7 @@ def test_escape_html(self): description="The