# coding=utf-8
"""
XML utils
"""

from __future__ import print_function

try:
    import lxml.etree as ET
    from lxml.builder import ElementMaker

    def output_xml(xml, filename):
        ET.ElementTree(xml).write(filename, encoding='utf-8', xml_declaration=True, pretty_print=True)
except ImportError:
    import functools
    #noinspection PyRedeclaration
    import xml.etree.cElementTree as ET

    #noinspection PyRedeclaration
    class ElementMaker(object):
        def __call__(self, tag, *children, **attrib):
            elem = ET.Element(tag, attrib)
            for item in children:
                if isinstance(item, dict):
                    elem.attrib.update(item)
                elif isinstance(item, basestring):
                    if len(elem):
                        elem[-1].tail = (elem[-1].tail or "") + item
                    else:
                        elem.text = (elem.text or "") + item
                elif ET.iselement(item):
                    elem.append(item)
                else:
                    raise TypeError("bad argument: '{!r}".format(item))
            return elem

        def __getattr__(self, tag):
            return functools.partial(self, tag)

    def indent(elem, level=0):
        i = '\n' + '  ' * level
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + '  '
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
            for elem in elem:
                indent(elem, level + 1)
            if not elem.tail or not elem.tail.strip():
                elem.tail = i
        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i

    #noinspection PyRedeclaration
    def output_xml(xml, filename):
        indent(xml)
        ET.ElementTree(xml).write(filename, encoding='utf-8')


# create factory object
E = ElementMaker()

@@ -70,17 +70,8 @@
 on%0A%0A
-try:%0A    
 impo
@@ -90,20 +90,16 @@
 e as ET%0A
-    
 from lxm
@@ -129,20 +129,17 @@
 tMaker%0A%0A
-    
+%0A
 def outp
@@ -157,28 +157,24 @@
  filename):%0A
-    
     ET.Eleme
@@ -264,1613 +264,8 @@
 ue)%0A
-except ImportError:%0A    import functools%0A    #noinspection PyRedeclaration%0A    import xml.etree.cElementTree as ET%0A%0A    #noinspection PyRedeclaration%0A    class ElementMaker(object):%0A        def __call__(self, tag, *children, **attrib):%0A            elem = ET.Element(tag, attrib)%0A            for item in children:%0A                if isinstance(item, dict):%0A                    elem.attrib.update(item)%0A                elif isinstance(item, basestring):%0A                    if len(elem):%0A                        elem%5B-1%5D.tail = (elem%5B-1%5D.tail or %22%22) + item%0A                    else:%0A                        elem.text = (elem.text or %22%22) + item%0A                elif ET.iselement(item):%0A                    elem.append(item)%0A                else:%0A                    raise TypeError(%22bad argument: '%7B!r%7D%22.format(item))%0A            return elem%0A%0A        def __getattr__(self, tag):%0A            return functools.partial(self, tag)%0A%0A    def indent(elem, level=0):%0A        i = '%5Cn' + '  ' * level%0A        if len(elem):%0A            if not elem.text or not elem.text.strip():%0A                elem.text = i + '  '%0A            if not elem.tail or not elem.tail.strip():%0A                elem.tail = i%0A            for elem in elem:%0A                indent(elem, level + 1)%0A            if not elem.tail or not elem.tail.strip():%0A                elem.tail = i%0A        else:%0A            if level and (not elem.tail or not elem.tail.strip()):%0A                elem.tail = i%0A%0A    #noinspection PyRedeclaration%0A    def output_xml(xml, filename):%0A        indent(xml)%0A        ET.ElementTree(xml).write(filename, encoding='utf-8')%0A
 %0A%0A# 

remove ElementTree fallback, and require lxml