Esempio n. 1
0
def doit(args):
    ofile1 = args.outfile1
    ofile2 = args.outfile2
    ofile3 = args.outfile3

    xmlstring = "<item>\n<subitem hello='world'>\n<subsub name='moon'>\n<value>lunar</value>\n</subsub>\n</subitem>"
    xmlstring += "<subitem hello='jupiter'>\n<subsub name='moon'>\n<value>IO</value>\n</subsub>\n</subitem>\n</item>"

    # Using etutil's xmlitem class

    xmlobj = etutil.xmlitem()
    xmlobj.etree = ET.fromstring(xmlstring)

    etwobj = etutil.ETWriter(xmlobj.etree)
    etwobj.serialize_xml(xmlobj.write_to_xml)

    ofile1.write(xmlobj.outxmlstr)

    # Just using ETWriter

    etwobj = etutil.ETWriter(ET.fromstring(xmlstring))
    etwobj.serialize_xml(ofile2.write)

    # Changing parameters

    etwobj = etutil.ETWriter(ET.fromstring(xmlstring))
    etwobj.indentIncr = "    "
    etwobj.indentFirst = ""
    etwobj.serialize_xml(ofile3.write)

    # Close files and exit
    ofile1.close()
    ofile2.close()
    ofile3.close()
    return
Esempio n. 2
0
def writeXMLobject(dtreeitem, font, dirn, filen, exists, fobject=False):
    params = font.outparams
    object = dtreeitem if fobject else dtreeitem.fileObject  # Set fobject to True if a file object is passed ratehr than dtreeitem
    if object.outparams:
        params = object.outparams  # override default params with object-specific ones
    indentFirst = params["indentFirst"]
    attribOrder = {}
    if object.type in params['attribOrders']:
        attribOrder = params['attribOrders'][object.type]
    if object.type == "plist":
        indentFirst = params["plistIndentFirst"]
        object.etree.attrib[
            ".doctype"] = 'plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"'

    # Format ET data if any data parameters are set
    if params["sortDicts"] or params["precision"] is not None:
        normETdata(object.etree, params, type=object.type)

    etw = ETU.ETWriter(object.etree,
                       attributeOrder=attribOrder,
                       indentIncr=params["indentIncr"],
                       indentFirst=indentFirst,
                       indentML=params["indentML"],
                       precision=params["precision"],
                       numAttribs=params["numAttribs"])
    etw.serialize_xml(object.write_to_xml)
    # Now we have the output xml, need to compare with existing item's xml, if present
    changed = True

    if exists:  # File already on disk
        if exists == "same":  # Output and input locations the same
            oxmlstr = object.inxmlstr
        else:  # Read existing XML from disk
            oxmlstr = ""
            try:
                oxml = open(os.path.join(dirn, filen), "r")
            except Exception as e:
                print e
                sys.exit(1)
            for line in oxml.readlines():
                oxmlstr += line
            oxml.close()
        if oxmlstr == object.outxmlstr: changed = False

    if changed: object.write_to_file(dirn, filen)
    if not fobject:
        dtreeitem.written = True  # Mark as True, even if not changed - the file should still be there!
Esempio n. 3
0
 def save(self, file):
     self.outxmlstr = ""
     element = self.create_element()
     etw = ETU.ETWriter(element, inlineelem=["em"])
     self.outxmlstr = etw.serialize_xml()
     file.write(self.outxmlstr)