Rev 3894: Add a new function that can convert 'chunks' format to a 'lines' format. in http://bzr.arbash-meinel.com/branches/bzr/1.11/get_record_stream_chunked

John Arbash Meinel john at arbash-meinel.com
Thu Dec 11 01:11:03 GMT 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.11/get_record_stream_chunked

------------------------------------------------------------
revno: 3894
revision-id: john at arbash-meinel.com-20081211011038-osioaxd7moquxxmy
parent: john at arbash-meinel.com-20081211010104-3tcii2strejk5252
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: get_record_stream_chunked
timestamp: Wed 2008-12-10 19:10:38 -0600
message:
  Add a new function that can convert 'chunks' format to a 'lines' format.
-------------- next part --------------
=== modified file 'bzrlib/osutils.py'
--- a/bzrlib/osutils.py	2008-10-17 03:49:08 +0000
+++ b/bzrlib/osutils.py	2008-12-11 01:10:38 +0000
@@ -812,6 +812,7 @@
             rps.append(f)
     return rps
 
+
 def joinpath(p):
     for f in p:
         if (f == '..') or (f is None) or (f == ''):
@@ -819,6 +820,18 @@
     return pathjoin(*p)
 
 
+def chunked_to_lines(chunks):
+    """Ensure that chunks is split cleanly into lines.
+
+    Each entry in the result should contain a single newline at the end. Except
+    for the last entry which may not have a final newline.
+
+    :param chunks: An iterable of strings
+    :return: A list of strings.
+    """
+    return split_lines(''.join(chunks))
+
+
 def split_lines(s):
     """Split s into lines, but without removing the newline characters."""
     lines = s.split('\n')

=== modified file 'bzrlib/tests/test_osutils.py'
--- a/bzrlib/tests/test_osutils.py	2008-10-01 07:56:03 +0000
+++ b/bzrlib/tests/test_osutils.py	2008-12-11 01:10:38 +0000
@@ -756,6 +756,20 @@
         self.assertEndsWith(osutils._mac_getcwd(), u'B\xe5gfors')
 
 
+class TestChunkedToLines(TestCase):
+
+    def assertChunksToLines(self, lines, chunks):
+        self.assertEqual(lines, osutils.chunked_to_lines(chunks))
+
+    def test_fulltext_chunk_to_lines(self):
+        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
+                                 ['foo\nbar\r\nba\rz\n'])
+
+    def test_lines_to_lines(self):
+        self.assertChunksToLines(['foo\n', 'bar\r\n', 'ba\rz\n'],
+                                 ['foo\n', 'bar\r\n', 'ba\rz\n'])
+
+
 class TestSplitLines(TestCase):
 
     def test_split_unicode(self):



More information about the bazaar-commits mailing list