Allow accessing Blob contents as chunks.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 31 Mar 2010 13:58:32 +0000 (15:58 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 31 Mar 2010 13:58:32 +0000 (15:58 +0200)
NEWS
dulwich/objects.py
dulwich/tests/test_objects.py

diff --git a/NEWS b/NEWS
index 345bc85db61891a4041a8d57d956ead1b0e02b2b..473aa5b2bd4515561d113700e41518bb72ee82c7 100644 (file)
--- a/NEWS
+++ b/NEWS
   * Repo.get_blob, Repo.commit, Repo.tag and Repo.tree are now deprecated.
     (Jelmer Vernooij)
 
+ API CHANGES
+
+  * Blob.chunked was added. (Jelmer Vernooij)
+
 0.5.0  2010-03-03
 
  BUG FIXES
index 73b2374c7fb01927a5cd437f7f22a967eedb960a..8e059d4e2eb4264b0519925834e9add6755d02d3 100644 (file)
@@ -260,18 +260,45 @@ class Blob(ShaFile):
 
     _type = BLOB_ID
     _num_type = 3
-    _needs_serialization = False
-    _needs_parsing = False
+
+    def __init__(self):
+        super(Blob, self).__init__()
+        self._chunked = []
+        self._text = ""
+        self._needs_parsing = False
+        self._needs_serialization = False
 
     def get_data(self):
+        if self._needs_serialization:
+            self.serialize()
         return self._text
 
     def set_data(self, data):
         self._text = data
+        self._needs_parsing = True
+        self._needs_serialization = False
 
     data = property(get_data, set_data,
             "The text contained within the blob object.")
 
+    def get_chunked(self):
+        if self._needs_parsing:
+            self._parse_text()
+        return self._chunked
+
+    def set_chunked(self, chunks):
+        self._chunked = chunks
+        self._needs_serialization = True
+
+    chunked = property(get_chunked, set_chunked,
+        "The text within the blob object, as chunks (not necessarily lines).")
+
+    def _parse_text(self):
+        self._chunked = [self._text]
+
+    def serialize(self):
+        self._text = "".join(self._chunked)
+
     @classmethod
     def from_file(cls, filename):
         blob = ShaFile.from_file(filename)
index 62f1c173ce6420e2558ee80de5922bfc0ec5b4e1..2d50195c984e938a7e847ec87ca18221e49e6497 100644 (file)
@@ -82,6 +82,18 @@ class BlobReadTests(unittest.TestCase):
         b = Blob.from_string(string)
         self.assertEqual(b.data, string)
         self.assertEqual(b.sha().hexdigest(), b_sha)
+
+    def test_chunks(self):
+        string = 'test 5\n'
+        b = Blob.from_string(string)
+        self.assertEqual([string], b.chunked)
+
+    def test_set_chunks(self):
+        b = Blob()
+        b.chunked = ['te', 'st', ' 5\n']
+        self.assertEqual('test 5\n', b.data)
+        b.chunked = ['te', 'st', ' 6\n']
+        self.assertEqual('test 6\n', b.as_raw_string())
   
     def test_parse_legacy_blob(self):
         string = 'test 3\n'