[pygobject] add (Tree|List)Store set method override



commit cadbd4142bd0045368b5123d4b0a1876bdd5d798
Author: John (J5) Palmieri <johnp redhat com>
Date:   Sat Aug 13 11:03:07 2011 -0400

    add (Tree|List)Store set method override

 gi/overrides/Gtk.py     |   50 +++++++++++++++++++++++
 tests/test_overrides.py |  102 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 149 insertions(+), 3 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 5e378ef..6fd7c99 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -961,6 +961,31 @@ class ListStore(Gtk.ListStore, TreeModel, TreeSortable):
         value = self._convert_value(treeiter, column, value)
         Gtk.ListStore.set_value(self, treeiter, column, value)
 
+    def set(self, treeiter, *args):
+
+        def _set_lists(columns, values):
+            if len(columns) != len(values):
+                raise TypeError('The number of columns do not match the number of values')
+            for col_num, val in zip(columns, values):
+                if not isinstance(col_num, int):
+                    raise TypeError('TypeError: Expected integer argument for column.')
+                self.set_value(treeiter, col_num, val)
+
+        if args:
+            if isinstance(args[0], int):
+                columns = args[::2]
+                values = args[1::2]
+                _set_lists(columns, values)
+            elif isinstance(args[0], (tuple, list)):
+                if len(args) != 2:
+                    raise TypeError('Too many arguments');
+                _set_lists(args[0], args[1])
+            elif isinstance(args[0], dict):
+                columns = args[0].keys()
+                values = args[0].values()
+                _set_lists(columns, values)
+            else:
+                raise TypeError('Argument list must be in the form of (column, value, ...), ((columns,...), (values, ...)) or {column: value}.  No -1 termination is needed.')
 
 ListStore = override(ListStore)
 __all__.append('ListStore')
@@ -1135,6 +1160,31 @@ class TreeStore(Gtk.TreeStore, TreeModel, TreeSortable):
         value = self._convert_value(treeiter, column, value)
         Gtk.TreeStore.set_value(self, treeiter, column, value)
 
+    def set(self, treeiter, *args):
+
+        def _set_lists(columns, values):
+            if len(columns) != len(values):
+                raise TypeError('The number of columns do not match the number of values')
+            for col_num, val in zip(columns, values):
+                if not isinstance(col_num, int):
+                    raise TypeError('TypeError: Expected integer argument for column.')
+                self.set_value(treeiter, col_num, val)
+
+        if args:
+            if isinstance(args[0], int):
+                columns = args[::2]
+                values = args[1::2]
+                _set_lists(columns, values)
+            elif isinstance(args[0], (tuple, list)):
+                if len(args) != 2:
+                    raise TypeError('Too many arguments');
+                _set_lists(args[0], args[1])
+            elif isinstance(args[0], dict):
+                columns = args[0].keys()
+                values = args[0].values()
+                _set_lists(columns, values)
+            else:
+                raise TypeError('Argument list must be in the form of (column, value, ...), ((columns,...), (values, ...)) or {column: value}.  No -1 termination is needed.')
 
 TreeStore = override(TreeStore)
 __all__.append('TreeStore')
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index d6b09bd..14114b7 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -721,7 +721,7 @@ class TestGtk(unittest.TestCase):
                                    GObject.TYPE_CHAR)
 
         parent = None
-        for i in range(100):
+        for i in range(97):
             label = 'this is child #%d' % i
             testobj = TestGtk.TestClass(self, i, label)
             parent = tree_store.append(parent, (i,
@@ -740,6 +740,67 @@ class TestGtk(unittest.TestCase):
                                                 254,
                                                 _bytes('a')
                                                 ))
+        # test set
+        parent = tree_store.append(parent)
+        i = 97
+        label = 'this is child #%d' % i
+        testobj = TestGtk.TestClass(self, i, label)
+        tree_store.set(parent, 0, i,
+                               2, testobj,
+                               1, label,
+                               3, testobj,
+                               4, test_pyobj,
+                               5, test_pydict,
+                               6, test_pylist,
+                               7, i % 2,
+                               8, bool(i % 2),
+                               9, i,
+                               10, GObject.G_MAXULONG,
+                               11, GObject.G_MININT64,
+                               12, 0xffffffffffffffff,
+                               13, 254,
+                               14, _bytes('a'))
+
+        parent = tree_store.append(parent)
+        i = 98
+        label = 'this is child #%d' % i
+        testobj = TestGtk.TestClass(self, i, label)
+        tree_store.set(parent, {0: i,
+                                2: testobj,
+                                1: label,
+                                3: testobj,
+                                4: test_pyobj,
+                                5: test_pydict,
+                                6: test_pylist,
+                                7: i % 2,
+                                8: bool(i % 2),
+                                9: i,
+                                10: GObject.G_MAXULONG,
+                                11: GObject.G_MININT64,
+                                12: 0xffffffffffffffff,
+                                13: 254,
+                                14: _bytes('a')})
+
+        parent = tree_store.append(parent)
+        i = 99
+        label = 'this is child #%d' % i
+        testobj = TestGtk.TestClass(self, i, label)
+        tree_store.set(parent, (0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14),
+                               (i,
+                                testobj,
+                                label,
+                                testobj,
+                                test_pyobj,
+                                test_pydict,
+                                test_pylist,
+                                i % 2,
+                                bool(i % 2),
+                                i,
+                                GObject.G_MAXULONG,
+                                GObject.G_MININT64,
+                                0xffffffffffffffff,
+                                254,
+                                _bytes('a')))
 
         # len gets the number of children in the root node
         # since we kept appending to the previous node
@@ -911,7 +972,42 @@ class TestGtk(unittest.TestCase):
                                1,
                                True))
 
-        self.assertEquals(len(list_store), 100)
+        i = 100
+        label = 'this is row #100'
+        treeiter = list_store.append()
+        list_store.set(treeiter, 1, label,
+                                 0, i,
+                                 2, TestGtk.TestClass(self, i, label),
+                                 3, test_pyobj,
+                                 4, test_pydict,
+                                 5, test_pylist,
+                                 6, 0,
+                                 7, False)
+        i = 101
+        label = 'this is row #101'
+        treeiter = list_store.append()
+        list_store.set(treeiter, {1: label,
+                                  0: i,
+                                  2: TestGtk.TestClass(self, i, label),
+                                  3: test_pyobj,
+                                  4: test_pydict,
+                                  5: test_pylist,
+                                  6: 1,
+                                  7: True})
+        i = 102
+        label = 'this is row #102'
+        treeiter = list_store.append()
+        list_store.set(treeiter, (1, 0, 2, 3, 4, 5, 6, 7),
+                                  (label,
+                                   i,
+                                   TestGtk.TestClass(self, i, label),
+                                   test_pyobj,
+                                   test_pydict,
+                                   test_pylist,
+                                   0,
+                                   False))
+
+        self.assertEquals(len(list_store), 103)
 
         # walk the list to see if the values were stored correctly
         i = 0
@@ -942,7 +1038,7 @@ class TestGtk(unittest.TestCase):
 
             counter += 1
 
-        self.assertEquals(i, 99)
+        self.assertEquals(i, 102)
 
     def test_tree_path(self):
         p1 = Gtk.TreePath()



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