31 from operator import itemgetter |
31 from operator import itemgetter |
32 if PY3: |
32 if PY3: |
33 from inspect import getfullargspec as getargspec |
33 from inspect import getfullargspec as getargspec |
34 else: |
34 else: |
35 from inspect import getargspec |
35 from inspect import getargspec |
|
36 from functools import wraps |
36 from itertools import repeat |
37 from itertools import repeat |
37 from uuid import uuid4 |
38 from uuid import uuid4 |
38 from warnings import warn |
39 from warnings import warn |
39 from threading import Lock |
40 from threading import Lock |
40 from logging import getLogger |
41 from logging import getLogger |
189 raise NotImplementedError |
190 raise NotImplementedError |
190 def pop(self, i): |
191 def pop(self, i): |
191 self._size -= 1 |
192 self._size -= 1 |
192 |
193 |
193 |
194 |
194 class UStringIO(list): |
195 def handle_writing_constraints(method): |
195 """a file wrapper which automatically encode unicode string to an encoding |
196 @wraps(method) |
196 specifed in the constructor |
197 def wrapper(self, value): |
197 """ |
|
198 |
|
199 def __init__(self, tracewrites=False, *args, **kwargs): |
|
200 self.tracewrites = tracewrites |
|
201 super(UStringIO, self).__init__(*args, **kwargs) |
|
202 |
|
203 def __bool__(self): |
|
204 return True |
|
205 |
|
206 __nonzero__ = __bool__ |
|
207 |
|
208 def write(self, value): |
|
209 assert isinstance(value, text_type), u"unicode required not %s : %s"\ |
198 assert isinstance(value, text_type), u"unicode required not %s : %s"\ |
210 % (type(value).__name__, repr(value)) |
199 % (type(value).__name__, repr(value)) |
211 if self.tracewrites: |
200 if self.tracewrites: |
212 from traceback import format_stack |
201 from traceback import format_stack |
213 stack = format_stack(None)[:-1] |
202 stack = format_stack(None)[:-1] |
214 escaped_stack = xml_escape(json_dumps(u'\n'.join(stack))) |
203 escaped_stack = xml_escape(json_dumps(u'\n'.join(stack))) |
215 escaped_html = xml_escape(value).replace('\n', '<br/>\n') |
204 escaped_html = xml_escape(value).replace('\n', '<br/>\n') |
216 tpl = u'<span onclick="alert(%s)">%s</span>' |
205 tpl = u'<span onclick="alert(%s)">%s</span>' |
217 value = tpl % (escaped_stack, escaped_html) |
206 value = tpl % (escaped_stack, escaped_html) |
|
207 return method(self, value) |
|
208 return wrapper |
|
209 |
|
210 |
|
211 class UStringIO(list): |
|
212 """a file wrapper which automatically encode unicode string to an encoding |
|
213 specifed in the constructor |
|
214 """ |
|
215 |
|
216 def __init__(self, tracewrites=False, *args, **kwargs): |
|
217 self.tracewrites = tracewrites |
|
218 super(UStringIO, self).__init__(*args, **kwargs) |
|
219 |
|
220 def __bool__(self): |
|
221 return True |
|
222 |
|
223 __nonzero__ = __bool__ |
|
224 |
|
225 @handle_writing_constraints |
|
226 def write(self, value): |
218 self.append(value) |
227 self.append(value) |
|
228 |
|
229 @handle_writing_constraints |
|
230 def write_front(self, value): |
|
231 self.insert(0, value) |
219 |
232 |
220 def getvalue(self): |
233 def getvalue(self): |
221 return u''.join(self) |
234 return u''.join(self) |
222 |
235 |
223 def __repr__(self): |
236 def __repr__(self): |