Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

4.8 KB
Raw Blame
"""Read Evernote exports using the project's existing XML and HTML parsers."""

from pathlib import Path
from time import strptime
from typing import Any, Iterator

# nosemgrep: python.lang.security.use-defused-xml.use-defused-xml, reason: TreeBuilder only assembles elements; defusedxml's parser reads the XML
from xml.etree.ElementTree import TreeBuilder

from bs4 import BeautifulSoup
from defusedxml.common import DTDForbidden
from defusedxml.ElementTree import DefusedXMLParser, iterparse
from langchain_core.document_loaders import BaseLoader
from langchain_core.documents import Document


class _EvernoteXMLParser(DefusedXMLParser):
    """defusedxml's parser, refusing any DOCTYPE with an internal subset.

    Evernote exports declare ``<!DOCTYPE en-export SYSTEM "...">``, and
    defusedxml's ``forbid_dtd`` refuses every DOCTYPE, that one included.
    This parser keeps ``forbid_dtd`` on but refuses only a DOCTYPE with an
    internal subset (``[...]``), where a file could declare entities or
    attribute defaults that expat adds to every matching element. Expat
    reports the subset when it reaches the ``[``, so it is refused before
    any of its declarations are read.
    """

    def __init__(self) -> None:
        super().__init__(
            target=TreeBuilder(),
            forbid_dtd=True,
            forbid_entities=True,
            forbid_external=True,
        )

    def defused_start_doctype_decl(
        self, name, sysid, pubid, has_internal_subset
    ):
        if has_internal_subset:
            raise DTDForbidden(name, sysid, pubid)


class EverNoteLoader(BaseLoader):
    """Extract notes from an ENEX file without an undeclared parser dependency."""

    def __init__(
        self, file_path: str | Path, load_single_document: bool = True
    ) -> None:
        self.file_path = str(file_path)
        self.load_single_document = load_single_document

    def lazy_load(self) -> Iterator[Document]:
        notes = self._iter_notes()
        if self.load_single_document:
            yield Document(
                # A blank line between notes, so one note's last word does not run into
                # the next note's first
                page_content="\n\n".join(note.page_content for note in notes),
                metadata={"source": self.file_path},
            )
        else:
            yield from notes

    def _iter_notes(self) -> Iterator[Document]:
        # Standard exports declare Evernote's doctype by its external
        # identifier only, and ElementTree does not fetch that DTD. Any
        # internal subset is refused (see _EvernoteXMLParser), and entity
        # declarations and external references stay forbidden. Clearing the
        # root after each note avoids retaining earlier notes and attachments.
        with open(self.file_path, "rb") as stream:
            context = iterparse(
                stream,
                events=("start", "end"),
                parser=_EvernoteXMLParser(),
            )
            root = None
            for event, element in context:
                if root is None:
                    root = element
                if event != "end" or element.tag != "note":
                    continue

                content = element.findtext("content")
                if content is not None:
                    metadata: dict[str, Any] = {"source": self.file_path}
                    for child in element:
                        if child.tag in {"content", "resource"}:
                            continue
                        if child.tag == "note-attributes":
                            for attribute in child:
                                metadata[f"note-attributes.{attribute.tag}"] = (
                                    attribute.text
                                )
                        elif child.tag == "tag":
                            # A note has one <tag> element per tag
                            if child.text:
                                metadata.setdefault("tag", []).append(
                                    child.text
                                )
                        elif child.tag in {"created", "updated"}:
                            metadata[child.tag] = (
                                strptime(child.text, "%Y%m%dT%H%M%SZ")
                                if child.text
                                else None
                            )
                        else:
                            metadata[child.tag] = child.text

                    html = BeautifulSoup(content, "html.parser")
                    for ignored in html(["script", "style"]):
                        ignored.decompose()
                    yield Document(
                        page_content=html.get_text(" ", strip=True),
                        metadata=metadata,
                    )
                element.clear()
                root.clear()