[Twisted-Python] copy domish element

Jack Bates jack.bates at gmail.com
Tue Jun 9 11:55:09 EDT 2009


On Tue, 2009-06-09 at 08:47 +0200, Ralph Meijer wrote:
> On Mon, 2009-06-08 at 18:03 +0100, Phil Mayers wrote:
> > Jack Bates wrote:
> > > i have a domish element - when i try the following,
> > > 
> > >>>> message = element
> > >>>> del message['from']
> > >>>> element['from']
> > > 
> > > - i get a key error
> > 
> > Well, yes. That's how python works ;o)
> >  
> > > to avoid this key error, i think i want to copy the element, so i can
> > > drop the 'from' attribute from one copy without affecting the other?
> > > 
> > > but i didn't find anything to make a copy in the domish element api
> > 
> > I haven't tried it, but have you tried the python standard copy protocol:
> > 
> > import copy
> > message = copy.deepcopy(element)
> 
> Deep copies of Element instances is probably not what you want here. See
> issue #724.
> 
> I suspect from your example that you want to respond to an incoming XML
> Stanza. In general, it is better to construct a new stanza from the old
> one, keeping the addressing but in reverse. There is a utility function
> for this: twisted.words.protocols.jabber.xmlstream.toResponse. For error
> responses, you want to look at t.w.p.j.error.StanzaError and its
> toResponse method.

much thanks for your attention ralphm! actually i want to reroute an
incoming xml <message>

i have one script, qubot, which sits in our project chat room,
http://github.com/jablko/qubot/blob/722b28f66d5eaf96d3b00902f3532ba61588c4c9/qubot

- and one script, jabber, which sends commit messages to qubot,
http://github.com/jablko/jabber/blob/92a418a4c6d1d2a21beda02ff7d9edce18146e18/jabber

when qubot receives a message from jabber, it should relay the message
to the chat room

i used to construct a new <message> and copy a whitelist of attributes
and content from the incoming <message>, however i switched to
attempting a perfect copy of the incoming <message> and adjusting a
blacklist of attributes (such as del message['from']) because otherwise
xmpp extensions like xhtml-im were not relayed,
http://github.com/jablko/qubot/commit/722b28f66d5eaf96d3b00902f3532ba61588c4c9




More information about the Twisted-Python mailing list