Files
SIA/sia/read_entry.py
2024-11-11 14:23:08 +01:00

57 lines
1.6 KiB
Python

from datetime import datetime
import xml.etree.ElementTree as ET
from .entry import Entry
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
"""
element = ET.Element("read_stdin", {"id": self._id})
if self._read:
element.text = self._content
return element