Fix superfluous CDATA tokens

This commit is contained in:
2024-11-11 14:23:08 +01:00
parent 3a1d8a449c
commit 59f482bc0e
17 changed files with 102 additions and 324 deletions

View File

@@ -4,7 +4,6 @@ import xml.etree.ElementTree as ET
from typing import Optional
from .entry import Entry
from .util import escape_text_for_xml
class BackgroundEntry(Entry):
"""
@@ -66,13 +65,10 @@ class BackgroundEntry(Entry):
"""
if self._process is not None:
try:
# Close file handles if they're open
if self._process.stdout:
self._process.stdout.close()
if self._process.stderr:
self._process.stderr.close()
# Terminate process if it's still running
if self._process.poll() is None:
self._process.terminate()
try:
@@ -90,65 +86,48 @@ class BackgroundEntry(Entry):
Start the process if not running and collect any new output.
Updates stdout and stderr with any new output.
"""
try:
# Start process if not running
if self._process is None and self._exit_code is None:
self._process = subprocess.Popen(
self._script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1 # Line buffered
)
return # Return after starting to allow process to generate output
if self._process is None:
return # Process already completed
# Check if process has finished
exit_code = self._process.poll()
if exit_code is not None:
# Process has terminated, collect remaining output
try:
remaining_out, remaining_err = self._process.communicate(timeout=0.1)
self._stdout += remaining_out
self._stderr += remaining_err
except subprocess.TimeoutExpired:
pass # Process didn't finish communicating, try again next update
self._exit_code = exit_code
self.cleanup()
return
if self._process is None and self._exit_code is None:
self._process = subprocess.Popen(
self._script,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1 # Line buffered
)
return
if self._process is None:
return
exit_code = self._process.poll()
if exit_code is not None:
try:
remaining_out, remaining_err = self._process.communicate(timeout=0.1)
self._stdout += remaining_out
self._stderr += remaining_err
except subprocess.TimeoutExpired:
pass # Process didn't finish communicating, try again next update
# Read stdout if available
if self._process.stdout:
while True:
try:
line = self._process.stdout.readline()
if not line:
break
self._stdout += line
except:
break
# Read stderr if available
if self._process.stderr:
while True:
try:
line = self._process.stderr.readline()
if not line:
break
self._stderr += line
except:
break
except Exception as e:
# Handle any errors
error_msg = f"Error handling background process: {str(e)}"
self._stderr += f"\n{error_msg}"
self._exit_code = -1
self._exit_code = exit_code
self.cleanup()
return
if self._process.stdout:
while True:
try:
line = self._process.stdout.readline()
if not line:
break
self._stdout += line
except:
break
if self._process.stderr:
while True:
try:
line = self._process.stderr.readline()
if not line:
break
self._stderr += line
except:
break
def generate_context(self) -> ET.Element:
"""
@@ -157,23 +136,15 @@ class BackgroundEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
# Create root element with appropriate status
element = ET.Element("background", {"id": self._id})
if self._process is not None:
element.set("pid", str(self._process.pid))
elif self._exit_code is not None:
element.set("exit_code", str(self._exit_code))
# Add script as CDATA or escaped text
element.text = escape_text_for_xml(self._script)
# Add stdout element with accumulated output
element.text = self._script
stdout_elem = ET.SubElement(element, "stdout")
stdout_elem.text = escape_text_for_xml(self._stdout)
# Add stderr element with accumulated output
stdout_elem.text = self._stdout
stderr_elem = ET.SubElement(element, "stderr")
stderr_elem.text = escape_text_for_xml(self._stderr)
stderr_elem.text = self._stderr
return element