← Back to team overview

dulwich-users team mailing list archive

[PATCH 2/3] Subclass TreeEntry in objects.py.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

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

Change-Id: If5ed4fcd3b1e3c50c86c5d448fff9624cbf410cf
---
 dulwich/_objects.c            |   12 +++++++-----
 dulwich/misc.py               |   12 ++++++------
 dulwich/objects.py            |   11 ++++++++++-
 dulwich/tests/test_objects.py |    4 +---
 4 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/dulwich/_objects.c b/dulwich/_objects.c
index e4ab236..a66c199 100644
--- a/dulwich/_objects.c
+++ b/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;
 }
diff --git a/dulwich/misc.py b/dulwich/misc.py
index e9e0f01..7c74751 100644
--- a/dulwich/misc.py
+++ b/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())
diff --git a/dulwich/objects.py b/dulwich/objects.py
index e406770..1878a1f 100644
--- a/dulwich/objects.py
+++ b/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.
 
diff --git a/dulwich/tests/test_objects.py b/dulwich/tests/test_objects.py
index 4fd4f42..6ee6961 100644
--- a/dulwich/tests/test_objects.py
+++ b/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,
-- 
1.7.2




References