Selaa lähdekoodia

Add MemoryObjectStore.

Jelmer Vernooij 16 vuotta sitten
vanhempi
commit
f280b90eb1
4 muutettua tiedostoa jossa 55 lisäystä ja 10 poistoa
  1. 1 1
      dulwich/index.py
  2. 36 1
      dulwich/object_store.py
  3. 2 2
      dulwich/repo.py
  4. 16 6
      dulwich/tests/test_object_store.py

+ 1 - 1
dulwich/index.py

@@ -126,7 +126,7 @@ def write_index_dict(f, entries):
 
 
 def cleanup_mode(mode):
-    if stat.S_ISLNK(fsmode)
+    if stat.S_ISLNK(fsmode):
         mode = stat.S_IFLNK
     else:
         mode = stat.S_IFREG

+ 36 - 1
dulwich/object_store.py

@@ -112,7 +112,7 @@ class BaseObjectStore(object):
         return iter(MissingObjectFinder(self, wants, graph_walker, progress).next, None)
 
 
-class ObjectStore(BaseObjectStore):
+class DiskObjectStore(BaseObjectStore):
     """Git-style object store that exists on disk."""
 
     def __init__(self, path):
@@ -300,6 +300,41 @@ class ObjectStore(BaseObjectStore):
         commit()
 
 
+class MemoryObjectStore(BaseObjectStore):
+
+    def __init__(self):
+        super(MemoryObjectStore, self).__init__()
+        self._data = {}
+
+    def __contains__(self, sha):
+        return sha in self._data
+
+    def __iter__(self):
+        """Iterate over the SHAs that are present in this store."""
+        return self._data.iterkeys()
+
+    def get_raw(self, name):
+        """Obtain the raw text for an object.
+        
+        :param name: sha for the object.
+        :return: tuple with object type and object contents.
+        """
+        return self[sha].as_raw_string()
+
+    def add_object(self, obj):
+        """Add a single object to this object store.
+
+        """
+        self._dict[obj.id] = obj
+
+    def add_objects(self, objects):
+        """Add a set of objects to this object store.
+
+        :param objects: Iterable over a list of objects.
+        """
+        for obj in objects:
+            self._data[obj.id] = obj
+
 
 class ObjectImporter(object):
     """Interface for importing objects."""

+ 2 - 2
dulwich/repo.py

@@ -31,7 +31,7 @@ from dulwich.errors import (
     NotTreeError, 
     )
 from dulwich.object_store import (
-    ObjectStore,
+    DiskObjectStore,
     )
 from dulwich.objects import (
     Blob,
@@ -163,7 +163,7 @@ class Repo(object):
     @property
     def object_store(self):
         if self._object_store is None:
-            self._object_store = ObjectStore(self.object_dir())
+            self._object_store = DiskObjectStore(self.object_dir())
         return self._object_store
 
     def pack_dir(self):

+ 16 - 6
dulwich/tests/test_object_store.py

@@ -18,24 +18,34 @@
 
 from unittest import TestCase
 
-from dulwich.object_store import ObjectStore
+from dulwich.object_store import (
+    DiskObjectStore,
+    MemoryObjectStore,
+    )
 
-class ObjectStoreTests(TestCase):
+class DiskObjectStoreTests(TestCase):
 
     def test_pack_dir(self):
-        o = ObjectStore("foo")
+        o = DiskObjectStore("foo")
         self.assertEquals("foo/pack", o.pack_dir)
 
     def test_empty_packs(self):
-        o = ObjectStore("foo")
+        o = DiskObjectStore("foo")
         self.assertEquals([], o.packs)
 
     def test_add_objects_empty(self):
-        o = ObjectStore("foo")
+        o = DiskObjectStore("foo")
         o.add_objects([])
 
     def test_add_commit(self):
-        o = ObjectStore("foo")
+        o = DiskObjectStore("foo")
         # TODO: Argh, no way to construct Git commit objects without 
         # access to a serialized form.
         o.add_objects([])
+
+
+class MemoryObjectStoreTests(TestCase):
+
+    def test_iter(self):
+        store = MemoryObjectStore()
+        self.assertEquals([], list(store))