[Telepathy-commits] [telepathy-gabble/master] gabbletest: backport elem() API from Gadget

Guillaume Desmottes guillaume.desmottes at collabora.co.uk
Fri Sep 26 10:02:28 PDT 2008


20080702094937-7fe3f-7dd04f15ca9a32e10ad5ac6996d2bb575888b50f.gz
---
 tests/twisted/gabbletest.py |   52 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/tests/twisted/gabbletest.py b/tests/twisted/gabbletest.py
index 05d0aaf..118789f 100644
--- a/tests/twisted/gabbletest.py
+++ b/tests/twisted/gabbletest.py
@@ -384,3 +384,55 @@ def handle_set_vcard(event, data):
     return True
 
 
+def _elem_add(elem, *children):
+    for child in children:
+        if isinstance(child, domish.Element):
+            elem.addChild(child)
+        elif isinstance(child, unicode):
+            elem.addContent(child)
+        else:
+            raise ValueError('invalid child object %r', child)
+
+def elem(a, b=None, **kw):
+    r"""
+    >>> elem('foo')().toXml()
+    u'<foo/>'
+    >>> elem('foo', x='1')().toXml()
+    u"<foo x='1'/>"
+    >>> elem('foo', x='1')(u'hello').toXml()
+    u"<foo x='1'>hello</foo>"
+    >>> elem('foo', x='1')(u'hello',
+    ...         elem('http://foo.org', 'bar', y='2')(u'bye')).toXml()
+    u"<foo x='1'>hello<bar xmlns='http://foo.org' y='2'>bye</bar></foo>"
+    """
+
+    class _elem(domish.Element):
+        def __call__(self, *children):
+            _elem_add(self, *children)
+            return self
+
+    if b is not None:
+        elem = _elem((a, b))
+    else:
+        elem = _elem((None, a))
+
+    for k, v in kw.iteritems():
+        elem[k] = v
+
+    return elem
+
+def elem_iq(server, type, **kw):
+    class _iq(IQ):
+        def __call__(self, *children):
+            _elem_add(self, *children)
+            return self
+
+    iq = _iq(server, type)
+
+    for k, v in kw.iteritems():
+        if k == 'from_':
+            iq['from'] = v
+        else:
+            iq[k] = v
+
+    return iq
-- 
1.5.6.5




More information about the Telepathy-commits mailing list