Make it possible to serialize as unicode in SEDA export adapter
authorDenis Laxalde <denis.laxalde@logilab.fr>
Wed, 02 Oct 2019 15:46:00 +0200
changeset 3047 63e2c0ef1a8e
parent 3046 bf353c190352
child 3048 91110717a53b
Make it possible to serialize as unicode in SEDA export adapter This is useful in tests because the doctest comparison engine from lxml works better with text objects. The new _encoding argument is essentially used a "encoding" argument of lxml's etree.tostring() which will hence serialize to a unicode string. When doing so, we must also avoid any XML declaration (otherwise, lxml messes up when loading the content, not sure why), so we insert the "standalone" flag only when dumping to bytes.
cubicweb_seda/entities/profile_generation.py
--- a/cubicweb_seda/entities/profile_generation.py	Wed Oct 02 15:17:35 2019 +0200
+++ b/cubicweb_seda/entities/profile_generation.py	Wed Oct 02 15:46:00 2019 +0200
@@ -278,10 +278,19 @@
         attributes.update(self._root_attributes)
         return attributes
 
-    def dump(self):
-        """Return an schema string for the adapted SEDA profile."""
+    def dump(self, _encoding=None):
+        """Return an schema string for the adapted SEDA profile
+
+        _encoding will be used as "encoding" argument of lxml's tostring, in
+        order to retrieve a unicode string. This is useful for tests.
+        """
         root = self.dump_etree()
-        return etree.tostring(root, encoding=self.encoding, pretty_print=True, standalone=False)
+        kwargs = {}
+        if _encoding is None:
+            # We only want the XML declaration at all if _encoding is not specified.
+            kwargs['standalone'] = False
+        kwargs['encoding'] = _encoding or self.encoding
+        return etree.tostring(root, pretty_print=True, **kwargs)
 
     def dump_etree(self):
         """Return an XSD etree for the adapted SEDA profile."""