Distinguish between ShaFile.from_file and ShaFile.from_path.
authorJelmer Vernooij <jelmer@samba.org>
Fri, 30 Apr 2010 12:52:51 +0000 (14:52 +0200)
committerJelmer Vernooij <jelmer@samba.org>
Fri, 30 Apr 2010 12:52:51 +0000 (14:52 +0200)
dulwich/object_store.py
dulwich/objects.py
dulwich/tests/test_objects.py

index a3e6d22394450a1a5fcda77715b3d73c17c621f5..77bbed822c7b406622aec8ae406afccbdd2f5f14 100644 (file)
@@ -378,7 +378,7 @@ class DiskObjectStore(PackBasedObjectStore):
     def _get_loose_object(self, sha):
         path = self._get_shafile_path(sha)
         try:
     def _get_loose_object(self, sha):
         path = self._get_shafile_path(sha)
         try:
-            return ShaFile.from_file(path)
+            return ShaFile.from_path(path)
         except (OSError, IOError), e:
             if e.errno == errno.ENOENT:
                 return None
         except (OSError, IOError), e:
             if e.errno == errno.ENOENT:
                 return None
index 4102590d99f47adacac1493c859062c69831ea82..51ab7a47135cf1226d7817b507fb323ce10d6601 100644 (file)
@@ -256,7 +256,7 @@ class ShaFile(object):
         num_type = (ord(magic[0]) >> 4) & 7
         obj_class = object_class(num_type)
         if not obj_class:
         num_type = (ord(magic[0]) >> 4) & 7
         obj_class = object_class(num_type)
         if not obj_class:
-            raise ObjectFormatError("Not a known type: %d" % num_type)
+            raise ObjectFormatException("Not a known type: %d" % num_type)
         obj = obj_class()
         obj._filename = f.name
         return obj
         obj = obj_class()
         obj._filename = f.name
         return obj
@@ -318,21 +318,27 @@ class ShaFile(object):
             f.close()
 
     @classmethod
             f.close()
 
     @classmethod
-    def from_file(cls, filename):
-        """Get the contents of a SHA file on disk."""
-        f = GitFile(filename, 'rb')
+    def from_path(cls, path):
+        f = GitFile(path, 'rb')
         try:
         try:
-            try:
-                obj = cls._parse_file_header(f)
-                obj._sha = FixedSha(filename_to_hex(filename))
-                obj._needs_parsing = True
-                obj._needs_serialization = True
-                return obj
-            except (IndexError, ValueError), e:
-                raise ObjectFormatException("invalid object header")
+            obj = cls.from_file(f)
+            obj._sha = FixedSha(filename_to_hex(path))
+            return obj
         finally:
             f.close()
 
         finally:
             f.close()
 
+    @classmethod
+    def from_file(cls, f):
+        """Get the contents of a SHA file on disk."""
+        try:
+            obj = cls._parse_file_header(f)
+            obj._sha = None
+            obj._needs_parsing = True
+            obj._needs_serialization = True
+            return obj
+        except (IndexError, ValueError), e:
+            raise ObjectFormatException("invalid object header")
+
     @staticmethod
     def from_raw_string(type_num, string):
         """Creates an object of the indicated type from the raw string given.
     @staticmethod
     def from_raw_string(type_num, string):
         """Creates an object of the indicated type from the raw string given.
@@ -491,10 +497,10 @@ class Blob(ShaFile):
         "The text within the blob object, as chunks (not necessarily lines).")
 
     @classmethod
         "The text within the blob object, as chunks (not necessarily lines).")
 
     @classmethod
-    def from_file(cls, filename):
-        blob = ShaFile.from_file(filename)
+    def from_path(cls, path):
+        blob = ShaFile.from_path(path)
         if not isinstance(blob, cls):
         if not isinstance(blob, cls):
-            raise NotBlobError(filename)
+            raise NotBlobError(path)
         return blob
 
     def check(self):
         return blob
 
     def check(self):
@@ -539,8 +545,8 @@ class Tag(ShaFile):
         self._tag_timezone_neg_utc = False
 
     @classmethod
         self._tag_timezone_neg_utc = False
 
     @classmethod
-    def from_file(cls, filename):
-        tag = ShaFile.from_file(filename)
+    def from_path(cls, filename):
+        tag = ShaFile.from_path(filename)
         if not isinstance(tag, cls):
             raise NotTagError(filename)
         return tag
         if not isinstance(tag, cls):
             raise NotTagError(filename)
         return tag
@@ -625,7 +631,7 @@ class Tag(ShaFile):
             elif field is None:
                 self._message = value
             else:
             elif field is None:
                 self._message = value
             else:
-                raise ObjectFormatError("Unknown field %s" % field)
+                raise ObjectFormatException("Unknown field %s" % field)
 
     def _get_object(self):
         """Get the object pointed to by this tag.
 
     def _get_object(self):
         """Get the object pointed to by this tag.
@@ -711,8 +717,8 @@ class Tree(ShaFile):
         self._entries = {}
 
     @classmethod
         self._entries = {}
 
     @classmethod
-    def from_file(cls, filename):
-        tree = ShaFile.from_file(filename)
+    def from_path(cls, filename):
+        tree = ShaFile.from_path(filename)
         if not isinstance(tree, cls):
             raise NotTreeError(filename)
         return tree
         if not isinstance(tree, cls):
             raise NotTreeError(filename)
         return tree
@@ -860,10 +866,10 @@ class Commit(ShaFile):
         self._commit_timezone_neg_utc = False
 
     @classmethod
         self._commit_timezone_neg_utc = False
 
     @classmethod
-    def from_file(cls, filename):
-        commit = ShaFile.from_file(filename)
+    def from_path(cls, path):
+        commit = ShaFile.from_path(path)
         if not isinstance(commit, cls):
         if not isinstance(commit, cls):
-            raise NotCommitError(filename)
+            raise NotCommitError(path)
         return commit
 
     def _deserialize(self, chunks):
         return commit
 
     def _deserialize(self, chunks):
index 3d77d9fb1f4262e482e5ff518b012b32332e7ddc..5f32e61785ef3f48cacf3c21b794bbb3adc03353 100644 (file)
@@ -104,7 +104,7 @@ class BlobReadTests(unittest.TestCase):
 
     def get_sha_file(self, cls, base, sha):
         dir = os.path.join(os.path.dirname(__file__), 'data', base)
 
     def get_sha_file(self, cls, base, sha):
         dir = os.path.join(os.path.dirname(__file__), 'data', base)
-        return cls.from_file(hex_to_filename(dir, sha))
+        return cls.from_path(hex_to_filename(dir, sha))
 
     def get_blob(self, sha):
         """Return the blob named sha from the test data dir"""
 
     def get_blob(self, sha):
         """Return the blob named sha from the test data dir"""
@@ -424,7 +424,7 @@ class TreeTests(ShaFileCheckTests):
 
     def _do_test_parse_tree(self, parse_tree):
         dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
 
     def _do_test_parse_tree(self, parse_tree):
         dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
-        o = Tree.from_file(hex_to_filename(dir, tree_sha))
+        o = Tree.from_path(hex_to_filename(dir, tree_sha))
         o._parse_file()
         self.assertEquals([('a', 0100644, a_sha), ('b', 0100644, b_sha)],
                           list(parse_tree(o.as_raw_string())))
         o._parse_file()
         self.assertEquals([('a', 0100644, a_sha), ('b', 0100644, b_sha)],
                           list(parse_tree(o.as_raw_string())))