List Info

Thread: python wrapper for xslt document API: context problem; __cmp__ needed?




python wrapper for xslt document API: context problem; __cmp__ needed?
user name
2006-09-06 12:06:29
My reason for building the python wrapper for the xslt
document API is
so that a single python document handling function can deal
behave
differently for different stylesheet applications. For
example, it
might serve import files from user specific directories.

The doc loader API defines a context to be passed to the
document
loader, which is said to be the contextual stylesheet. So
when the
document handler function is called it has a reference to
the
stylesheet that has caused the handler function to be
called.


So I have code like this in my C wrapper for the python doc
loader
function:

    if (pythonDocLoaderObject != NULL) {
        xsltStylesheetPtr style;
        PyObject *ctxtobj;
        PyObject *pctxtobj;
        PyObject *result;

        style = (xsltStylesheetPtr) ctxt;
        ctxtobj = libxslt_xsltStylesheetPtrWrap(ctxt);
        pctxtobj = libxml_xmlParserCtxtPtrWrap(pctxt);

        // For now just send parserContext and URL
        result =
PyObject_CallFunction(pythonDocLoaderObject, 
                                       (char *)
"(sOO)", URI, pctxtobj, ctxtobj);
	Py_XDECREF(pctxtobj);


And the python looks like this:

def fn(url, pctx, ctx):
    try:
        # print url
        style = libxslt.stylesheet(_obj=ctx)
        print "inside the function: %s" %
(style)
        pctxt = libxml2.parserCtxt(_obj=pctx)
        doc =
pctxt.ctxtReadDoc("""<?xml
version='1.0'?><b>Goodbye</b>""
", url, "UTF-8", 2)
        # doc.dump(sys.stdout)
        return doc
    except Exception, e:
        print >>sys.stderr, "something went
wrong: ", e
        return None


def run():
    libxslt.setLoaderFunc(fn)
    styledoc = libxml2.parseDoc("""
    <xsl:stylesheet version='1.0'
      xmlnssl='
http://www.w3.or
g/1999/XSL/Transform'>
      <xsl:template match='/'>
        <x><xsl:value-of
select='document("b.xml")'/></x>
      </xsl:template>
    </xsl:stylesheet>
    """)
    style = libxslt.parseStylesheetDoc(styledoc)
    print "outside the function: %s" % (style)
    doc = libxml2.parseDoc("<a/>")
    result = style.applyStylesheet(doc, {})
    result.dump(sys.stdout)
    style.freeStylesheet()
    doc.freeDoc()


The trouble is the xslt is wrapped by the python and the
python
objects are NOT the same (obvious really, since they're
being
wrapped).

So I think I probably need the stylesheet to implement the
Python
comparator interface. 

A naive implementation would be in C and would test that the
libxslt
stylesheet struct was the same instance. That would do for
me.


Does anyone have any thoughts on this? Is there a simpler
way to
achieve what I want?

-- 
Nic Ferrier
http://www.tapsellfer
rier.co.uk   for all your tapsell ferrier needs
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
python wrapper for xslt document API: context problem; __cmp__ needed?
user name
2006-09-06 15:08:20
Nic James Ferrier <nferriertapsellferrier.co.uk>
writes:

> So I think I probably need the stylesheet to implement
the Python
> comparator interface. 
>
> A naive implementation would be in C and would test
that the libxslt
> stylesheet struct was the same instance. That would do
for me.

I've been looking at this more. Here's the steps (I think)

- add a libxslt_xsltCompare(PyObject *a, PyObject *b)

- add python API for the above (alter
libxslt-python-api.xml)

- add a new class in libxsl.py: xsltCore with a __cmp__ or
an __eq__
  that calls libxslt_xsltCompare

- change generator.py's classes_ancestor map to include
stylesheet and
  transformCtxt as xsltCore objects


I'm still unclear as to how libxslt_xsltCompare might work
generically
for stylesheet and transformCtxt objects. It might be that I
need 2
different parent classes just to perform this trick.


Someone (Daniel?) verify this isn't mad?

-- 
Nic Ferrier
http://www.tapsellfer
rier.co.uk   for all your tapsell ferrier needs
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xmlgnome.org
http://mai
l.gnome.org/mailman/listinfo/xml
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )