hachoir / hachoir-gtk / hachoir-gtk

#!/usr/bin/env python
import pygtk
import sys
import os
pygtk.require ('2.0') # 2.2 for Clipboard
import gtk.glade
from hachoir_core.cmd_line import unicodeFilename
from hachoir_parser import createParser

UI_FILENAME = os.path.dirname( __file__ ) + "/hachoir.glade"

class Interface:
    def __init__(self, fieldset, filename):
        self.xml = gtk.glade.XML(UI_FILENAME, "main_window")
        self.xml.signal_autoconnect(self)
        self.fieldset = fieldset
        self.table = self.xml.get_widget("table")

        self.xml.get_widget("main_window").set_title("Hachoir - %s" % filename)

        self.table.set_search_equal_func(resultSearchCb)

    def quit(self):
        gtk.main_quit()

    def on_quit_activate(self, widget):
        self.quit()

    def on_window_destroy(self, widget, data=None):
        self.quit()

    def treeview_add_column(self, treeview, name, num):
        col = gtk.TreeViewColumn(name)
        treeview.append_column(col)
        cell = gtk.CellRendererText()
        col.pack_start(cell, True)
        col.add_attribute(cell, 'text', num)
        treeview.set_search_column(num)
        return num+1

    def display_tree(self, parent):
        treeStore = gtk.TreeStore(str, str)
        self.table.set_model(treeStore)
        self.treeview_add_column(self.table, "name", 0)
        self.treeview_add_column(self.table, "value", 1)
        self.fill_tree(parent,None,treeStore)

    def fill_tree(self, parent, treeparent, treeStore):
        for field in parent:
            if field.hasValue():
                value = field.display
                #value = field.raw_display
            else:
                value = field.description
            newparent = treeStore.append(treeparent,(field.name,value))
            if field.is_field_set:
                self.fill_tree(field, newparent, treeStore)

def resultSearchCb (model, column, key, it):
    """Callback function for searching in treeview"""
    plainText = model.get_value(it, column)

    # if search text contains only lower-case characters, do case-insensitive matching:
    if key.islower():
        plainText = plainText.lower()

    # if the line contains the search text, it matches:
    if plainText.find(key) >= 0:
        return False

    # line doesn't match:
    return True


#import cProfile
# import profile

def main():
    if(len(sys.argv)!=2 ):
        print "usage: %s file" %sys.argv[0]
        return
    filename = sys.argv[1]

    # Create parser
    filename, realname = unicodeFilename(filename), filename
    fieldset = createParser(filename, realname)

    # test if there is a least one tree
    if  fieldset is  None :
        print "No fields to display in this file. This is probably a text file."
        return

    # Create window and gtk event loop
    interface = Interface(fieldset, filename)
    interface.display_tree(fieldset)
    gtk.main()

if __name__ == "__main__":
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.