Browse Source

Subclass TreeEntry in objects.py.

This class logically belongs in objects.py, and this allows us to add
additional methods.

Change-Id: If5ed4fcd3b1e3c50c86c5d448fff9624cbf410cf
Signed-off-by: Jelmer Vernooij <jelmer@samba.org>
Dave Borowitz 14 years ago
parent
commit
3461ff3bbb
4 changed files with 24 additions and 15 deletions
  1. 7 5
      dulwich/_objects.c
  2. 6 6
      dulwich/misc.py
  3. 10 1
      dulwich/objects.py
  4. 1 3
      dulwich/tests/test_objects.py

+ 7 - 5
dulwich/_objects.c

@@ -246,18 +246,20 @@ static PyMethodDef py_objects_methods[] = {
 PyMODINIT_FUNC
 init_objects(void)
 {
-	PyObject *m, *misc_mod;
+	PyObject *m, *objects_mod;
 
 	m = Py_InitModule3("_objects", py_objects_methods, NULL);
 	if (m == NULL)
 		return;
 
-	misc_mod = PyImport_ImportModule("dulwich.misc");
-	if (misc_mod == NULL)
+	/* This is a circular import but should be safe since this module is
+	 * imported at at the very bottom of objects.py. */
+	objects_mod = PyImport_ImportModule("dulwich.objects");
+	if (objects_mod == NULL)
 		return;
 
-	tree_entry_cls = PyObject_GetAttrString(misc_mod, "TreeEntry");
-	Py_DECREF(misc_mod);
+	tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
+	Py_DECREF(objects_mod);
 	if (tree_entry_cls == NULL)
 		return;
 }

+ 6 - 6
dulwich/misc.py

@@ -104,7 +104,7 @@ def unpack_from(fmt, buf, offset=0):
 try:
     from collections import namedtuple
 
-    TreeEntry = namedtuple('TreeEntry', ['path', 'mode', 'sha'])
+    TreeEntryTuple = namedtuple('TreeEntryTuple', ['path', 'mode', 'sha'])
 except ImportError:
     # Provide manual implementations of namedtuples for Python <2.5.
     # If the class definitions change, be sure to keep these in sync by running
@@ -115,8 +115,8 @@ except ImportError:
     _property = property
     from operator import itemgetter as _itemgetter
 
-    class TreeEntry(tuple):
-            'TreeEntry(path, mode, sha)'
+    class TreeEntryTuple(tuple):
+            'TreeEntryTuple(path, mode, sha)'
 
             __slots__ = ()
 
@@ -127,21 +127,21 @@ except ImportError:
 
             @classmethod
             def _make(cls, iterable, new=tuple.__new__, len=len):
-                'Make a new TreeEntry object from a sequence or iterable'
+                'Make a new TreeEntryTuple object from a sequence or iterable'
                 result = new(cls, iterable)
                 if len(result) != 3:
                     raise TypeError('Expected 3 arguments, got %d' % len(result))
                 return result
 
             def __repr__(self):
-                return 'TreeEntry(path=%r, mode=%r, sha=%r)' % self
+                return 'TreeEntryTuple(path=%r, mode=%r, sha=%r)' % self
 
             def _asdict(t):
                 'Return a new dict which maps field names to their values'
                 return {'path': t[0], 'mode': t[1], 'sha': t[2]}
 
             def _replace(_self, **kwds):
-                'Return a new TreeEntry object replacing specified fields with new values'
+                'Return a new TreeEntryTuple object replacing specified fields with new values'
                 result = _self._make(map(kwds.pop, ('path', 'mode', 'sha'), _self))
                 if kwds:
                     raise ValueError('Got unexpected field names: %r' % kwds.keys())

+ 10 - 1
dulwich/objects.py

@@ -25,6 +25,7 @@ from cStringIO import (
     StringIO,
     )
 import os
+import posixpath
 import stat
 import zlib
 
@@ -39,7 +40,7 @@ from dulwich.errors import (
 from dulwich.file import GitFile
 from dulwich.misc import (
     make_sha,
-    TreeEntry,
+    TreeEntryTuple,
     )
 
 
@@ -685,6 +686,14 @@ class Tag(ShaFile):
     message = serializable_property("message", "The message attached to this tag")
 
 
+class TreeEntry(TreeEntryTuple):
+    """Namedtuple encapsulating a single tree entry."""
+
+    def in_path(self, path):
+        """Return a copy of this entry with the given path prepended."""
+        return TreeEntry(posixpath.join(path, self.path), self.mode, self.sha)
+
+
 def parse_tree(text):
     """Parse a tree text.
 

+ 1 - 3
dulwich/tests/test_objects.py

@@ -30,9 +30,6 @@ import stat
 from dulwich.errors import (
     ObjectFormatException,
     )
-from dulwich.misc import (
-    TreeEntry,
-    )
 from dulwich.objects import (
     Blob,
     Tree,
@@ -45,6 +42,7 @@ from dulwich.objects import (
     check_hexsha,
     check_identity,
     parse_timezone,
+    TreeEntry,
     parse_tree,
     _parse_tree_py,
     sorted_tree_items,