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

Re: [augeas-devel] Simple pyGTK Augeas viewer



On Tue, 2009-03-10 at 17:11 -0700, David Lutterkort wrote:
> On Tue, 2009-03-10 at 19:23 -0400, David Malcolm wrote:
> > On Sun, 2009-03-08 at 20:30 -0700, David Lutterkort wrote:
> > > On Sat, 2009-03-07 at 12:33 -0500, David Malcolm wrote:
> > > > I was playing around with the python augeas bindings.
> > > > 
> > > > Attached is a simple pygtk app that lets you browse the full Augeas
> > > > tree, in a two column Path/Value treeview.
> > > 
> > > Nice :) Unfortunately, this segfaults for me on an F10 machine somewhere
> > > in _ctypes.so while its callin free(). Not sure what that is - an error
> > > in the bindings ?
> > 
> > Not sure.  For reference, it's working for me with these rpms:
> > augeas-devel-0.3.6-1.fc10.i386
> > python-augeas-0.3.0-1.fc10.noarch
> > augeas-libs-0.3.6-1.fc10.i386
> > augeas-0.3.6-1.fc10.i386
> > augeas-debuginfo-0.3.3-1.fc10.i386
> 
> Found the problem (this is with augeas-0.4.2, but will cause trouble
> with any Augeas >= 0.4.0): the very  first call to populate_tree_store
> is with a path of "/", resulting in a call to aug.match("//*") which
> since Augeas 0.4.0 means 'any node anywhere in the tree', following the
> XPath syntax.
> 
> If you change populate_tree_store to (sorry for the non-diff):
> 
>             def populate_tree_store(self, path, parent_iter):
>                 iter = self.treestore.append(parent_iter, [path, self.aug.get(path)])
>                 if path == "/":
>                     path = ""
>                 try:
>                     child_paths = self.aug.match(path+'/*')
>                 except RuntimeError:
>                     return
>                 for path in child_paths:
>                     self.populate_tree_store(path, iter),
> 
> But even with that, it still dies 3 out of 4 times somewhere deep in the
> guts of python - but at leasy I've seen the UI ;)

Attached is an updated version, with the above change, plus a right-hand
pane which tried to show you the file corresponding to the
currently-selected node.

[snip]
from augeas import Augeas
import pygtk
pygtk.require('2.0')
import gtk
import os

def add_with_scrolling(parent, child):
    sw = gtk.ScrolledWindow()
    sw.add(child)
    parent.add(sw)

class AugeasViewer:
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def populate_tree_store(self, path, parent_iter):
        iter = self.tree_store.append(parent_iter, [path, self.aug.get(path)])
        if path == "/":
            path = ""
        try:
            child_paths = self.aug.match(path+'/*')
        except RuntimeError:
            return        
        for path in child_paths:
            self.populate_tree_store(path, iter),

    def __init__(self):
        self.aug = Augeas()

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Augeas viewer")

        self.window.set_size_request(500, 400)

        self.window.connect("delete_event", self.delete_event)

        self.tree_store = gtk.TreeStore(str, str)
        self.populate_tree_store('/', None)

        self.treeview = gtk.TreeView(self.tree_store)

        for (i, columnName) in enumerate(['Path', 'Value']):
            column = gtk.TreeViewColumn(columnName)

            self.treeview.append_column(column)

            cr = gtk.CellRendererText()
            column.pack_start(cr, True)

            column.add_attribute(cr, 'text', i)
            column.set_sort_column_id(i)

        self.treeview.set_search_column(0)            

        self.textview = gtk.TextView()
        
        hbox = gtk.HBox()
        add_with_scrolling(hbox, self.treeview)
        add_with_scrolling(hbox, self.textview)
        self.window.add(hbox)

        self.treeview.get_selection().connect('changed', self.on_selection_changed, self)

        self.window.show_all()

    def on_selection_changed(self, selection, foo):
        (model, iter) = selection.get_selected()
        path = model.get_value(iter, 0)
        if path.startswith('/files/'):
            filename = path[6:]
            if os.path.isfile(filename):
                content = open(filename).read()
                self.textview.get_buffer().set_text(content)


if __name__ == "__main__":
   app = AugeasViewer()
   gtk.main()

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