예제 #1
0
def testSaveIntoOutputBuffer():
    """
    Similar to the previous two tests, except this time we invoke the save
    methods on the output buffer object and pass in an XML node object.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = str_io()
    doc = libxml2.parseDoc(input)
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFileTo(doc, 'UTF-8')
    if f.getvalue() != expected:
        print('outputBuffer.saveFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
    f = str_io()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFormatFileTo(doc, 'UTF-8', 1)
    if f.getvalue() != expected:
        print('outputBuffer.saveFormatFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
    doc.freeDoc()
예제 #2
0
def testSaveIntoOutputBuffer():
    """
    Similar to the previous two tests, except this time we invoke the save
    methods on the output buffer object and pass in an XML node object.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = StringIO.StringIO()
    doc = libxml2.parseDoc(input)
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFileTo(doc, 'UTF-8')
    if f.getvalue() != expected:
        print 'outputBuffer.saveFileTo() call failed.'
        print '     got: %s' % repr(f.getvalue())
        print 'expected: %s' % repr(expected)
        sys.exit(1)
    f = StringIO.StringIO()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    buf.saveFormatFileTo(doc, 'UTF-8', 1)
    if f.getvalue() != expected:
        print 'outputBuffer.saveFormatFileTo() call failed.'
        print '     got: %s' % repr(f.getvalue())
        print 'expected: %s' % repr(expected)
        sys.exit(1)
    doc.freeDoc()
예제 #3
0
파일: svgflatten.py 프로젝트: PKRG/jpoker
def flatten(string):
    doc = libxml2.parseDoc(string)
    context = doc.xpathNewContext()
    result = context.xpathEval("//use")
    for orig in result:
        xlink = orig.prop('href')
        id = orig.prop('id')
        id_length = len(id)
        nodes = context.xpathEval('//g[@id="'+xlink[1:]+'"]')
        if len(nodes) == 0:
            nodes = context.xpathEval('//image[@id="'+xlink[1:]+'"]')
        node = nodes[0]
        copy = node.copyNode(extended=True)
        copy.removeNsDef(None)
        copy_context = doc.xpathNewContext()
        copy_context.setContextNode(copy)
        for copy_id in copy_context.xpathEval('.//@id'):
            copy_id.setContent(id + copy_id.content[id_length:])
        tx, ty = re.match('translate\((-?\d+\.?\d*.*),(-?\d+\.?\d*.*)\)', orig.prop('transform')).groups()
        transform = { 'x': float(tx), 'y': float(ty) }
        for c in [ 'x', 'y' ]:
            for coord in copy_context.xpathEval('.//@' + c):
                coord.setContent(str(int(round(float(coord.content) + transform[c]))))
        orig.replaceNode(copy)

    f = StringIO()
    buf = libxml2.createOutputBuffer(f, None)
    doc.saveFileTo(buf, None)
    return f.getvalue()
예제 #4
0
파일: common.py 프로젝트: czchen/h4-scripts
def get_wikidot_content_body(URL):
    xmlfile = tempfile.mktemp()
    htmlfile = tempfile.mktemp()

    os.system("wget -O " + htmlfile + " " + URL)
    the_html = file2string(htmlfile)
    os.system("rm " + htmlfile)

    the_xml = html2xml(the_html)
    string2file(the_xml, xmlfile)
    doc = libxml2.parseFile(xmlfile)

    ctxt = doc.xpathNewContext()
    ctxt.xpathRegisterNs('xhtml', 'http://www.w3.org/1999/xhtml')
    rows = ctxt.xpathEval('//xhtml:div[@id="page-content"]')
    ctxt.xpathFreeContext()

    f = StringIO.StringIO()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    rows[0].docSetRootElement(doc)
    doc.saveFileTo(buf, 'UTF-8')

    os.system("rm " + xmlfile)
    result = f.getvalue()

    return result
예제 #5
0
def flatten(string):
    doc = libxml2.parseDoc(string)
    context = doc.xpathNewContext()
    result = context.xpathEval("//use")
    for orig in result:
        xlink = orig.prop('href')
        id = orig.prop('id')
        id_length = len(id)
        nodes = context.xpathEval('//g[@id="' + xlink[1:] + '"]')
        if len(nodes) == 0:
            nodes = context.xpathEval('//image[@id="' + xlink[1:] + '"]')
        node = nodes[0]
        copy = node.copyNode(extended=True)
        copy.removeNsDef(None)
        copy_context = doc.xpathNewContext()
        copy_context.setContextNode(copy)
        for copy_id in copy_context.xpathEval('.//@id'):
            copy_id.setContent(id + copy_id.content[id_length:])
        tx, ty = re.match('translate\((-?\d+\.?\d*.*),(-?\d+\.?\d*.*)\)',
                          orig.prop('transform')).groups()
        transform = {'x': float(tx), 'y': float(ty)}
        for c in ['x', 'y']:
            for coord in copy_context.xpathEval('.//@' + c):
                coord.setContent(
                    str(int(round(float(coord.content) + transform[c]))))
        orig.replaceNode(copy)

    f = StringIO()
    buf = libxml2.createOutputBuffer(f, None)
    doc.saveFileTo(buf, None)
    return f.getvalue()
예제 #6
0
def testSaveFormattedDocToBuffer():
    input = "<outer><inner>Some text</inner><inner/></outer>"
    # The formatted and non-formatted versions of the output.
    expected = (
        """\
<?xml version="1.0" encoding="UTF-8"?>
<outer><inner>Some text</inner><inner/></outer>
""",
        """\
<?xml version="1.0" encoding="UTF-8"?>
<outer>
  <inner>Some text</inner>
  <inner/>
</outer>
""",
    )
    doc = libxml2.parseDoc(input)
    for i in (0, 1):
        f = str_io()
        buf = libxml2.createOutputBuffer(f, "UTF-8")
        doc.saveFormatFileTo(buf, "UTF-8", i)
        if f.getvalue() != expected[i]:
            print("xmlDoc.saveFormatFileTo() call failed.")
            print("     got: %s" % repr(f.getvalue()))
            print("expected: %s" % repr(expected[i]))
            sys.exit(1)
    doc.freeDoc()
예제 #7
0
def testSimpleBufferWrites():
    f = StringIO.StringIO()
    buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
    buf.write(3, "foo")
    buf.writeString("bar")
    buf.close()

    if f.getvalue() != "foobar":
        print "Failed to save to StringIO"
        sys.exit(1)
예제 #8
0
def testSimpleBufferWrites():
    f = str_io()
    buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
    buf.write(3, "foo")
    buf.writeString("bar")
    buf.close()

    if f.getvalue() != "foobar":
        print("Failed to save to StringIO")
        sys.exit(1)
예제 #9
0
 def write(self, fd, formatted=True):
     """
     Write the Metadata information to the given file descriptor
     """
     s = StringIO()
     buf = libxml2.createOutputBuffer(s, "UTF-8")
     buf.saveFormatFileTo(self.doc, "UTF-8", formatted)
     header = self.Header()
     header.size = s.len
     header.write(fd)
     fd.write(s.getvalue())
예제 #10
0
    def SendReport(self, xmldoc):
        if xmldoc.type != 'document_xml':
            raise Exception, "Input is not XML document"

        fbuf = StringIO.StringIO()
        xmlbuf = libxml2.createOutputBuffer(fbuf, 'UTF-8')
        doclen = xmldoc.saveFileTo(xmlbuf, 'UTF-8')

        compr = bz2.BZ2Compressor(9)
        cmpr = compr.compress(fbuf.getvalue())
        data = base64.b64encode(cmpr + compr.flush())
        ret = self.srv.SendReport(self.hostname, data)
        print "rtevalclient::SendReport() - Sent %i bytes (XML document length: %i bytes, compression ratio: %.02f%%)" % (len(data), doclen, (1-(float(len(data)) / float(doclen)))*100 )
        return ret
예제 #11
0
    def SendReport(self, xmldoc):
        if xmldoc.type != 'document_xml':
            raise Exception("Input is not XML document")

        fbuf = io.StringIO()
        xmlbuf = libxml2.createOutputBuffer(fbuf, 'UTF-8')
        doclen = xmldoc.saveFileTo(xmlbuf, 'UTF-8')

        compr = bz2.BZ2Compressor(9)
        cmpr = compr.compress(fbuf.getvalue())
        data = base64.b64encode(cmpr + compr.flush())
        ret = self.srv.SendReport(self.hostname, data)
        print("rtevalclient::SendReport() - Sent %i bytes (XML document length: %i bytes, compression ratio: %.02f%%)" % (len(data), doclen, (1-(float(len(data)) / float(doclen)))*100 ))
        return ret
예제 #12
0
    def SendReport(self, xmldoc):
        if xmldoc.type != 'document_xml':
            raise Exception, "Input is not XML document"

        f = open('/home/kchalas/Workspace/rteval/log', 'r')
        fbuf = StringIO.StringIO()
        xmlbuf = libxml2.createOutputBuffer(fbuf, 'UTF-8')
        doclen = xmldoc.saveFileTo(xmlbuf, 'UTF-8')

        compr = bz2.BZ2Compressor(9)
        cmpr = compr.compress(fbuf.getvalue() + '\n<!--' + f.read() + '\n-->')
        data = base64.b64encode(cmpr + compr.flush())
        ret = self.srv.SendReport(self.hostname, data)
        print "rtevalclient::SendReport() - Sent %i bytes (XML document length: %i bytes, compression ratio: %.02f%%)" % (
            len(data), doclen, (1 - (float(len(data)) / float(doclen))) * 100)
        return ret
예제 #13
0
    def formatXML(self, fn):
        """
        Format XML file.

        @param fn: XML filename.
        """
        ctxt = libxml2.createFileParserCtxt(fn)
        ctxt.parseDocument()
        doc = ctxt.doc()

        # save formatted file
        f = open(fn, 'w')
        buf = libxml2.createOutputBuffer(f, 'UTF-8')
        doc.saveFormatFileTo(buf, 'UTF-8', 1)
        f.close()

        doc.freeDoc()
        del ctxt
예제 #14
0
def testSaveDocToBuffer():
    """
    Regression test for bug #154294.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = StringIO.StringIO()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    doc = libxml2.parseDoc(input)
    doc.saveFileTo(buf, 'UTF-8')
    doc.freeDoc()
    if f.getvalue() != expected:
        print 'xmlDoc.saveFileTo() call failed.'
        print '     got: %s' % repr(f.getvalue())
        print 'expected: %s' % repr(expected)
        sys.exit(1)
예제 #15
0
def testSaveDocToBuffer():
    """
    Regression test for bug #154294.
    """
    input = '<foo>Hello</foo>'
    expected = '''\
<?xml version="1.0" encoding="UTF-8"?>
<foo>Hello</foo>
'''
    f = str_io()
    buf = libxml2.createOutputBuffer(f, 'UTF-8')
    doc = libxml2.parseDoc(input)
    doc.saveFileTo(buf, 'UTF-8')
    doc.freeDoc()
    if f.getvalue() != expected:
        print('xmlDoc.saveFileTo() call failed.')
        print('     got: %s' % repr(f.getvalue()))
        print('expected: %s' % repr(expected))
        sys.exit(1)
예제 #16
0
def testSaveFormattedDocToBuffer():
    input = '<outer><inner>Some text</inner><inner/></outer>'
    # The formatted and non-formatted versions of the output.
    expected = ('''\
<?xml version="1.0" encoding="UTF-8"?>
<outer><inner>Some text</inner><inner/></outer>
''', '''\
<?xml version="1.0" encoding="UTF-8"?>
<outer>
  <inner>Some text</inner>
  <inner/>
</outer>
''')
    doc = libxml2.parseDoc(input)
    for i in (0, 1):
        f = StringIO.StringIO()
        buf = libxml2.createOutputBuffer(f, 'UTF-8')
        doc.saveFormatFileTo(buf, 'UTF-8', i)
        if f.getvalue() != expected[i]:
            print 'xmlDoc.saveFormatFileTo() call failed.'
            print '     got: %s' % repr(f.getvalue())
            print 'expected: %s' % repr(expected[i])
            sys.exit(1)
    doc.freeDoc()
예제 #17
0
def testSaveFormattedDocToBuffer():
    input = '<outer><inner>Some text</inner><inner/></outer>'
    # The formatted and non-formatted versions of the output.
    expected = ('''\
<?xml version="1.0" encoding="UTF-8"?>
<outer><inner>Some text</inner><inner/></outer>
''', '''\
<?xml version="1.0" encoding="UTF-8"?>
<outer>
  <inner>Some text</inner>
  <inner/>
</outer>
''')
    doc = libxml2.parseDoc(input)
    for i in (0, 1):
        f = str_io()
        buf = libxml2.createOutputBuffer(f, 'UTF-8')
        doc.saveFormatFileTo(buf, 'UTF-8', i)
        if f.getvalue() != expected[i]:
            print('xmlDoc.saveFormatFileTo() call failed.')
            print('     got: %s' % repr(f.getvalue()))
            print('expected: %s' % repr(expected[i]))
            sys.exit(1)
    doc.freeDoc()
예제 #18
0
#!/usr/bin/python -u
import sys
import libxml2
import StringIO

print "Skipped"
sys.exit(1)

# Memory debug specific
libxml2.debugMemory(1)

#f = open('res', 'w')
f = StringIO.StringIO()
buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
buf.write(3, "foo")
buf.writeString("bar")
buf.close()
del buf

if f.getvalue() != "foobar":
    print "Failed to save to StringIO"
    sys.exit(1)

del f

# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
    print "OK"
else:
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
예제 #19
0
            else:
                status = "Validation status: This document (%s) is valid MEDIN metadata" % unique_id
                log(status)
        else:
            status = (
                "Validation status: This document (%s) has NOT been validated as conforming to the MEDIN Metadata Standard"
                % unique_id
            )
            log(status)
        # save the validation status in the document itself
        comment = doc.newDocComment(status)
        doc.getRootElement().addPrevSibling(comment)

        # output the document
        f = StringIO()
        buf = libxml2.createOutputBuffer(f, "utf-8")
        doc.saveFormatFileTo(buf, "utf-8", True)
        doc.freeDoc()  # clean up

        return (f.getvalue().strip(), warning)

    def _createFlush(self, xml, doc, unique_id):
        """
        Return a function that outputs a metadata entry
        """
        raise NotImplementedError("This method must be overridden")

    def __call__(self, iter_metadata, filter_=None):
        if filter_ is None:
            filter_ = FilterMetadata()
#!/usr/bin/python -u
import sys
import libxml2
import StringIO

print "Skipped"
sys.exit(1)

# Memory debug specific
libxml2.debugMemory(1)

# f = open('res', 'w')
f = StringIO.StringIO()
buf = libxml2.createOutputBuffer(f, "ISO-8859-1")
buf.write(3, "foo")
buf.writeString("bar")
buf.close()
del buf

if f.getvalue() != "foobar":
    print "Failed to save to StringIO"
    sys.exit(1)

del f

# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
    print "OK"
else:
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))