from gi.repository import Gtk
from gi.repository import GObject

class MyModel(GObject.GObject, Gtk.TreeModel):
    def __init__(self):
        GObject.GObject.__init__(self)
        self.contents = [ '1', '2', '3', '4', '5' ]
        self.stamp = 1

    def do_get_flags(self):
        return Gtk.TreeModelFlags.LIST_ONLY | Gtk.TreeModelFlags.ITERS_PERSIST

    def do_get_n_columns(self):
        return 1

    def do_get_column_type(self, idx):
        if idx == 0:
            return str

        return None

    def do_get_iter(self, path):
        if ':' in str(path):
            return False, None

        pos = path.__iter__().next();

        if pos < 0 or pos >= len(self.contents):
            return False, None

        iterator = Gtk.TreeIter()
        iterator.stamp = self.stamp
        iterator.user_data = int(pos)
        return True, iterator

    def do_get_path(self, iterator):
        if iterator is None or iterator.stamp != self.stamp:
            return None

        pos = iterator.user_data

        if pos < 0 or pos >= len(self.contents):
            return None

        return Gtk.TreePath(pos)

    def do_get_value(self, iterator, idx):
        if idx != 0:
            return None

        if iterator.stamp != self.stamp:
            return None

        pos = iterator.user_data

        if pos < 0 or pos >= len(self.contents):
            return None

        return self.contents[pos]

    def do_iter_next (self, iterator):
        if iterator is None or iterator.stamp != self.stamp:
            return False

        pos = iterator.user_data + 1
        if pos < 0 or pos >= len(self.contents):
            return False

        iterator.user_data = pos
        return True

    def do_iter_children(self, parent):
        if parent is not None:
            return False, None

        iterator = Gtk.TreeIter()
        iterator.stamp = self.stamp
        iterator.user_data = 0
        return True, iterator

    def do_iter_has_child(self, iterator):
        if iterator is None:
            return True
        return False

    def do_iter_n_children(self, iterator):
        if iterator is not None:
            return 0

        return len(self.contents)

    def do_iter_nth_child(self, parent, idx):
        if parent is not None:
            return False, None

        if idx < 0 or idx >= len(self.contents):
            return False, None

        iterator = Gtk.TreeIter()
        iterator.stamp = self.stamp
        iterator.user_data = idx

        return True, iterator

    def do_iter_parent(self, child):
        return False, None

mymodel = MyModel()
view = Gtk.TreeView.new_with_model(mymodel)

col = Gtk.TreeViewColumn()
col.set_title("col")
renderer = Gtk.CellRendererText()
col.pack_start(renderer, True)
col.add_attribute(renderer, "text", 0)
view.append_column(col)

w = Gtk.Window()
w.add(view)
w.show_all()
Gtk.main()
