def rollback_transaction(self, e: Exception) -> None: error_text = (f"EXCEPTION raised by {self._bundle_segment}, " f"stage {self.class_name()}: {e}\n" f"{traceback.format_exc()}") PDS_LOGGER.open(f"Stage '{self.class_name}' error raised") PDS_LOGGER.error(error_text) PDS_LOGGER.close() self._marker_file.set_marker_info(self.class_name(), "failure", error_text)
def builder(document: Document) -> Node: doc = document stack: List[Union[Node, List[Node]]] = [] class Builder(xml.sax.handler.ContentHandler): def startElement(self, name: str, attrs: Any) -> None: if name == "NODE": param_name = attrs["name"] param = dictionary[param_name] if type(param) in [str]: elmt = doc.createTextNode(param) elif type(param) in [int, float]: elmt = doc.createTextNode(str(param)) else: if not _is_function(param): raise ValueError( f"{param_name} is type " + f"{type(param)}; should be function.") elmt = param(doc) if not isinstance(elmt, xml.dom.Node): raise TypeError("Failed to create NODE based " + f"on info keyed by {param_name} " + "in dictionary.") stack.append(elmt) elif name == "FRAGMENT": param_name = attrs["name"] param = dictionary[param_name] if not _is_function(param): raise ValueError( f"{param_name} is type " + f"{type(param)}; should be function.") elmts = param(doc) if not isinstance(elmts, list): raise TypeError( f"elmts created by {param_name} is not a list." ) elmt_list: List[Node] = elmts for elmt in elmt_list: if not isinstance(elmt, xml.dom.Node): raise TypeError( "Failed to create NODE based " + f"on info keyed by {param_name} " + "in dictionary.") stack.append(elmt_list) else: elmt = doc.createElement(name) for name in attrs.getNames(): elmt.setAttribute(name, attrs[name]) if not isinstance(elmt, xml.dom.Node): raise TypeError(f"{elmt} is not a dom NODE.") stack.append(elmt) def endElement(self, name: str) -> None: if name == "FRAGMENT": elmts = stack.pop() if not isinstance(elmts, list): raise TypeError(f"{elmts} is not a list.") elmt_list: List[xml.dom.Node] = elmts for elmt in elmt_list: elmt.normalize() if stack: cast(xml.dom.Node, stack[-1]).appendChild(elmt) else: stack.append(elmt) else: elmt = cast(xml.dom.Node, stack.pop()) elmt.normalize() if stack: cast(xml.dom.Node, stack[-1]).appendChild(elmt) else: stack.append(elmt) def characters(self, content: str) -> None: node = doc.createTextNode(content) cast(xml.dom.Node, stack[-1]).appendChild(node) try: xml.sax.parseString(template, Builder()) except Exception: PDS_LOGGER.open("Template error") PDS_LOGGER.error(f"malformed template: {template}") PDS_LOGGER.close() raise return stack[-1]