def test_pretty_print(): root = etree.Element("root") attrs = OrderedDict([("c", "2"), ("b", "1"), ("a", "0")]) etree.SubElement(root, "element", attrs).text = "text" etree.SubElement(root, "element").text = "text" root.append(etree.Element("empty-element")) result = etree.tostring(root, encoding="unicode", pretty_print=True) assert result == ("<root>\n" ' <element c="2" b="1" a="0">text</element>\n' " <element>text</element>\n" " <empty-element/>\n" "</root>\n")
def _dict_element(d, ctx): el = etree.Element("dict") items = d.items() if ctx.sort_keys: items = sorted(items) ctx.indent_level += 1 for key, value in items: if not isinstance(key, basestring): if ctx.skipkeys: continue raise TypeError("keys must be strings") k = etree.SubElement(el, "key") k.text = tounicode(key, "utf-8") el.append(_make_element(value, ctx)) ctx.indent_level -= 1 return el
def _dict_element(d: Mapping[str, PlistEncodable], ctx: SimpleNamespace) -> etree.Element: el = etree.Element("dict") items = d.items() if ctx.sort_keys: items = sorted(items) # type: ignore ctx.indent_level += 1 for key, value in items: if not isinstance(key, str): if ctx.skipkeys: continue raise TypeError("keys must be strings") k = etree.SubElement(el, "key") k.text = tounicode(key, "utf-8") el.append(_make_element(value, ctx)) ctx.indent_level -= 1 return el