From 05469560063be5635cf068ae2d747845977b6a63 Mon Sep 17 00:00:00 2001 From: vzhd1701 Date: Wed, 25 May 2022 14:05:40 +0500 Subject: [PATCH] fix: show processed notes count during upload fix #33 --- enex2notion/cli.py | 9 +++++++-- enex2notion/enex_parser.py | 17 +++++++++++++++++ enex2notion/enex_uploader.py | 7 ++----- tests/test_cli.py | 2 ++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/enex2notion/cli.py b/enex2notion/cli.py index d857956..d969ba5 100644 --- a/enex2notion/cli.py +++ b/enex2notion/cli.py @@ -6,7 +6,7 @@ from typing import Optional from enex2notion.cli_wkhtmltopdf import ensure_wkhtmltopdf -from enex2notion.enex_parser import iter_notes +from enex2notion.enex_parser import count_notes, iter_notes from enex2notion.enex_uploader import ( BadTokenException, NoteUploadFailException, @@ -83,7 +83,9 @@ def upload(self, enex_file: Path): notebook_root = self._get_notebook_root(enex_file.stem) - for note in iter_notes(enex_file): + notes_total = count_notes(enex_file) + + for note_idx, note in enumerate(iter_notes(enex_file), 1): if note.note_hash in self.done_hashes: logger.debug(f"Skipping note '{note.title}' (already uploaded)") continue @@ -96,6 +98,9 @@ def upload(self, enex_file: Path): continue if notebook_root is not None: + logger.info( + f"Uploading note {note_idx} out of {notes_total} '{note.title}'" + ) _upload_note(notebook_root, note, note_blocks) self.done_hashes.add(note.note_hash) diff --git a/enex2notion/enex_parser.py b/enex2notion/enex_parser.py index bba46c2..4b564e8 100644 --- a/enex2notion/enex_parser.py +++ b/enex2notion/enex_parser.py @@ -16,6 +16,23 @@ logger = logging.getLogger(__name__) +def count_notes(enex_file: Path) -> int: + total_notes = 0 + + with open(enex_file, "rb") as f: + context = ElementTree.iterparse(f, events=("start", "end")) + + _, root = next(context) + + for event, elem in context: + if event == "end" and elem.tag == "note": + total_notes += 1 + + root.clear() + + return total_notes + + def iter_notes(enex_file: Path): with open(enex_file, "rb") as f: context = ElementTree.iterparse(f, events=("start", "end")) diff --git a/enex2notion/enex_uploader.py b/enex2notion/enex_uploader.py index cb95854..d7c9c97 100644 --- a/enex2notion/enex_uploader.py +++ b/enex2notion/enex_uploader.py @@ -47,14 +47,11 @@ def get_import_root(client, title): def upload_note(root, note: EvernoteNote, note_blocks): - logger.info(f"Creating new page for note '{note.title}'") + logger.debug(f"Creating new page for note '{note.title}'") new_page = _make_page(note, root) - # Escape % to prevent progress bar crashing - note_title = note.title.replace("%", "%%") - try: - for block in Bar(f"Uploading '{note_title}'").iter(note_blocks): + for block in Bar().iter(note_blocks): upload_block(new_page, block) except HTTPError: if isinstance(new_page, CollectionRowBlock): diff --git a/tests/test_cli.py b/tests/test_cli.py index 3cca024..2270356 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -21,8 +21,10 @@ def mock_api(mocker): @pytest.fixture() def fake_note_factory(mocker): + mock_count = mocker.patch("enex2notion.cli.count_notes") mock_iter = mocker.patch("enex2notion.cli.iter_notes") mock_iter.return_value = [mocker.MagicMock(note_hash="fake_hash", is_webclip=False)] + mock_count.side_effect = lambda x: len(mock_iter.return_value) return mock_iter