diff options
author | Julien Cristau <julien.cristau@logilab.fr> | 2014-11-28 14:47:13 +0100 |
---|---|---|
committer | Julien Cristau <julien.cristau@logilab.fr> | 2014-11-28 14:47:13 +0100 |
commit | d06bab438fd83ca1cd2326aeeba4729144765a2b (patch) | |
tree | 621346f26362db20c89242802f65ac65704d0859 | |
parent | e8a83ac75b91fd8bf770caff17b9fe96635abd08 (diff) | |
download | logilab-common-d06bab438fd83ca1cd2326aeeba4729144765a2b.tar.gz |
[ureports] write unicode not bytes (closes #277372)
-rw-r--r-- | test/utils.py | 37 | ||||
-rw-r--r-- | ureports/__init__.py | 12 | ||||
-rw-r--r-- | ureports/text_writer.py | 30 |
3 files changed, 44 insertions, 35 deletions
diff --git a/test/utils.py b/test/utils.py index da315e4..ca1730e 100644 --- a/test/utils.py +++ b/test/utils.py @@ -20,24 +20,33 @@ from __future__ import print_function -from logilab.common.compat import StringIO +import sys +from io import StringIO +buffers = [StringIO] +if sys.version_info < (3, 0): + from cStringIO import StringIO as cStringIO + from StringIO import StringIO as pStringIO + buffers += [cStringIO, pStringIO] + from logilab.common.ureports.nodes import * class WriterTC: def _test_output(self, test_id, layout, msg=None): - buffer = StringIO() - self.writer.format(layout, buffer) - got = buffer.getvalue() - expected = getattr(self, test_id) - try: - self.assertMultiLineEqual(got, expected) - except: - print('**** got for %s' % test_id) - print(got) - print('**** while expected') - print(expected) - print('****') - raise + for buffercls in buffers: + buffer = buffercls() + self.writer.format(layout, buffer) + got = buffer.getvalue() + expected = getattr(self, test_id) + try: + self.assertMultiLineEqual(got, expected) + except: + print('**** using a %s' % buffer.__class__) + print('**** got for %s' % test_id) + print(got) + print('**** while expected') + print(expected) + print('****') + raise def test_section(self): layout = Section('Section title', diff --git a/ureports/__init__.py b/ureports/__init__.py index 65e9641..d76ebe5 100644 --- a/ureports/__init__.py +++ b/ureports/__init__.py @@ -42,13 +42,13 @@ def layout_title(layout): """ for child in layout.children: if isinstance(child, Title): - return ' '.join([node.data for node in get_nodes(child, Text)]) + return u' '.join([node.data for node in get_nodes(child, Text)]) def build_summary(layout, level=1): """make a summary for the report, including X level""" assert level > 0 level -= 1 - summary = List(klass='summary') + summary = List(klass=u'summary') for child in layout.children: if not isinstance(child, Section): continue @@ -57,7 +57,7 @@ def build_summary(layout, level=1): continue if not child.id: child.id = label.replace(' ', '-') - node = Link('#'+child.id, label=label or child.id) + node = Link(u'#'+child.id, label=label or child.id) # FIXME: Three following lines produce not very compliant # docbook: there are some useless <para><para>. They might be # replaced by the three commented lines but this then produces @@ -99,7 +99,7 @@ class BaseWriter(object): for child in getattr(layout, 'children', ()): child.accept(self) - def writeln(self, string=''): + def writeln(self, string=u''): """write a line in the output buffer""" self.write(string + linesep) @@ -132,7 +132,7 @@ class BaseWriter(object): result[-1].append(cell) # fill missing cells while len(result[-1]) < cols: - result[-1].append('') + result[-1].append(u'') return result def compute_content(self, layout): @@ -147,7 +147,7 @@ class BaseWriter(object): stream.write(data) except UnicodeEncodeError: stream.write(data.encode(self.encoding)) - def writeln(data=''): + def writeln(data=u''): try: stream.write(data+linesep) except UnicodeEncodeError: diff --git a/ureports/text_writer.py b/ureports/text_writer.py index a25cf26..c87613c 100644 --- a/ureports/text_writer.py +++ b/ureports/text_writer.py @@ -27,8 +27,8 @@ from logilab.common.textutils import linesep from logilab.common.ureports import BaseWriter -TITLE_UNDERLINES = ['', '=', '-', '`', '.', '~', '^'] -BULLETS = ['*', '-'] +TITLE_UNDERLINES = [u'', u'=', u'-', u'`', u'.', u'~', u'^'] +BULLETS = [u'*', u'-'] class TextWriter(BaseWriter): """format layouts as text @@ -48,13 +48,13 @@ class TextWriter(BaseWriter): if self.pending_urls: self.writeln() for label, url in self.pending_urls: - self.writeln('.. _`%s`: %s' % (label, url)) + self.writeln(u'.. _`%s`: %s' % (label, url)) self.pending_urls = [] self.section -= 1 self.writeln() def visit_title(self, layout): - title = ''.join(list(self.compute_content(layout))) + title = u''.join(list(self.compute_content(layout))) self.writeln(title) try: self.writeln(TITLE_UNDERLINES[self.section] * len(title)) @@ -88,19 +88,19 @@ class TextWriter(BaseWriter): def default_table(self, layout, table_content, cols_width): """format a table""" cols_width = [size+1 for size in cols_width] - format_strings = ' '.join(['%%-%ss'] * len(cols_width)) + format_strings = u' '.join([u'%%-%ss'] * len(cols_width)) format_strings = format_strings % tuple(cols_width) format_strings = format_strings.split(' ') - table_linesep = '\n+' + '+'.join(['-'*w for w in cols_width]) + '+\n' - headsep = '\n+' + '+'.join(['='*w for w in cols_width]) + '+\n' + table_linesep = u'\n+' + u'+'.join([u'-'*w for w in cols_width]) + u'+\n' + headsep = u'\n+' + u'+'.join([u'='*w for w in cols_width]) + u'+\n' # FIXME: layout.cheaders self.write(table_linesep) for i in range(len(table_content)): - self.write('|') + self.write(u'|') line = table_content[i] for j in range(len(line)): self.write(format_strings[j] % line[j]) - self.write('|') + self.write(u'|') if i == 0 and layout.rheaders: self.write(headsep) else: @@ -109,7 +109,7 @@ class TextWriter(BaseWriter): def field_table(self, layout, table_content, cols_width): """special case for field table""" assert layout.cols == 2 - format_string = '%s%%-%ss: %%s' % (linesep, cols_width[0]) + format_string = u'%s%%-%ss: %%s' % (linesep, cols_width[0]) for field, value in table_content: self.write(format_string % (field, value)) @@ -120,14 +120,14 @@ class TextWriter(BaseWriter): indent = ' ' * self.list_level self.list_level += 1 for child in layout.children: - self.write('%s%s%s ' % (linesep, indent, bullet)) + self.write(u'%s%s%s ' % (linesep, indent, bullet)) child.accept(self) self.list_level -= 1 def visit_link(self, layout): """add a hyperlink""" if layout.label != layout.url: - self.write('`%s`_' % layout.label) + self.write(u'`%s`_' % layout.label) self.pending_urls.append( (layout.label, layout.url) ) else: self.write(layout.url) @@ -135,11 +135,11 @@ class TextWriter(BaseWriter): def visit_verbatimtext(self, layout): """display a verbatim layout as text (so difficult ;) """ - self.writeln('::\n') + self.writeln(u'::\n') for line in layout.data.splitlines(): - self.writeln(' ' + line) + self.writeln(u' ' + line) self.writeln() def visit_text(self, layout): """add some text""" - self.write(layout.data) + self.write(u'%s' % layout.data) |