from datetime import datetime import xml.etree.ElementTree as ET from .entry import Entry from .util import escape_text_for_xml class ReadEntry(Entry): """ Entry type for reading content from standard input. Attributes: _content: Content read from stdin, empty until first update _io_buffer: Buffer to use for IO operations _read: Whether content has been read """ def __init__(self, io_buffer, id: str, timestamp: datetime): """ Initialize a new read entry. Args: io_buffer: Buffer to use for IO operations id: Unique identifier for this entry timestamp: Creation timestamp for this entry """ super().__init__(id, timestamp) self._content = "" self._io_buffer = io_buffer self._read = False @property def content(self) -> str: """ Get the content read from stdin. """ return self._content def update(self) -> None: """ Read from stdin if not already read. Uses the provided IO buffer for the actual read operation. """ if not self._read: self._content = self._io_buffer.read() self._read = True def generate_context(self) -> ET.Element: """ Generate an XML Element representing this read entry. Returns: ET.Element: XML element containing the entry's data """ # Create root element element = ET.Element("read_stdin", {"id": self._id}) # Add content as CDATA or escaped text if content has been read if self._read: element.text = escape_text_for_xml(self._content) return element