2 new revisions:

Revision: 97f9568032bc
Author:   Robot Framework Developers <[email protected]>
Date:     Tue Dec 20 04:08:34 2011
Log:      serializer: open files using codecs in UTF-8
http://code.google.com/p/robotframework/source/detail?r=97f9568032bc

Revision: 8797eb9455f5
Author:   Robot Framework Developers <[email protected]>
Date:     Tue Dec 20 04:09:25 2011
Log:      writer: escape newlines in text formats, cleanup
http://code.google.com/p/robotframework/source/detail?r=8797eb9455f5

==============================================================================
Revision: 97f9568032bc
Author:   Robot Framework Developers <[email protected]>
Date:     Tue Dec 20 04:08:34 2011
Log:      serializer: open files using codecs in UTF-8
http://code.google.com/p/robotframework/source/detail?r=97f9568032bc

Modified:
 /src/robot/writer/serializer.py

=======================================
--- /src/robot/writer/serializer.py     Wed Dec  7 23:07:59 2011
+++ /src/robot/writer/serializer.py     Tue Dec 20 04:08:34 2011
@@ -13,6 +13,7 @@
 #  limitations under the License.

 import os
+import codecs

 from .writer import FileWriter

@@ -70,7 +71,7 @@
     @property
     def output(self):
         if not self._output:
-            self._output = open(self._get_source(), 'wb')
+            self._output = codecs.open(self._get_source(), 'wb', 'UTF-8')
         return self._output

     @property

==============================================================================
Revision: 8797eb9455f5
Author:   Robot Framework Developers <[email protected]>
Date:     Tue Dec 20 04:09:25 2011
Log:      writer: escape newlines in text formats, cleanup
http://code.google.com/p/robotframework/source/detail?r=8797eb9455f5

Modified:
 /src/robot/writer/formatters.py
 /src/robot/writer/tableformatters.py
 /utest/writer/test_formatters.py

=======================================
--- /src/robot/writer/formatters.py     Wed Dec 14 23:28:07 2011
+++ /src/robot/writer/formatters.py     Tue Dec 20 04:09:25 2011
@@ -15,7 +15,7 @@
 import re

from .tableformatters import (RowSplittingFormatter, SplittingHtmlFormatter,
-    ColumnAligner, SettingTableAligner, NameCell, HeaderCell, Cell)
+    ColumnAligner, SettingTableAligner, NameCell, HeaderCell, HtmlCell)


 class _TestDataFileFormatter(object):
@@ -69,6 +69,7 @@
         return self._pad(row)

     def _pad(self, row):
+        row = [cell.replace('\n', ' ') for cell in row]
         return row + [self._padding] * (self._cols - len(row))


@@ -113,7 +114,8 @@
     def _escape(self, row):
         if len(row) >= 2 and row[0] == '' and row[1] == '':
             row[1] = '\\'
- return [re.sub('\s\s+(?=[^\s])', lambda match: '\\'.join(match.group(0)), item) for item in row]
+        return [re.sub('\s\s+(?=[^\s])',
+ lambda match: '\\'.join(match.group(0)), item.replace('\n', ' ')) for item in row]


 class PipeFormatter(TxtFormatter):
@@ -127,7 +129,7 @@
         self._formatter = SplittingHtmlFormatter('', self._cols)

     def empty_row(self):
-        return [NameCell('')] + [Cell('') for _ in range(self._cols-1)]
+        return [NameCell('')] + [HtmlCell('') for _ in range(self._cols-1)]

     def _setting_table_formatter(self):
         return self._formatter
=======================================
--- /src/robot/writer/tableformatters.py        Mon Dec 19 08:11:10 2011
+++ /src/robot/writer/tableformatters.py        Tue Dec 20 04:09:25 2011
@@ -147,7 +147,7 @@
         if isinstance(item, Documentation):
             return self._format_documentation(item, indent)
         rows = self._row_splitter.split(item.as_list(), indent)
- return [self._pad([NameCell(row[0])] + [Cell(c) for c in row[1:]]) for row in rows] + return [self._pad([NameCell(row[0])] + [HtmlCell(c) for c in row[1:]]) for row in rows]

     def _format_documentation(self, doc, indent):
         if indent:
@@ -155,7 +155,7 @@
             value = doc.as_list()[1:]
             if len(value) == 1:
return [start + [DocumentationCell(doc.value, self._cols-1-indent)]]
-            return [self._pad(start + [Cell(v) for v in value])]
+            return [self._pad(start + [HtmlCell(v) for v in value])]
         return [[NameCell(doc.setting_name),
                 DocumentationCell(doc.value, self._cols-1)]]

@@ -167,14 +167,16 @@
     def _pad(self, row, colspan=False, indent=0):
         if colspan:
             return row
- return row + [Cell(self._padding)] * (self._cols - len(row) - indent) + return row + [HtmlCell(self._padding)] * (self._cols - len(row) - indent)


 class HtmlCell(object):
     _backslash_matcher = re.compile(r'(\\+)n ')

-    def __init__(self, content='', attributes=None, tag='td'):
-        self.content = content
+    def __init__(self, content='', attributes=None, tag='td', escape=True):
+        if escape:
+            content = utils.html_escape(content)
+        self.content = self._replace_newlines(content)
         self.attributes = attributes or {}
         self.tag = tag

@@ -187,18 +189,10 @@
         return self._backslash_matcher.sub(replacer, content)


-class Cell(HtmlCell):
-
-    def __init__(self, content, attributes=None):
-        HtmlCell.__init__(self,
- self._replace_newlines(utils.html_escape(content)),
-                          attributes)
-
-
 class NameCell(HtmlCell):

     def __init__(self, name, attributes=None):
-        HtmlCell.__init__(self, self._replace_newlines(name), attributes)
+        HtmlCell.__init__(self, name, attributes)
         self.attributes.update({'class': 'name'})


@@ -206,13 +200,13 @@

     def __init__(self, name, type_):
         HtmlCell.__init__(self, self._link_from_name(name, type_),
-                          {'class': 'name'})
-
+                          {'class': 'name'}, escape=False)

     def _link_from_name(self, name, type_):
return '<a name="%s_%s">%s</a>' % (type_, utils.html_attr_escape(name),
                                            utils.html_escape(name))

+
 class DocumentationCell(HtmlCell):

     def __init__(self, content, span):
@@ -238,11 +232,7 @@

     def split(self, row, indent):
         self._in_comment = False
-        # TODO: encoding does not belong here
-        return [self._encode(r) for r in self._split_to_rows(row, indent)]
-
-    def _encode(self, row):
-        return [cell.encode('UTF-8').replace('\n', ' ') for cell in row]
+        return self._split_to_rows(row, indent)

     def _split_to_rows(self, data, indent=0):
         if not data:
=======================================
--- /utest/writer/test_formatters.py    Mon Dec 19 08:11:10 2011
+++ /utest/writer/test_formatters.py    Tue Dec 20 04:09:25 2011
@@ -1,8 +1,8 @@
 import unittest
 from robot.parsing.model import TestCaseTable, TestCaseFileSettingTable

-from robot.writer.formatters import TxtFormatter, Cell, HtmlFormatter
-from robot.writer.tableformatters import RowSplitter
+from robot.writer.formatters import TxtFormatter, HtmlFormatter, TsvFormatter
+from robot.writer.tableformatters import RowSplitter, HtmlCell
 from robot.utils.asserts import assert_equals


@@ -22,9 +22,17 @@

 class TestTxtFormatter(unittest.TestCase):

-   def test_escaping(self):
-        formatter = TxtFormatter()
-        assert_equals(formatter._escape(['so  me']), ['so \ me'])
+    def test_escaping_whitespace(self):
+        assert_equals(TxtFormatter()._escape(['so  me']), ['so \ me'])
+
+    def test_replacing_newlines(self):
+        assert_equals(TxtFormatter()._escape(['so\nme']), ['so me'])
+
+
+class TestTsvFormatter(unittest.TestCase):
+
+    def test_replacing_newlines(self):
+        assert_equals(TsvFormatter()._format_row(['so\nme'])[0], 'so me')


 class TestHtmlFormatter(unittest.TestCase):
@@ -81,15 +89,15 @@
         original = """This is real new line:
here we have a single backslash n: \\n and here backslash + newline: \\\n and here bslash blash n \\\\n and bslash x 3 n \\\\\\n """ expected = 'This is real new line:\n here we have a single backslash n: \\n<br>\nand here backslash + newline: \\\n and here bslash blash n \\\\n and bslash x 3 n \\\\\\n<br>\n'
-        assert_equals(Cell(original).content, expected)
+        assert_equals(HtmlCell(original).content, expected)

     def test_no_br_to_newlines_without_whitespace(self):
         original = r"Here there is no space after backslash-n: '\n'"
-        assert_equals(Cell(original).content, original)
+        assert_equals(HtmlCell(original).content, original)

     def test_no_br_to_double_backslashes(self):
         original = r"Here there is double backslash-n: \\n "
-        assert_equals(Cell(original).content, original)
+        assert_equals(HtmlCell(original).content, original)


 if __name__ == "__main__":

Reply via email to