class XmppXmlHandler(object): STREAM_TAG='{http://etherx.jabber.org/streams}stream' def __init__(self): self._stack = 0 self._builder = TreeBuilder() self._results = deque() def data(self, data): self._builder.data(data) def start(self, tag, attrib): if tag == self.STREAM_TAG: return self._builder.start(tag, attrib) self._stack += 1 def end(self, tag): self._stack -= 1 elem = self._builder.end(tag) if self._stack == 0: self._results.append(elem) def get_elem(self): """If a top-level XML element has been completed since the last call to get_elem, return it; else return None.""" try: return self._results.popleft() except IndexError: return None
def from_dataframe(cls, df, **kwargs): if isinstance(df, pandas.DataFrame): return xml.etree.ElementTree.fromstring( df.to_html(**kwargs), parser=XMLParser(target=TreeBuilder(element_factory=cls))) elif isinstance(df, pandas.io.formats.style.Styler): render = df.render() render = re.sub(r"colspan=([1234567890]*)>", "colspan=\"\g<1>\">", render, 0) try: return xml.etree.ElementTree.fromstring( f"<div>{render}</div>", parser=XMLParser(target=TreeBuilder(element_factory=cls))) except Exception as parse_err: if type(parse_err).__name__ == 'ParseError': x = Elem('div') x << xml.etree.ElementTree.fromstring( df.data.to_html(**kwargs), parser=XMLParser(target=TreeBuilder( element_factory=cls))) x << Elem('pre', text=render) x << Elem('pre', text=str(parse_err)) return x else: raise
def test_add_column_readonly(self): from xml.etree.ElementTree import TreeBuilder, tostring from sqlalchemy.orm.util import class_mapper from c2cgeoportal_geoportal.lib.xsd import XSDGenerator gen = XSDGenerator(include_foreign_keys=True) mapper = class_mapper(self.cls) tb = TreeBuilder() p = mapper.attrs["readonly"] gen.add_column_property_xsd(tb, p) e = tb.close() self.assertEqual( '<xsd:element name="readonly" minOccurs="0" nillable="true" type="xsd:string">' "<xsd:annotation>" "<xsd:appinfo>" '<readonly value="true" />' "</xsd:appinfo>" "</xsd:annotation>" "</xsd:element>", tostring(e).decode("utf-8"), )
def _render_holes(svg: TreeBuilder, layer: Layer) -> None: # Sort the holes left to right then up and down to avoid any pathological # movement of the cutting head. holes = sorted(layer.holes(), key=lambda hole: tuple(hole.position)) for hole in holes: if layer.connected(hole.position): continue transformation = Transformation(offset=hole.position, scale=layer.grid, rotation=R0) x, y = transformation.transform_point((0, 0)) r = transformation.transform_distance(hole.radius) path = PathBuilder() path.move_to(x, y + r) path.arc_to(x + r, y, rx=r, ry=r) path.arc_to(x, y - r, rx=r, ry=r) path.arc_to(x - r, y, rx=r, ry=r) path.arc_to(x, y + r, rx=r, ry=r) path.close_path() # TODO path = path.close() svg.start( "path", { "d": str(path), "stroke": "red", "stroke-width": "0.25", "fill": "none", }) svg.end("path")
def build(self, root=None): if root is None: was_root = True root = TreeBuilder() else: was_root = False root.start(self.tagname(), self.attrs()) for i, child in enumerate(self.children): if isinstance(child, HTMLBuilder): child.build(root=root) else: if i in self._formatted: try: proxy = TreeProxy(root) parser = XMLParser(html=True, target=proxy) parser.feed(child) proxy.cleanup() except Exception as e: print("Bad formatting", e) root.data(str(child)) else: root.data(str(child)) root.end(self.tagname()) if was_root: root = root.close() return str(tostring(root, method="html").decode('utf-8'))
def test_xsd_sequence_callback(self): from xml.etree.ElementTree import TreeBuilder, tostring from c2cgeoportal_geoportal.lib.dbreflection import xsd_sequence_callback from papyrus.xsd import tag tb = TreeBuilder() with tag(tb, "xsd:sequence") as tb: xsd_sequence_callback(tb, self.cls) e = tb.close() self.assertIsNotNone( re.search( '<xsd:element name="child2">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>', tostring(e).decode("utf-8"), )) self.assertIsNotNone( re.search( '<xsd:element minOccurs="0" name="child1" nillable="true">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>', tostring(e).decode("utf-8"), ))
def test_xsd_sequence_callback(self): from xml.etree.ElementTree import TreeBuilder, tostring from c2cgeoportal.lib.dbreflection import _xsd_sequence_callback from papyrus.xsd import tag tb = TreeBuilder() with tag(tb, 'xsd:sequence') as tb: _xsd_sequence_callback(tb, self.cls) e = tb.close() self.assertEqual( tostring(e), '<xsd:sequence>' '<xsd:element minOccurs="0" name="child1" nillable="true">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>' '<xsd:element minOccurs="0" name="child2" nillable="true">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>' '</xsd:sequence>')
def test_xsd_sequence_callback(self): from xml.etree.ElementTree import TreeBuilder, tostring from c2cgeoportal.lib.dbreflection import _xsd_sequence_callback from papyrus.xsd import tag tb = TreeBuilder() with tag(tb, "xsd:sequence") as tb: _xsd_sequence_callback(tb, self.cls) e = tb.close() self.assertEqual( tostring(e), '<xsd:sequence>' '<xsd:element minOccurs="0" name="child1" nillable="true">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>' '<xsd:element minOccurs="0" name="child2" nillable="true">' '<xsd:simpleType>' '<xsd:restriction base="xsd:string">' '<xsd:enumeration value="foo" />' '<xsd:enumeration value="bar" />' '</xsd:restriction>' '</xsd:simpleType>' '</xsd:element>' '</xsd:sequence>')
def coverageview_store_info(builder: TreeBuilder, data: dict, name='store'): # if 'classname' in data.items(): if data.get('classname'): builder.start(name, {'class': data.get('classname')}) builder.start('name') builder.data(data.get('name', None)) builder.end('name') builder.end(name) pass
def write(builder: TreeBuilder, metadata: dict) -> None: builder.start(name, dict()) # 遍历传入的字典 for k, v in metadata.items(): builder.start("entry", dict(key=k)) if k == "COVERAGE_VIEW": # 需要创建 coverageView coverageview_xml(builder, v) pass
def _element_from_sexpr(sexpr, builder: ET.TreeBuilder) -> Optional[ET.Element]: if isinstance(sexpr, list): _element_from_list(sexpr, builder) elif isinstance(sexpr, ET.Element): return sexpr elif isinstance(sexpr, str): builder.data(sexpr) else: builder.data(str(sexpr))
def __init__(self, filename: pathlib.Path, stylesheet_path: pathlib.Path) -> None: super().__init__(filename) self.stylesheet_path = stylesheet_path self.builder = TreeBuilder() self.heading_class: List[str] = [] # Heading class stack self.heading_level: List[int] = [] # Heading level stack self.table_cell_html_tag: Optional[str] = None self.in_keep_together_div = False self.in_measure = False self.frontpage_done = False
def _element_from_list(sexpr, builder: ET.TreeBuilder): assert len(sexpr) tag, *rest = sexpr attributes = {} if len(rest) and isinstance(rest[0], dict): attributes, *rest = rest element = builder.start(tag, attributes) for child in rest: child_element = _element_from_sexpr(child, builder) if child_element is not None: element.append(child_element) builder.end(tag)
def _get_new_album_body(self, title): ''' Формирует XML-элемент для нового альбома. @param title: string @return Element ''' builder = TreeBuilder(Element) builder.start('entry', {'xmlns': ATOM_NS}) builder.start('title', {}) builder.data(title.decode('utf8')) builder.end('title') builder.end('entry') node = builder.close() return tostring(node)
def __init__(self, html=0, target=None, encoding=None): try: from xml.parser import expat except ImportError: try: import pyexpat as expat except ImportError: raise ImportError("No module named expat") self.parser = expat.ParserCreate(encoding, "}") if target is None: target = TreeBuilder() self.target = target self._error = expat.error self._names = {} self.entity = {} parser = self.parser parser.DefaultHandlerExpand = self._default for k, v in { "start": (self._start, "StartElementHandler"), "end": (self._end, "EndElementHandler"), "data": (None, "CharacterDataHandler"), "comment": (None, "CommentHandler"), "pi": (None, "ProcessingInstructionHandler") }.items(): if hasattr(target, k): setattr(parser, v[1], v[0] if v[0] is not None else getattr(target, k)) parser.buffer_text = 1 parser.ordered_attributes = 1 parser.specified_attributes = 1 self.positions = {}
def save_xml(data, filename): tb = TreeBuilder() studyguide = tb.start("studyguide", {}) for dataQuestion in data: xmlQuestion = SubElement(studyguide, "question", {}) xmlQuestion.text = dataQuestion[0] for dataAnswer in dataQuestion[1]: if dataAnswer == dataQuestion[1][0]: xmlAnswer = SubElement(xmlQuestion, "answer", {'correct': "true"}) else : xmlAnswer = SubElement(xmlQuestion, "answer", {'correct':"false"}) xmlAnswer.text = dataAnswer #dump(studyguide) ElementTree(studyguide).write("xml/" + filename)
def __init__(self, *, target=None, encoding="UTF-8"): if target is None: target = TreeBuilder() self._data = bytearray() self.parser = self._parser = _CryXMLBParser(target, encoding) self.target = self._target = target self.target = target self.encoding = encoding self._error = ValueError self._names = {} # name memo cache # main callbacks if hasattr(target, "start"): self.parser.StartElementHandler = self._start if hasattr(target, "end"): self.parser.EndElementHandler = self._end if hasattr(target, "start_ns"): self.parser.StartNamespaceDeclHandler = self._start_ns if hasattr(target, "end_ns"): self.parser.EndNamespaceDeclHandler = self._end_ns if hasattr(target, "data"): self.parser.CharacterDataHandler = target.data # miscellaneous callbacks if hasattr(target, "comment"): self.parser.CommentHandler = target.comment if hasattr(target, "pi"): self.parser.ProcessingInstructionHandler = target.pi self.entity = {}
def from_string(cls, xml_as_string): if isinstance(xml_as_string, bytes): xml_as_string = xml_as_string.decode() try: return xml.etree.ElementTree.fromstring(xml_as_string, parser=XMLParser(target=TreeBuilder(element_factory=cls))) except xml.etree.ElementTree.ParseError: return cls.from_string(xml_as_string.replace("<style scoped>","<style scoped='1'>"))
def from_string(cls, xml_as_string, _fix_step=0): if isinstance(xml_as_string, bytes): xml_as_string = xml_as_string.decode() parser = XMLParser(target=TreeBuilder(element_factory=cls)) try: return xml.etree.ElementTree.fromstring(xml_as_string, parser=parser) except Exception as err: # xml.etree.ElementTree.ParseError # see: https://stackoverflow.com/questions/47917787/xml-etree-elementtree-parseerror-exception-handling-not-catching-errors if type(err).__name__ == 'ParseError': if _fix_step == 0: return cls.from_string(xml_as_string.replace( "<style scoped>", "<style scoped='1'>"), _fix_step=1) elif _fix_step == 1: return cls.from_string("<div>" + xml_as_string.replace( "<style scoped>", "<style scoped='1'>") + "</div>", _fix_step=2) elif _fix_step == 2: return cls.from_string(xml_as_string.replace( " ", " "), _fix_step=3) else: import logging lined = "\n".join(f"{n: 4d} | {line}" for n, line in enumerate( xml_as_string.split("\n"), start=1)) logging.getLogger("").error(f"ParseError in:\n{lined}") raise else: raise
def _getModuleStepsDocAndTables(self, moduleName, pythonLibDirectories): for pythonLibDirectory in pythonLibDirectories: stepsDocFileName = pythonLibDirectory + os.sep + moduleName + "-steps-doc.xml" stepsDocDict = self.allImportedModulesStepsDoc.get(stepsDocFileName) stepsTablesDict = self.allImportedModulesStepsTables.get(stepsDocFileName) if stepsDocDict and stepsTablesDict: return (stepsDocDict, stepsTablesDict) else: if os.path.exists(stepsDocFileName): stepsModuleDocTree = et.parse(stepsDocFileName, TreeBuilder()) stepsDocDict = {} stepsDocTree = stepsModuleDocTree.find('steps') if stepsDocTree is not None: for stepElem in stepsDocTree.getiterator('step'): stepName = stepElem.get('name') stepsDocDict[stepName] = stepElem stepsTablesDict = {} stepsTablesTree = stepsModuleDocTree.find('stepsTables') if stepsTablesTree is not None: for stepsTableElem in stepsTablesTree.getiterator('stepsTable'): stepsTableName = stepsTableElem.get('name') stepsTable = [] for stepElem in stepsTableElem.getiterator('step'): stepId = stepElem.get('id') stepName = stepElem.get('name') stepsTable.append((stepId, stepName)) stepsTablesDict[stepsTableName] = stepsTable # store doc dicts to avoid parsing them again self.allImportedModulesStepsDoc[stepsDocFileName] = stepsDocDict self.allImportedModulesStepsTables[stepsDocFileName] = stepsTablesDict return (stepsDocDict, stepsTablesDict) return (None, None)
def __init__(self, filePath, absUrl, xmlIf, treeWrapper): XMLTreeBuilder.__init__( self, target=TreeBuilder(element_factory=ElementExtension)) self._parser.StartNamespaceDeclHandler = self._start_ns self._parser.EndNamespaceDeclHandler = self._end_ns self.namespaces = [] XmlIfBuilderExtensionBase.__init__(self, filePath, absUrl, treeWrapper, xmlIf.elementWrapperClass)
def __init__(self, stream, *pos, **kw): self._stream = stream self._pending = deque() builder = _QueueBuilder(self._pending) self._parser = XMLParser(*pos, target=builder, **kw) self._builders = [TreeBuilder()] [method, pos, kw] = self._read() self.element = getattr(self._builders[-1], method)(*pos, **kw)
def emit(output, title_string, structure): "Write an SVG file to output representing structure." global builder builder = TreeBuilder() builder.start( "svg", dict(xmlns="http://www.w3.org/2000/svg", width=str(width), height=str(height))) title(title_string) structure.depth(3) for key, depth in [ (3, 3), (1, 5), (2, 6), (1, 10), (1, 11), (2, 15), (3, 16), (1, 22), (2, 25), (1, 25), ]: structure.key(key, depth) builder.end("svg") ElementTree(builder.close()).write(output, encoding='utf-8', xml_declaration=True) output.write("\n")
def write_xml_file(f, taxes): """ Export an XML file for the given taxonomies to the (open) file handle specified. """ x = TreeBuilder() x.start("taxonomy", {}) def f_pre(term): x.start("category", {"id": str(term.uid)}) x.start("name", {}) x.data(term.name) x.end("name") for clue, positive in term.iter_clues(): attrs = {} if not positive: attrs["negative"] = True x.start("clue", attrs) x.data(clue) x.end("clue") def f_post(term): x.end("category") for term in taxes: term.walk(f_pre, f_post) x.end("taxonomy") xml = ElementTree(x.close()) xml.write(f, xml_declaration=True, encoding="utf-8")
def message(self): builder = TreeBuilder() builder.start(self.resource_type, dict()) self.serialize(builder) builder.end(self.resource_type) msg = tostring(builder.close()) return msg
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.document = document self.doc = '' self.body = [] # the body of the document self.in_docinfo = None self.in_sidebar = None self.settings = settings = document.settings self.section_level = 0 self.title_level = 0 self.text_properties = {} # the list of properties for a run of test self.text_prefix = [] # text or commands inserted before next text run self.paragraph_properties = {} self.id = 1000 self.in_footnote = 0 self.no_text = 0 # flag to suppress any text output self.literal_text = 0 # flag to output newlines in text self.skip_encode = 0 # flag to skip the encoding of text to xml friendly output self.has_text_output = 0 # flag if any text output since last reset self.figure_count = 0 self.in_figure = 0 self.indentation = 0 # list related variables self.lists = [] self.list_level = -1 self.list_count = 0 self.list_properties = [] self.list_defs = [] self.template_list_defs = [] self.current_listdef = [] self.in_listdef = 0 self.show_list_properties = 0 self.extract_listdefs() self.substitutions = {} # table related variables self.in_table = 0 self.colspecs = [] self.row = 0 self.col = 0 self.spans = {} # for multispan columns and rows self.total_col_width = 0 # xml output variables self.doc_tree = TreeBuilder() self.doc_tree.start('w:document', {}) self.xml_spacing = 0
def start(self, tag, attrs): elem = TreeBuilder.start(self, tag, attrs) for key, value in attrs.items(): if key == _MELD_ID: if value in self.meldids: raise ValueError('Repeated meld id "%s" in source' % value) self.meldids[value] = 1 break return elem
def _elem(parent: Element, tag: str, data: Optional[str] = None) -> Element: b = TreeBuilder() b.start(tag, {}) # types: ignore if data: b.data(data) ret = b.end(tag) parent.append(ret) return ret
def coverage_meta_xml(): builder = TreeBuilder() dict_meta = dict(coverageview=dict(coveragebands=dict( coverageband_1=dict(definition='ceshi', index=1, inputcoveragebands=dict(inputcoverageband=dict( coverageName='x_wind_10m'))), coverageband_2=dict(definition='ceshi', index=1)))) coverageview_meta_info(builder, dict_meta) return builder
def _render_outline(svg: TreeBuilder, layer: Layer) -> None: w = layer.width * layer.grid h = layer.height * layer.grid path = PathBuilder() path.move_to(0, 0) path.line_to(w, 0) path.line_to(w, h) path.line_to(0, h) path.close_path() path = path.close() svg.start("path", { "d": str(path), "stroke": "red", "stroke-width": "0.25", "fill": "none", }) svg.end("path")
def emit(output, title_string, structure): "Write an SVG file to output representing structure." global builder builder = TreeBuilder() builder.start("svg", dict(xmlns="http://www.w3.org/2000/svg", width=str(width), height=str(height))) title(title_string) structure.depth(3) for key, depth in [(3, 3), (1, 5), (2, 6), (1, 10), (1, 11), (2, 15), (3, 16), (1, 22), (2, 25), (1, 25), ]: structure.key(key, depth) builder.end("svg") ElementTree(builder.close()).write(output, encoding='utf-8', xml_declaration=True) output.write("\n")
def compileClass(self): """ Compiles a complete class """ if (self.DEBUG): print "Compiling class" # Add non-terminal break for the class builder = TreeBuilder() builder.start("class", {}) builder.data("\n") self.etree = ElementTree(builder.end("class")) self.head.append(self.etree.getroot()) # Check for class keyword self.validateAndRead("keyword", "class") # Check for identifier self.validateAndRead("identifier") # Check for { symbol self.validateAndRead("symbol", '{') # Check for Class Vars while (self.compileClassVarDec()): """ BLANK ON PURPOSE!!! """ # Check for Subroutines while (self.compileSubroutine()): """ BLANK ON PURPOSE!!! """ # Check for } symbol self.validateAndRead("symbol", '}')
def __init__(self, filename): """ Opens file the parser needs to read from """ self.filename = filename.replace(".jack", "") try: inFile = open(filename, 'r') self.code = inFile.read() inFile.close() except IOError: print "Unable to open input file." exit(-2) # Setup the Element Tree builder = TreeBuilder() builder.start("tokens", {}) builder.data("\n") self.etree = ElementTree(builder.end("tokens")) # Remove Comments self.removeComments() # Strip self.code = str.strip(self.code) # Set up enum of token types self.KEYWORDS = r'(\Aclass\b)|(\Aconstructor\b)|(\Afunction\b)|(\Amethod\b)|(\Afield\b)|(\Astatic\b)|(\Avar\b)|(\Aint\b)|(\Achar\b)|(\Aboolean\b)|(\Avoid\b)|(\Atrue\b)|(\Afalse\b)|(\Anull\b)|(\Athis\b)|(\Alet\b)|(\Ado\b)|(\Aif\b)|(\Aelse\b)|(\Awhile\b)|(\Areturn\b)' self.SYMBOLS = '[(\{)|(\})|(\()|(\))|(\[)|(\])|(\.)|(,)|(;)|(\+)|(\-)|(\*)|(\/)|(&)|(|)|(<)|(>)|(=)|(~)]' # Set up token as blank self.token = 'null' self.tokenType = 'NULL'
def __init__(self, filename): """ Opens file the parser needs to read from """ self.filename = filename.replace(".jack", "") try: inFile = open(filename, 'r') self.code = inFile.read() inFile.close() except IOError: print "Unable to open input file." exit(-2) # Setup the Element Tree builder = TreeBuilder() builder.start("tokens", {}) builder.data("\n") self.etree = ElementTree(builder.end("tokens")) # Remove Comments self.removeComments() # Strip self.code = str.strip(self.code) # Set up enum of token types self.KEYWORDS = '(class)|(constructor)|(function)|(method)|(field)|(static)|(var)|(int)|(char)|(boolean)|(void)|(true)|(false)|(null)|(this)|(let)|(do)|(if)|(else)|(while)|(return)' self.SYMBOLS = '[(\{)|(\})|(\()|(\))|(\[)|(\])|(\.)|(,)|(;)|(\+)|(\-)|(\*)|(\/)|(&)|(|)|(<)|(>)|(=)|(~)]' # Set up token as blank self.token = 'null' self.tokenType = 'NULL'
def from_bytes(cls, xml_as_bytes): xml_as_string = xml_as_bytes.decode() try: return xml.etree.ElementTree.fromstring( xml_as_string, parser=XMLParser(target=TreeBuilder(element_factory=cls))) except Exception as err: # xml.etree.ElementTree.ParseError if type(err).__name__ == 'ParseError': return cls.from_string( xml_as_string.replace("<style scoped>", "<style scoped='1'>")) else: raise
def _addTestRequirement(self, testScriptFileName, testScript): testRequirement = et.SubElement(testScript, 'testRequirement') requirementFileName = os.path.dirname(testScriptFileName) + os.sep + 'Req.xml' try: requirementFile = et.parse(requirementFileName, TreeBuilder()) root = requirementFile.getroot() for requirement in root.getiterator('REQ'): xmlReq = et.SubElement(testRequirement, 'REQ') et.SubElement(xmlReq, 'ID').text = requirement.get('id') for desc in requirement.getiterator('REQ_DESCRIPTION'): et.SubElement(xmlReq, 'DESCRIPTION').text = desc.text except IOError: pass
def addTerminal(self): """ Adds a terminal element to the XML tree """ builder = TreeBuilder() builder.start(self.tag, {}) builder.data(" %s " % (self.data)) temp = builder.end(self.tag) temp.tail = "\n" self.head[-1].append(temp)
class XmppXmlHandler(object): STREAM_TAG = '{http://etherx.jabber.org/streams}stream' def __init__(self): self._stack = 0 self._builder = TreeBuilder() self._results = deque() def data(self, data): self._builder.data(data) def start(self, tag, attrib): if tag == self.STREAM_TAG: return self._builder.start(tag, attrib) self._stack += 1 def end(self, tag): self._stack -= 1 elem = self._builder.end(tag) if self._stack == 0: self._results.append(elem) def get_elem(self): """If a top-level XML element has been completed since the last call to get_elem, return it; else return None.""" try: elem = self._results.popleft() if elem.tag.endswith('failure') or elem.tag.endswith('error'): raise Exception("XMPP Error received - %s" % elem.tag) return elem except IndexError: return None
class XmppXmlHandler(object): STREAM_TAG = "{http://etherx.jabber.org/streams}stream" def __init__(self): self._stack = 0 self._builder = TreeBuilder() self._results = deque() def data(self, data): self._builder.data(data) def start(self, tag, attrib): if tag == self.STREAM_TAG: return self._builder.start(tag, attrib) self._stack += 1 def end(self, tag): self._stack -= 1 elem = self._builder.end(tag) if self._stack == 0: self._results.append(elem) def get_elem(self): """If a top-level XML element has been completed since the last call to get_elem, return it; else return None.""" try: elem = self._results.popleft() if elem.tag.endswith("failure") or elem.tag.endswith("error"): raise Exception("XMPP Error received - %s" % elem.tag) return elem except IndexError: return None
def parse(self, source=None, parser=None): try: if not parser: parser = XMLParser(target=TreeBuilder()) while 1: data = self.mergeScreenConfigs() if not data: break parser.feed(data) return parser.close() # self._root = parser.close() # return self._root except: pass
def addNonTerminal(self, name): """ Adds a non-terminal element to the XML tree """ builder = TreeBuilder() builder.start(name, {}) builder.data("\n") temp = builder.end(name) temp.tail = "\n" self.head[-1].append(temp) self.head.append(temp)
def addElement(self, tag): """ Adds the current token to the XML ElementTree """ e = self.etree.getroot() builder = TreeBuilder() builder.start(tag, {}) builder.data(" %s " % (self.token)) temp = builder.end(tag) temp.tail = "\n" e.append(temp) return self.token
def parseStream(stream, child_discard_set, callback_dict, catchall=None): """ stream (opened read stream) Where XML data is read from. Expects a "read" method following the "file" API. child_discard_set (anything implementing '__in__') Set of tags whose children should be discarded while parsing. callback_dict (dict) Dict of callables per event and tag type, to be called for these combinations when encountered. Expected structure is: { (event, tag): callable(element) -> None, } See xml.etree.ElementTree.iterparse for a list of possible events. catchall (None, callable(element) -> None) Callback triggered for all actually triggered events but not declared in callback_dict. If None, a ValueError exception will be raised when an element whose tag is present in child_discard_set receives a non-child-discarding child for which neither "start" nor "end" callback exist. """ if catchall is None: catchall = lambda x: None callback_set = { y for x, y in callback_dict if x in ('start', 'end', None) # None is equivalent to 'end' } else: callback_set = INFINITE_SET def elementFactory(tag, attrs): if tag in child_discard_set: return ChildDiscardingElement(tag, callback_set) return RestrictedElement(tag, attrs) for event, elem in iterparse( stream, events={x for x, _ in callback_dict}, parser=XMLParser(target=TreeBuilder( element_factory=elementFactory, ), ), ): callback_dict.get((event, elem.tag), catchall)(elem)
def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.document = document self.doc = "" self.body = [] # the body of the document self.in_docinfo = None self.in_sidebar = None self.settings = settings = document.settings self.section_level = 0 self.title_level = 0 self.text_properties = {} # the list of properties for a run of test self.text_prefix = [] # text or commands inserted before next text run self.paragraph_properties = {} self.id = 1000 self.in_footnote = 0 self.no_text = 0 # flag to suppress any text output self.literal_text = 0 # flag to output newlines in text self.skip_encode = 0 # flag to skip the encoding of text to xml friendly output self.has_text_output = 0 # flag if any text output since last reset self.figure_count = 0 self.in_figure = 0 self.indentation = 0 # list related variables self.lists = [] self.list_level = -1 self.list_count = 0 self.list_properties = [] self.list_defs = [] self.template_list_defs = [] self.current_listdef = [] self.in_listdef = 0 self.show_list_properties = 0 self.extract_listdefs() self.substitutions = {} # table related variables self.in_table = 0 self.colspecs = [] self.row = 0 self.col = 0 self.spans = {} # for multispan columns and rows self.total_col_width = 0 # xml output variables self.doc_tree = TreeBuilder() self.doc_tree.start("w:document", {}) self.xml_spacing = 0
class _XHTMLParser(parser.HTMLParser): # A helper class for parsing XHTML into an xml.etree.ElementTree def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.builder = TreeBuilder(element_factory=Element) def handle_starttag(self, tag, attrs): self.builder.start(tag, dict(attrs),) def handle_endtag(self, tag): self.builder.end(tag) def handle_data(self, data): self.builder.data(data)
def write_xml(f, taxes, _solr=None, rows=0): """ Export an XML file for the given taxonomies to the (open) file handle specified. If a SOLR connection is supplied, then include document elements for each category. """ x = TreeBuilder() x.start("taxonomy", {}) def f_pre(term): x.start("category", {"id": str(term.uid)}) x.start("name", {}) x.data(term.name) x.end("name") for clue, positive in term.iter_clues(): attrs = {} if not positive: attrs["negative"] = "true" x.start("clue", attrs) x.data(clue) x.end("clue") if _solr is not None: count, docs = get_docs_for_category(_solr, term, rows=rows) x.start("count", {}) x.data(str(count)) x.end("count") for doc_id, title, score in docs: x.start("doc", {"id": doc_id, "score": str(score)}) x.start("name", {}) x.data(title) x.end("name") x.end("doc") def f_post(term): x.end("category") for term in taxes: term.walk(f_pre, f_post) x.end("taxonomy") xml = ElementTree(x.close()) xml.write(f, xml_declaration=True, encoding="utf-8")
def data(self, data): assert isinstance(data, basestring), 'Got: %s' % data _TreeBuilder.data(self, data)
def __init__(self): _TreeBuilder.__init__(self, Element)
def _record_parse(self, key=None, **kwargs): """ Returns an element tree structure corresponding to a toolbox data file with all markers at the same level. Thus the following Toolbox database:: \_sh v3.0 400 Rotokas Dictionary \_DateStampHasFourDigitYear \lx kaa \ps V.A \ge gag \gp nek i pas \lx kaa \ps V.B \ge strangle \gp pasim nek after parsing will end up with the same structure (ignoring the extra whitespace) as the following XML fragment after being parsed by ElementTree:: <toolbox_data> <header> <_sh>v3.0 400 Rotokas Dictionary</_sh> <_DateStampHasFourDigitYear/> </header> <record> <lx>kaa</lx> <ps>V.A</ps> <ge>gag</ge> <gp>nek i pas</gp> </record> <record> <lx>kaa</lx> <ps>V.B</ps> <ge>strangle</ge> <gp>pasim nek</gp> </record> </toolbox_data> :param key: Name of key marker at the start of each record. If set to None (the default value) the first marker that doesn't begin with an underscore is assumed to be the key. :type key: str :param kwargs: Keyword arguments passed to ``StandardFormat.fields()`` :type kwargs: dict :rtype: ElementTree._ElementInterface :return: contents of toolbox data divided into header and records """ builder = TreeBuilder() builder.start('toolbox_data', {}) builder.start('header', {}) in_records = False for mkr, value in self.fields(**kwargs): if key is None and not in_records and mkr[0] != '_': key = mkr if mkr == key: if in_records: builder.end('record') else: builder.end('header') in_records = True builder.start('record', {}) builder.start(mkr, {}) builder.data(value) builder.end(mkr) if in_records: builder.end('record') else: builder.end('header') builder.end('toolbox_data') return builder.close()
def parse(self, encoding=None, errors='strict', **kwargs): """ Return the contents of toolbox settings file with a nested structure. :param encoding: encoding used by settings file :type encoding: str :param errors: Error handling scheme for codec. Same as ``decode()`` builtin method. :type errors: str :param kwargs: Keyword arguments passed to ``StandardFormat.fields()`` :type kwargs: dict :rtype: ElementTree._ElementInterface """ builder = TreeBuilder() for mkr, value in self.fields(encoding=encoding, errors=errors, **kwargs): # Check whether the first char of the field marker # indicates a block start (+) or end (-) block=mkr[0] if block in ("+", "-"): mkr=mkr[1:] else: block=None # Build tree on the basis of block char if block == "+": builder.start(mkr, {}) builder.data(value) elif block == '-': builder.end(mkr) else: builder.start(mkr, {}) builder.data(value) builder.end(mkr) return builder.close()
def scheme_to_etree(scheme, data_format="literal", pickle_fallback=False): """ Return an `xml.etree.ElementTree` representation of the `scheme. """ builder = TreeBuilder(element_factory=Element) builder.start("scheme", {"version": "2.0", "title": scheme.title or "", "description": scheme.description or ""}) ## Nodes node_ids = defaultdict(inf_range().__next__) builder.start("nodes", {}) for node in scheme.nodes: desc = node.description attrs = {"id": str(node_ids[node]), "name": desc.name, "qualified_name": desc.qualified_name, "project_name": desc.project_name or "", "version": desc.version or "", "title": node.title, } if node.position is not None: attrs["position"] = str(node.position) if type(node) is not SchemeNode: attrs["scheme_node_type"] = "%s.%s" % (type(node).__name__, type(node).__module__) builder.start("node", attrs) builder.end("node") builder.end("nodes") ## Links link_ids = defaultdict(inf_range().__next__) builder.start("links", {}) for link in scheme.links: source = link.source_node sink = link.sink_node source_id = node_ids[source] sink_id = node_ids[sink] attrs = {"id": str(link_ids[link]), "source_node_id": str(source_id), "sink_node_id": str(sink_id), "source_channel": link.source_channel.name, "sink_channel": link.sink_channel.name, "enabled": "true" if link.enabled else "false", } builder.start("link", attrs) builder.end("link") builder.end("links") ## Annotations annotation_ids = defaultdict(inf_range().__next__) builder.start("annotations", {}) for annotation in scheme.annotations: annot_id = annotation_ids[annotation] attrs = {"id": str(annot_id)} data = None if isinstance(annotation, SchemeTextAnnotation): tag = "text" attrs.update({"rect": repr(annotation.rect)}) # Save the font attributes font = annotation.font attrs.update({"font-family": font.get("family", None), "font-size": font.get("size", None)}) attrs = [(key, value) for key, value in attrs.items() if value is not None] attrs = dict((key, str(value)) for key, value in attrs) data = annotation.text elif isinstance(annotation, SchemeArrowAnnotation): tag = "arrow" attrs.update({"start": repr(annotation.start_pos), "end": repr(annotation.end_pos)}) # Save the arrow color try: color = annotation.color attrs.update({"fill": color}) except AttributeError: pass data = None else: log.warning("Can't save %r", annotation) continue builder.start(tag, attrs) if data is not None: builder.data(data) builder.end(tag) builder.end("annotations") builder.start("thumbnail", {}) builder.end("thumbnail") # Node properties/settings builder.start("node_properties", {}) for node in scheme.nodes: data = None if node.properties: try: data, format = dumps(node.properties, format=data_format, pickle_fallback=pickle_fallback) except Exception: log.error("Error serializing properties for node %r", node.title, exc_info=True) if data is not None: builder.start("properties", {"node_id": str(node_ids[node]), "format": format}) builder.data(data) builder.end("properties") builder.end("node_properties") builder.end("scheme") root = builder.close() tree = ElementTree(root) return tree
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.builder = TreeBuilder(element_factory=Element)
def __init__(self): TreeBuilder.__init__(self) self._characterBuffer = None self.startDocument()
def __init__(self): self._stack = 0 self._builder = TreeBuilder() self._results = deque()
def generate_nic(network): builder = TreeBuilder() builder.start('interface', {'type': network['type']}) builder.start('mac', {'address': network['mac']}) builder.end('mac') builder.start('source', network['source']) builder.end('source') builder.start('model', {'type':'virtio'}) builder.end('model') builder.end('interface') return builder.close() # vim:set sw=4 ts=4 et: # -*- coding: utf-8 -*-
def generate_disk(disk, devicename): """ Creates XML representation of disk for libvirt based on disk_definition disk_definition is something like { 'type': 'network', 'device': 'disk', 'format': 'raw', 'source': { 'protocol': 'sheepdog', 'name': 'Alice', 'hosts': [('127.0.0.1', '7000'),], }, } or { 'type': 'file', 'device': 'disk', 'format': 'qcow2', 'source': { 'file': '/var/lib/libvirt/images/Alice.img', }, }, devicename is string representing devicename (eg. vda, vdb, ...) """ builder = TreeBuilder() builder.start('disk', {'type': disk['type'], 'device': disk['device']}) builder.start('source', sans(disk['source'], 'hosts')) for host in disk['source'].get('hosts', []): builder.start('host', {'name': host[0], 'port': host[1]}) builder.end('host') builder.end('source') builder.start('target', {'dev': devicename, 'bus': 'virtio'}) builder.end('target') builder.start('driver', {'name': 'qemu', 'cache': 'none', 'type': disk['format']}) builder.end('driver') builder.end('disk') return builder.close()
def start(self, tag, attrs=None): if attrs is None: attrs = {} _TreeBuilder.start(self, tag, attrs)
# http:#nikerunning.nike.com/nikeplus/v2/services/app/get_gps_detail.jsp?_plus=true&id=<run-id>&format=json # # Save it to a file that ends in .json, and run this script: ./nike2gpx <filename>.json # This will create a file <filename>.gpx in GPX format # from xml.etree.ElementTree import TreeBuilder, ElementTree from datetime import datetime import json import sys data = json.load(open(sys.argv[1], "r")) route = data["plusService"]["route"] builder = TreeBuilder() gpxAttrs = { "version": "1.1", "creator": "Nike2GPX", "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance", "xmlns": "http://www.topografix.com/GPX/1/1", "xsi:schemaLocation": "http://www.topografix.com/GPX/1/1 http://www.topografix.com/gpx/1/1/gpx.xsd" } builder.start("gpx", gpxAttrs) builder.start("metadata", {}) builder.start("name", {}) builder.data("Run " + sys.argv[1][:-5]) builder.end("name")