From 84bc5a67accdc92bb0899330fb1f54983883c2c1 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Wed, 31 Mar 2010 15:58:32 +0200 Subject: [PATCH] Allow accessing Blob contents as chunks. --- NEWS | 4 ++++ dulwich/objects.py | 31 +++++++++++++++++++++++++++++-- dulwich/tests/test_objects.py | 12 ++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 345bc85..473aa5b 100644 --- a/NEWS +++ b/NEWS @@ -29,6 +29,10 @@ * 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 diff --git a/dulwich/objects.py b/dulwich/objects.py index 73b2374..8e059d4 100644 --- a/dulwich/objects.py +++ b/dulwich/objects.py @@ -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) diff --git a/dulwich/tests/test_objects.py b/dulwich/tests/test_objects.py index 62f1c17..2d50195 100644 --- a/dulwich/tests/test_objects.py +++ b/dulwich/tests/test_objects.py @@ -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' -- 2.34.1