From jc at eclis.ch Wed Mar 4 11:01:24 2009
From: jc at eclis.ch (Jean-Christian de Rivaz)
Date: Wed, 04 Mar 2009 11:01:24 +0100
Subject: [PyXMPP] Trying to make a class XMLRPCHandler
Message-ID: <49AE5174.50902@eclis.ch>
Hello,
I try to add a XML-RPC handler (XEP-0009) to the "echobot.py" example.
So far I have this function and class (edited for brevity):
def test_string_get():
return ("foo")
class XMLRPCHandler(object):
implements(IIqHandlersProvider, IFeaturesProvider)
def __init__(self, client):
self.client = client
def get_features(self):
return ["jabber:iq:rpc"]
def get_iq_get_handlers(self):
return []
def get_iq_set_handlers(self):
return [("query", "jabber:iq:rpc", self.set_rpc),]
def set_rpc(self,iq):
query = iq.get_query().children
stanza = query.serialize()
print stanza
call = xmlrpclib.loads(stanza)
print call[1], call[0]
result = locals()[call[1]](*call[0])
print result
stanza = "" + xmlrpclib.dumps((result,)) +
""
print stanza
iq=iq.make_result_response()
q=iq.new_query("jabber:iq:rpc")
# FIXME: Need to add the methodResponse XML tree.
# How to do that ?
return iq
The class has been added into the interface_providers list of the class
Client. Test show that the XMLRPCHandler.set_rpc() is called as expected.
The problem I have is that the xmlrpclib need a string, not a XML tree.
Using the serialize() should do the conversion, but the result have the
"default1:" in every nodes that make it incompatible with the xmlrpclib.
Here is an example fo the serialization result:
test_string_get
From where cam the "default1:" and how to disable it ?
Regards,
--
Jean-Christian de Rivaz
From jajcus at jajcus.net Wed Mar 4 18:38:36 2009
From: jajcus at jajcus.net (Jacek Konieczny)
Date: Wed, 4 Mar 2009 18:38:36 +0100
Subject: [PyXMPP] Trying to make a class XMLRPCHandler
In-Reply-To: <49AE5174.50902@eclis.ch>
References: <49AE5174.50902@eclis.ch>
Message-ID: <20090304173836.GD6146@lolek.nigdzie>
On Wed, Mar 04, 2009 at 11:01:24AM +0100, Jean-Christian de Rivaz wrote:
> The problem I have is that the xmlrpclib need a string, not a XML tree.
> Using the serialize() should do the conversion, but the result have the
> "default1:" in every nodes that make it incompatible with the xmlrpclib.
>
> Here is an example fo the serialization result:
>
>
> test_string_get
>
>
>
>
> From where cam the "default1:"
It is the default prefix that libxml2 uses for a namespace set for this
object. Namespace declaration is not visible, as it is at the outer
element ().
> and how to disable it ?
Try using pyxmpp.xmlextra.replace_ns()
Greets,
Jacek
From jc at eclis.ch Wed Mar 4 20:49:42 2009
From: jc at eclis.ch (Jean-Christian de Rivaz)
Date: Wed, 04 Mar 2009 20:49:42 +0100
Subject: [PyXMPP] Trying to make a class XMLRPCHandler
In-Reply-To: <20090304173836.GD6146@lolek.nigdzie>
References: <49AE5174.50902@eclis.ch> <20090304173836.GD6146@lolek.nigdzie>
Message-ID: <49AEDB56.4000303@eclis.ch>
Jacek Konieczny a ?crit :
> On Wed, Mar 04, 2009 at 11:01:24AM +0100, Jean-Christian de Rivaz wrote:
>> The problem I have is that the xmlrpclib need a string, not a XML tree.
>> Using the serialize() should do the conversion, but the result have the
>> "default1:" in every nodes that make it incompatible with the xmlrpclib.
>>
>> Here is an example fo the serialization result:
>>
>>
>> test_string_get
>>
>>
>>
>>
>> From where cam the "default1:"
>
> It is the default prefix that libxml2 uses for a namespace set for this
> object. Namespace declaration is not visible, as it is at the outer
> element ().
I am not certain to understand. If I try this simple code below, I don't
get a "default1:" in any node:
Code:
-----------------
#!/usr/bin/python
import libxml2
text_in = ""
print text_in, type(text_in)
tree = libxml2.parseDoc(text_in).children
print tree, type(tree)
text_out = tree.serialize()
print text_out, type(text_out)
-----------------
Result:
Where is the difference with the jabber.iq.rpc child node parsing and
serialization ?
I an other point of view: is there a way to simply get the original
string of the jabber.iq.rpc stanza without doing the parsing (required
for pyxmpp) and a serialization (because I don't know a other way to
pass the request to xmlrpclib).
>> and how to disable it ?
>
> Try using pyxmpp.xmlextra.replace_ns()
Great, PyXMPP have nice resources!
Now a have a working XEP-0009 with this code:
def set_rpc(self,iq):
print iq.serialize()
query = iq.get_query().children
pyxmpp.xmlextra.replace_ns(query, query.ns(),
pyxmpp.xmlextra.common_ns)
stanza = query.serialize()
print stanza
call = xmlrpclib.loads(stanza)
result = globals()[call[1]](*call[0])
stanza = "" + xmlrpclib.dumps((result,)) +
""
print stanza
tree = libxml2.parseDoc(stanza).children
iq=iq.make_result_response()
q=iq.new_query("jabber:iq:rpc")
q.addChild(tree)
return iq
Thanks for your help!
--
Jean-Christian de Rivaz