[pygobject] Override TreePath.__new__



commit dacfe618fa244445c979f1a5efa80c1f9a5a4ae9
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Dec 3 23:29:00 2010 +0100

    Override TreePath.__new__
    
    Instead of having a private _tree_path_from_string it is cleaner to
    override __new__ and it will be useful for all the api that take a
    TreePath or a string or a tuple.

 gi/overrides/Gtk.py     |   33 +++++++++++++++------------------
 tests/test_overrides.py |   18 ++++++++++++++++++
 2 files changed, 33 insertions(+), 18 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index dd87c78..37f8c95 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -615,30 +615,14 @@ class TreeModel(Gtk.TreeModel):
         return TreeModelRowIter(self, self.get_iter_first())
 
     def get_iter(self, path):
-        if isinstance(path, Gtk.TreePath):
-            pass
-        elif isinstance(path, (int, str,)):
-            path = self._tree_path_from_string(str(path))
-        elif isinstance(path, tuple):
-            path_str = ":".join(str(val) for val in path)
-            path = self._tree_path_from_string(path_str)
-        else:
-            raise TypeError("tree path must be one of Gtk.TreeIter, Gtk.TreePath, \
-                int, str or tuple, not %s" % type(path).__name__)
+        if not isinstance(path, Gtk.TreePath):
+            path = TreePath(path)
 
         success, aiter = super(TreeModel, self).get_iter(path)
         if not success:
             raise ValueError("invalid tree path '%s'" % path)
         return aiter
 
-    def _tree_path_from_string(self, path):
-        if len(path) == 0:
-            raise TypeError("could not parse subscript '%s' as a tree path" % path)
-        try:
-            return TreePath.new_from_string(path)
-        except TypeError:
-            raise TypeError("could not parse subscript '%s' as a tree path" % path)
-
     def get_iter_first(self):
         success, aiter = super(TreeModel, self).get_iter_first()
         if success:
@@ -846,6 +830,19 @@ __all__.append('TreeModelRowIter')
 
 class TreePath(Gtk.TreePath):
 
+    def __new__(cls, path=0):
+        if isinstance(path, int):
+            path = str(path)
+        elif isinstance(path, tuple):
+            path = ":".join(str(val) for val in path)
+
+        if len(path) == 0:
+            raise TypeError("could not parse subscript '%s' as a tree path" % path)
+        try:
+            return TreePath.new_from_string(path)
+        except TypeError:
+            raise TypeError("could not parse subscript '%s' as a tree path" % path)
+
     def __str__(self):
         return self.to_string()
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index afd5f3d..5f2955a 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -434,6 +434,24 @@ class TestGtk(unittest.TestCase):
 
         self.assertEquals(i, 99)
 
+    def test_tree_path(self):
+        p1 = Gtk.TreePath()
+        p2 = Gtk.TreePath.new_first()
+        self.assertEqual(p1, p2)
+        self.assertEqual(str(p1), '0')
+        p1 = Gtk.TreePath(2)
+        p2 = Gtk.TreePath.new_from_string('2')
+        self.assertEqual(p1, p2)
+        self.assertEqual(str(p1), '2')
+        p1 = Gtk.TreePath('1:2:3')
+        p2 = Gtk.TreePath.new_from_string('1:2:3')
+        self.assertEqual(p1, p2)
+        self.assertEqual(str(p1), '1:2:3')
+        p1 = Gtk.TreePath((1,2,3))
+        p2 = Gtk.TreePath.new_from_string('1:2:3')
+        self.assertEqual(p1, p2)
+        self.assertEqual(str(p1), '1:2:3')
+
     def test_tree_model(self):
         tree_store = Gtk.TreeStore(int, str)
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]