[PATCH] [utils] Add the possibility to add elements in the head of an UStringIO list

Guillaume Vandevelde guillaume.vandevelde at logilab.fr
Mon Feb 10 16:48:14 CET 2020


# HG changeset patch
# User Guillaume Vandevelde <gvandevelde at logilab.fr>
# Date 1581094414 -3600
#      Fri Feb 07 17:53:34 2020 +0100
# Node ID 9d62d9fed7859b47401b88ed27c959a65e190c10
# Parent  8fa3d2b0252d869ab00f17ea0a6f981d7878f0d2
[utils] Add the possibility to add elements in the head of an UStringIO list

diff -r 8fa3d2b0252d -r 9d62d9fed785 cubicweb/utils.py
--- a/cubicweb/utils.py	Fri Feb 07 17:09:59 2020 +0100
+++ b/cubicweb/utils.py	Fri Feb 07 17:53:34 2020 +0100
@@ -181,6 +181,19 @@
         self._size -= 1
 
 
+def handle_writing_constraints(method):
+    def wrapper(self, value):
+        if self.tracewrites:
+            from traceback import format_stack
+            stack = format_stack(None)[:-1]
+            escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
+            escaped_html = xml_escape(value).replace('\n', '<br/>\n')
+            tpl = u'<span onclick="alert(%s)">%s</span>'
+            value = tpl % (escaped_stack, escaped_html)
+        method(self, value)
+    return wrapper
+
+
 class UStringIO(list):
     """a file wrapper which automatically encode unicode string to an encoding
     specifed in the constructor
@@ -195,16 +208,14 @@
 
     __nonzero__ = __bool__
 
+    @handle_writing_constraints
     def write(self, value):
-        if self.tracewrites:
-            from traceback import format_stack
-            stack = format_stack(None)[:-1]
-            escaped_stack = xml_escape(json_dumps(u'\n'.join(stack)))
-            escaped_html = xml_escape(value).replace('\n', '<br/>\n')
-            tpl = u'<span onclick="alert(%s)">%s</span>'
-            value = tpl % (escaped_stack, escaped_html)
         self.append(value)
 
+    @handle_writing_constraints
+    def write_front(self, value):
+        self.insert(0, value)
+
     def getvalue(self):
         return u''.join(self)
 



More information about the cubicweb-devel mailing list