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

Nicola Spanti nicola.spanti at logilab.fr
Tue Feb 11 11:21:14 CET 2020


Hello.

Like this, it seems dark to me. Could you explain in the commit message 
the usefulness at the end and add a unit test for this new case?

Regards.

Le 10/02/2020 à 16:48, Guillaume Vandevelde a écrit :
> # 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