From elliot at rpath.com Tue Sep 7 13:01:26 2010
From: elliot at rpath.com (Elliot Peele)
Date: Tue, 07 Sep 2010 17:01:26 +0000
Subject: xobj: make sure simple collections with attributes continue to
work
Message-ID: <201009071701.o87H1QmJ000368@scc.eng.rpath.com>
changeset: 2f047aaf578e
user: Elliot Peele
date: Tue, 07 Sep 2010 13:01:23 -0400
make sure simple collections with attributes continue to work
diff --git a/py/test/xobjtest.py b/py/test/xobjtest.py
--- a/py/test/xobjtest.py
+++ b/py/test/xobjtest.py
@@ -907,6 +907,38 @@
self.failIfEqual(len(doc2.bar.j), len(Bar().j))
self.failUnlessEqual(doc2.bar.j, [Foo(3), Foo(4)])
+ def testCollectionWithAttrs(self):
+ class Foo(object):
+ bar = str
+ _xobj = xobj.XObjMetadata(attributes=('id', ))
+ class Foos(object):
+ foo = [ Foo, ]
+ _xobj = xobj.XObjMetadata(attributes=('id', ))
+ class Doc(xobj.Document):
+ foos = Foos
+
+ xml = """\
+
+
+
+ a
+
+
+ b
+
+
+"""
+
+ doc = xobj.parse(xml, documentClass=Doc)
+
+ self.failUnlessEqual(len(doc.foos.foo), 2)
+ self.failUnlessEqual(doc.foos.foo[0].bar, 'a')
+ self.failUnlessEqual(doc.foos.foo[1].bar, 'b')
+
+ xml2 = doc.toxml()
+
+ self.assertXMLEquals(xml, xml2)
+
if __name__ == "__main__":
testsuite.main()
From elliot at rpath.com Mon Sep 13 16:02:38 2010
From: elliot at rpath.com (Elliot Peele)
Date: Mon, 13 Sep 2010 20:02:38 +0000
Subject: xobj: Make sure tag values are consistently stored as part of the
xobj metadata
Message-ID: <201009132002.o8DK2cSm032521@scc.eng.rpath.com>
changeset: 28ee98fb3297
user: Elliot Peele
date: Mon, 13 Sep 2010 16:02:36 -0400
Make sure tag values are consistently stored as part of the xobj metadata
diff --git a/py/test/xobjtest.py b/py/test/xobjtest.py
--- a/py/test/xobjtest.py
+++ b/py/test/xobjtest.py
@@ -939,6 +939,33 @@
self.assertXMLEquals(xml, xml2)
+ def testSettingTag(self):
+ class Foo(object):
+ _xobj = xobj.XObjMetadata(tag='foo', attributes='href')
+ href = str
+ class Doc(xobj.Document):
+ foo = Foo
+
+ xml = """\
+
+
+"""
+
+ doc = xobj.parse(xml, documentClass=Doc)
+ self.failUnlessEqual(doc.foo._xobj.tag, 'foo')
+
+ doc2 = xobj.parse(xml)
+ self.failUnlessEqual(doc2.foo._xobj.tag, 'foo')
+
+ foo = Foo()
+ foo.href = 'http://example.com/api/'
+ xml2 = xobj.toxml(foo)
+
+ self.assertXMLEquals(xml, xml2)
+
+ doc.foo._xobj.tag = None
+ self.failUnlessRaises(TypeError, xobj.toxml, doc.foo)
+
if __name__ == "__main__":
testsuite.main()
diff --git a/py/xobj/xobj.py b/py/xobj/xobj.py
--- a/py/xobj/xobj.py
+++ b/py/xobj/xobj.py
@@ -495,6 +495,9 @@
if not hasattr(xobj, '_xobj'):
xobj._xobj = XObjMetadata()
+ if not xobj._xobj.tag:
+ xobj._xobj.tag = tag
+
initialized = set()
# handle children
@@ -615,13 +618,16 @@
s = StringIO(s)
return parsef(s, schemaf, documentClass = documentClass, typeMap = typeMap)
-def toxml(xobj, tag, prettyPrint = True, xml_declaration = True,
+def toxml(xobj, tag = None, prettyPrint = True, xml_declaration = True,
schemaf = None, nsmap = {}):
if schemaf:
schemaObj = etree.XMLSchema(file = schemaf)
else:
schemaObj = None
+ if tag is None and hasattr(xobj, '_xobj') and xobj._xobj.tag is None:
+ raise TypeError, 'must specify a tag'
+
gen = ElementGenerator(xobj, tag, schema = schemaObj, nsmap = nsmap)
return gen.tostring(prettyPrint = prettyPrint,