MCPcopy Index your code
hub / github.com/RustPython/RustPython / TreeBuilder

Class TreeBuilder

Lib/xml/etree/ElementTree.py:1398–1516  ·  view source on GitHub ↗

Generic element structure builder. This builder converts a sequence of start, data, and end method calls to a well-formed element structure. You can use this class to build an element structure using a custom XML parser, or a parser for some other XML-like format. *element_fac

Source from the content-addressed store, hash-verified

1396
1397
1398class TreeBuilder:
1399 """Generic element structure builder.
1400
1401 This builder converts a sequence of start, data, and end method
1402 calls to a well-formed element structure.
1403
1404 You can use this class to build an element structure using a custom XML
1405 parser, or a parser for some other XML-like format.
1406
1407 *element_factory* is an optional element factory which is called
1408 to create new Element instances, as necessary.
1409
1410 *comment_factory* is a factory to create comments to be used instead of
1411 the standard factory. If *insert_comments* is false (the default),
1412 comments will not be inserted into the tree.
1413
1414 *pi_factory* is a factory to create processing instructions to be used
1415 instead of the standard factory. If *insert_pis* is false (the default),
1416 processing instructions will not be inserted into the tree.
1417 """
1418 def __init__(self, element_factory=None, *,
1419 comment_factory=None, pi_factory=None,
1420 insert_comments=False, insert_pis=False):
1421 self._data = [] # data collector
1422 self._elem = [] # element stack
1423 self._last = None # last element
1424 self._root = None # root element
1425 self._tail = None # true if we're after an end tag
1426 if comment_factory is None:
1427 comment_factory = Comment
1428 self._comment_factory = comment_factory
1429 self.insert_comments = insert_comments
1430 if pi_factory is None:
1431 pi_factory = ProcessingInstruction
1432 self._pi_factory = pi_factory
1433 self.insert_pis = insert_pis
1434 if element_factory is None:
1435 element_factory = Element
1436 self._factory = element_factory
1437
1438 def close(self):
1439 """Flush builder buffers and return toplevel document Element."""
1440 assert len(self._elem) == 0, "missing end tags"
1441 assert self._root is not None, "missing toplevel element"
1442 return self._root
1443
1444 def _flush(self):
1445 if self._data:
1446 if self._last is not None:
1447 text = "".join(self._data)
1448 if self._tail:
1449 assert self._last.tail is None, "internal error (tail)"
1450 self._last.tail = text
1451 else:
1452 assert self._last.text is None, "internal error (text)"
1453 self._last.text = text
1454 self._data = []
1455

Callers 5

__init__Method · 0.85
XMLFunction · 0.85
XMLIDFunction · 0.85
fromstringlistFunction · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected