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 SingleEntry(Entry):
"""
@@ -61,33 +60,15 @@ class SingleEntry(Entry):
"""
if self._executed:
return
try:
# Run script and capture output
process = subprocess.run(
self.script,
shell=True,
capture_output=True,
text=True
)
# Store results
self._stdout = process.stdout
self._stderr = process.stderr
self._exit_code = process.returncode
except subprocess.SubprocessError as e:
# Handle subprocess errors by capturing the error message
self._stdout = ""
self._stderr = str(e)
self._exit_code = -1
except Exception as e:
# Handle any other errors
self._stdout = ""
self._stderr = f"Error executing script: {str(e)}"
self._exit_code = -1
process = subprocess.run(
self.script,
shell=True,
capture_output=True,
text=True
)
self._stdout = process.stdout
self._stderr = process.stderr
self._exit_code = process.returncode
self._executed = True
def generate_context(self) -> ET.Element:
@@ -97,25 +78,14 @@ class SingleEntry(Entry):
Returns:
ET.Element: XML element containing the entry's data
"""
# Create root element
element = ET.Element("single", {
"id": self.id,
})
# Add script as CDATA or escaped text
element.text = escape_text_for_xml(self.script)
# Only add output elements if script has executed
element.text = self.script
if self._executed:
# Add stdout element if there is output
stdout_elem = ET.SubElement(element, "stdout")
stdout_elem.text = escape_text_for_xml(self._stdout)
# Add stderr element if there are errors
stdout_elem.text = self._stdout
stderr_elem = ET.SubElement(element, "stderr")
stderr_elem.text = escape_text_for_xml(self._stderr)
# Add exit code attribute after execution
stderr_elem.text = self._stderr
element.set("exit_code", str(self._exit_code))
return element