Skip to content

Commit

Permalink
fix: fix crash for notes with empty resource
Browse files Browse the repository at this point in the history
fix #48
  • Loading branch information
vzhd1701 committed Aug 20, 2022
1 parent a5364d6 commit 844e95c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion enex2notion/enex_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _convert_resource(resource_raw):
ext = mimetypes.guess_extension(resource_raw["mime"]) or ".bin"
file_name = f"{file_name}{ext}"

if resource_raw["data"].get("#text"):
if resource_raw.get("data", {}).get("#text"):
data_bin = base64.b64decode(resource_raw["data"]["#text"])
else:
logger.debug("Empty resource")
Expand Down
53 changes: 53 additions & 0 deletions tests/test_enex_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,59 @@ def test_iter_notes_single_with_empty_resource(fs, caplog):
)


def test_iter_notes_single_with_empty_resource_no_data(fs, caplog):
test_enex = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
<en-export export-date="20211218T085932Z" application="Evernote" version="10.25.6">
<note>
<title>test1</title>
<created>20211118T085332Z</created>
<updated>20211118T085920Z</updated>
<note-attributes>
</note-attributes>
<content>test</content>
<resource>
<mime>image/gif</mime>
<resource-attributes>
<file-name>smallest.gif</file-name>
</resource-attributes>
</resource>
</note>
</en-export>
"""
fs.create_file("test.enex", contents=test_enex)

with caplog.at_level(logging.DEBUG, logger="enex2notion"):
notes = list(iter_notes(Path("test.enex")))

expected_resource = EvernoteResource(
data_bin=b"",
size=0,
md5="d41d8cd98f00b204e9800998ecf8427e",
mime="image/gif",
file_name="smallest.gif",
)

assert "Empty resource" in caplog.text
assert notes == [
EvernoteNote(
title="test1",
created=datetime.datetime(2021, 11, 18, 8, 53, 32, tzinfo=tzutc()),
updated=datetime.datetime(2021, 11, 18, 8, 59, 20, tzinfo=tzutc()),
content="test",
tags=[],
author="",
url="",
is_webclip=False,
resources=[expected_resource],
),
]
assert (
notes[0].resource_by_md5("d41d8cd98f00b204e9800998ecf8427e")
== expected_resource
)


def test_iter_notes_single_empty(fs, mocker):
test_enex = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export4.dtd">
Expand Down

0 comments on commit 844e95c

Please sign in to comment.